Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 5 Using methods Why methods ? Manage complexity Manage complexity Better program organization Better program organization Next step in understanding.

Similar presentations


Presentation on theme: "Lab 5 Using methods Why methods ? Manage complexity Manage complexity Better program organization Better program organization Next step in understanding."— Presentation transcript:

1

2 Lab 5 Using methods

3 Why methods ? Manage complexity Manage complexity Better program organization Better program organization Next step in understanding abstractions Next step in understanding abstractions Reusability of code Reusability of code

4 What’s an interface? Limits how much we see Limits how much we see Provides a view that is simpler than reality Provides a view that is simpler than reality Your car: things in the “interface” Your car: things in the “interface” YESYES Steering wheel Steering wheel Gauges Gauges Key Key NONO the fuel injector/radiator/etc the fuel injector/radiator/etc

5 How does this relate to programming? Programs are complicated Programs are complicated Algorithms are complicated Algorithms are complicated Do we really need to see the details of everything? Do we really need to see the details of everything? How can we package programs to limit the details to which we are exposed? How can we package programs to limit the details to which we are exposed?

6 METHODS

7 Recall the factorial algorithm int fact = 1; for (i=1; i<=n; i++) { fact = fact * i; } { fact = fact * i; } Do I need to rewrite this every time I use it?

8 Take a “black-box” view Only consider what goes in and what comes out. Only consider what goes in and what comes out. Black Box Input Output

9 “factorial” black-box factorial 5 120

10 “factorial” black-box (in) factorial 5 120 int factorial (int n) What you pass in

11 “factorial” black-box (out) factorial 5 120 int factorial (int n) What is sent back to the caller

12 When something comes “out” where does it go? To the part of the program that calls it. To the part of the program that calls it. Consider Consider System.out.println(“factorial of 5 is 120”);System.out.println(“factorial of 5 is 120”);And System.out.println(“factorial of “+n+” is “ + factorial(n));System.out.println(“factorial of “+n+” is “ + factorial(n)); The result is passed back to the println method which will print it. The result is passed back to the println method which will print it.

13 Putting it together Write the method Write the method 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 method Use the method n = 5; System.out.println(“factorial of “+n+” is “ + factorial(n));

14 Putting it together – data in Write the method Write the method 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 method Use the method n = 5; System.out.println(“factorial of “+n+” is “ + factorial(n));

15 Putting it together – result out Write the method Write the method 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 method Use the method n = 5; System.out.println(“factorial of “+n+” is “ + factorial(n));

16 Parameter “n” is input result “returned” is output Write the method Write the method 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 method Use the method n = 5; System.out.println(“factorial of “+n+” is “ + factorial(n));

17 Why Methods? By adding a simple interface By adding a simple interface We are able to package the method for easy reuse We are able to package the method for easy reuse This is not the only advantage of a method, but it’s an important one This is not the only advantage of a method, but it’s an important one


Download ppt "Lab 5 Using methods Why methods ? Manage complexity Manage complexity Better program organization Better program organization Next step in understanding."

Similar presentations


Ads by Google