Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Methodology (1). Implementing Methods main.

Similar presentations


Presentation on theme: "Programming Methodology (1). Implementing Methods main."— Presentation transcript:

1 Programming Methodology (1)

2 Implementing Methods main

3 Learning objectives explain the meaning of the term method; declare and define methods; call a method; explain the meaning of the terms actual parameters and formal parameters; return a result from a method; explain the meaning of the term polymorphism; declare and use overloaded methods; use methods to implement menu driven programs.

4 public static void main(String[ ] args) { } // code to accomplish task A // code to accomplish task B // code to accomplish task A { } method A CALL method A CALL method A // code to accomplish task A

5 Using methods: a first example…

6 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); firstName = sc.next(); System.out.println("Please enter your family name"); // more code here } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); displayMessage( );

7 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

8 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

9 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

10 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

11 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

12 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

13 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

14 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

15 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

16 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

17 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

18 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

19 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

20 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

21 public static void main(String[ ] args) { // variables declared here System.out.println("Please enter your first name"); firstName = sc.next(); System.out.println("Please enter your family name"); } System.out.println(“All information supplied is confidential"); System.out.println("No personal details will be shared "); {}{} displayMessage( )privatestaticvoid displayMessage( ); // more code here

22 Another example: The DisplayStars program

23 public static void main (String [ ] args) { } for (int i = 1; i < = 5; i++) { System.out.println(*****); } for (int i = 1; i < = 5; i++) { System.out.println(*****); } private static void showStars( ) { } showStars( );

24 Let’s look at the version of DisplayStars where the user chooses the size of the square.. * * * * * * * * *

25 * * * * * * * * * * * * * * * * * * * * * * * * *

26 public static void main(String[ ] args) { int num; Scanner sc = new Scanner (System.in); System.out.println("Size of square?"); num = sc.nextInt(); for (int i = 1; i <= num ; i++) { for (int j = 1; j<= num ; j++) { System.out.print("*"); } System.out.println(); } } private static void showStars( ) { } showStars( ); }

27 public static void main(String[ ] args) { int num; Scanner sc = new Scanner (System.in); System.out.println("Size of square?"); num = sc.nextInt(); for (int i = 1; i <= ; i++) { for (int j = 1; j<= ; j++) { System.out.print("*"); } System.out.println(); } private static void showStars( ) { } showStars( ); } num showStars( num ); int ) numIn numIn num numIn ? actual parameter formal parameter

28 Remember parameters are copies!

29 public static void main(String[ ] args) { int x = 1; method1(x); System.out.print(x); } private static void method1(int { } x ) x = x + 1; System.out.print(x); xIn = xIn + 1; System.out.print(xIn); xIn ) xxIn x 1 12

30 Some more examples of writing methods: add

31 public static void main (String [ ] args) { } int x, y, z; x = 10; y = 20; z = x + y; private static add ( )?int num1, num2int num1, int num2int {}{} int sum = num1 + num2; return sum; return num1 + num2; add (x, y) ;

32 public static void main (String [ ] args) { } int x, y, z; x = 10; y = 20; x + y; private staticadd ( )int num1, int num2int {}{} return num1 + num2; add (x, y) ;z = System.out.println( );x + y add (x, y)

33 Some more examples of writing methods: isEven

34 if( ) { } else { } isEven( )intnumberInprivatestaticboolean { return true; return false; numberIn % 2 == 0 } return (numberIn % 2 == 0 ); }

35 Using the isEven method….

36 public static void main (String [ ] args) { } int num = 10; if( ) { } isEven(num)== true System.out.println(“number is odd”); System.out.println(“number is even”); !== false

37 Revisiting the DisplayStars Program * * * * * * * * *

