lab 5 using methods why methods ? manage complexity manage complexity better program organization...
Post on 22-Dec-2015
218 views
TRANSCRIPT
Why methods ?Why methods ?
Manage complexityManage complexity Better program organizationBetter program organization Next step in understanding Next step in understanding
abstractionsabstractions Reusability of codeReusability of code
What’s an interface?What’s an interface?
Limits how much we see Limits how much we see Provides a view that is simpler than Provides a view that is simpler than
realityreality Your car: things in the “interface”Your car: things in the “interface”
• YESYES Steering wheelSteering wheel GaugesGauges KeyKey
• NO NO the fuel injector/radiator/etcthe fuel injector/radiator/etc
How does this relate to How does this relate to programming?programming?
Programs are complicatedPrograms are complicated Algorithms are complicatedAlgorithms are complicated Do we really need to see the details Do we really need to see the details
of everything?of everything? How can we package programs to How can we package programs to
limit the details to which we are limit the details to which we are exposed?exposed?
Recall the factorial algorithmRecall the factorial algorithm
int fact = 1; int fact = 1; for (i=1; i<=n; i++)for (i=1; i<=n; i++) { fact = fact * i; }{ fact = fact * i; }
Do I need to rewrite this Do I need to rewrite this every time I use it?every time I use it?
Take a “black-box” viewTake a “black-box” view
Only consider what goes in and what Only consider what goes in and what comes out.comes out.
Black BoxInput Output
““factorial” black-box (in)factorial” black-box (in)
factorial5 120
int factorial (int n)
What you pass in
““factorial” black-box (out)factorial” black-box (out)
factorial5 120
int factorial (int n)
What is sent back to the caller
When something comes “out”When something comes “out”where does it go?where does it go?
To the part of the program that calls it.To the part of the program that calls it. ConsiderConsider
• System.out.println(“factorial of 5 is 120”);System.out.println(“factorial of 5 is 120”);
AndAnd• System.out.println(“factorial of “+n+” is “ + System.out.println(“factorial of “+n+” is “ + factorial(n));factorial(n));
The result is passed back to the println The result is passed back to the println method which will print it.method which will print it.
Putting it togetherPutting it together Write the methodWrite the method
int factorial (int n)int factorial (int n){{ int fact = 1; int i;int fact = 1; int i; for (i=1; i<=n; i++)for (i=1; i<=n; i++) { fact = fact * i; }{ fact = fact * i; } return fact;return fact;}}
Use the methodUse the methodn = 5;n = 5;
System.out.println(“factorial of “+n+” is “ + factorial(n));System.out.println(“factorial of “+n+” is “ + factorial(n));
Putting it together – data inPutting it together – data in Write the methodWrite the method
int factorial (int n)int factorial (int n){{ int fact = 1; int i;int fact = 1; int i; for (i=1; i<=n; i++)for (i=1; i<=n; i++) { fact = fact * i; }{ fact = fact * i; } return fact;return fact;}}
Use the methodUse the methodn = 5;n = 5;
System.out.println(“factorial of “+n+” is “ + factorial(n));System.out.println(“factorial of “+n+” is “ + factorial(n));
Putting it together – result outPutting it together – result out Write the methodWrite the method
int factorial (int n)int factorial (int n){{ int fact = 1; int i;int fact = 1; int i; for (i=1; i<=n; i++)for (i=1; i<=n; i++) { fact = fact * i; }{ fact = fact * i; } return fact;return fact;}}
Use the methodUse the methodn = 5;n = 5;
System.out.println(“factorial of “+n+” is “ + factorial(n));System.out.println(“factorial of “+n+” is “ + factorial(n));
Parameter “n” is inputParameter “n” is inputresult “returned” is outputresult “returned” is output
Write the methodWrite the method
int factorial (int n)int factorial (int n){{ int fact = 1; int i;int fact = 1; int i; for (i=1; i<=n; i++)for (i=1; i<=n; i++) { fact = fact * i; }{ fact = fact * i; } return fact;return fact;}}
Use the methodUse the methodn = 5;n = 5;
System.out.println(“factorial of “+n+” is “ + factorial(n));System.out.println(“factorial of “+n+” is “ + factorial(n));
Why Methods?Why Methods?
By adding a simple interfaceBy adding a simple interface We are able to package the method We are able to package the method
for easy reusefor easy reuse This is not the only advantage of a This is not the only advantage of a
method, but it’s an important onemethod, but it’s an important one