38 public static void main(String[ ] args) { int num; num = showStars(num); } private static void showStars( int numIn) { // code goes here } Scanner sc = new Scanner (System.in); System.out.println("Size of square?"); sc.nextInt();getSize( );

39 private static getSize( ) { } public static void main(String[ ] args) { int num; num = showStars(num); } private static void showStars( int numIn) { // code goes here } Scanner sc = new Scanner (System.in); System.out.println("Size of square?"); size = sc.nextInt(); getSize( ); int size; return size; int

40 Method overloading

41 Drawing a rectangle instead of a square.. * * * * * * * * * * * * * * *

42 private static void showStars( int { for(int i = 1; i <= ; i++) { for (int j = 1; j<= ; j++) { System.out.print("*"); } System.out.println(); } numIn ))rowIn colIn rowIn colIn, int

43 private static void showStars(int numIn) { // code to draw square here } private static void showStars(int rowIn, int colIn) { // code to draw rectangle here } Two or more methods with the same name performing different functions: “METHOD OVERLOADING” Method overloading is an example of “POLYMORPHISM”

44 private static void showStars(int numIn) { // code to draw square here } private static void showStars(int rowIn, int colIn) { // code to draw rectangle here } public static void main (String [ ] args) { } showStars( 4, 7); showStars( 6 );

45 Using methods in menu driven programs.

46 *** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 2 1.OOp.m

47 *** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 5 Options 1-4 only!

48 *** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 1 10.OOa.m

49 *** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 3 11.00a.m

50 *** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 4 Goodbye

51 public static void main (String [ ] args) { char choice; // declare more variables do { } while (choice != ‘4’); } // display menu // get choice // process choice (using ‘switch’)

52 switch (choice) { } case ‘1’: case ‘2’: case ‘3’: System.out.println(“10.00am”); System.out.println(“1.00pm”); System.out.println(“11.00am”); default:System.out.println(“1-4 only”); break; case ‘4’:System.out.println(“Goodbye”); break;

53 switch (choice) { } case ‘1’: case ‘2’: case ‘3’: System.out.println(“1.00pm”); System.out.println(“11.00am”); default:System.out.println(“1-4 only”); break; option1( ); case ‘4’:System.out.println(“Goodbye”); break;

54 switch (choice) { } case ‘1’: case ‘2’: case ‘3’:System.out.println(“11.00am”); default:System.out.println(“1-4 only”); break; option1( ); option2( ); case ‘4’:System.out.println(“Goodbye”); break;

55 switch (choice) { } case ‘1’: case ‘2’: case ‘3’: default:System.out.println(“1-4 only”); break; option1( ); option2( ); option3( ); case ‘4’:System.out.println(“Goodbye”); break;

56 private static void option1 ( ) { System.out.println(“10.00am”); } private static void option2 ( ) { System.out.println(“1.00pm”); } private static void option3 ( ) { System.out.println(“11.00am”); }

57

58 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run?

59 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run?

60 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run?

61 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? firstIn

62 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? 3 firstIn

63 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? firstIn 3 secondIn

64 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? firstIn 3 secondIn 5

65 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? firstIn 3 secondIn 5

66 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? firstIn 3 secondIn 5

67 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( -2 ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run?

68 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run?

69 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? -2

70 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? -2

71 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? -2

72 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? -2

73 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? -2

74 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? -2

75 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? -2

76 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? -2

77 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( 18 ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? -2

78 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? -2

79 public class MethodsQ1 { public static void main(String[ ] args) { System.out.println( myMethod( 3, 5) ); System.out.println( myMethod( 3, 5, 10) ); } private static int myMethod(int firstIn, int secondIn, int thirdIn) { return firstIn + secondIn + thirdIn; } private static int myMethod(int firstIn, int secondIn) { return firstIn - secondIn; } What would be displayed on the screen when this program was run? -2 18

80 import java.util.*; public class FindCost3 { public static void main(String[ ] args ) { Scanner sc = new Scanner(System.in); double price, tax; System.out.println("*** Product Price Check ***"); System.out.print("Enter initial price: "); price = sc.nextDouble(); System.out.print("Enter tax rate: "); tax = sc.nextDouble(); price = price * (1 + tax/100); System.out.println("Cost after tax = " + price); }

81 import java.util.*; public class FindCost3 { public static void main(String[ ] args ) { double price, tax; } // call method to display title // call method to get price // call method to get tax // call method to calculate new price // call method to display result displayTitle( ); price = getPrice( ); tax = getTax( ); price = calculate( price, tax); displayResult( price);


Download ppt "Programming Methodology (1). Implementing Methods main."

Similar presentations


Ads by Google