Programming Methodology (1). Implementing Methods main.

81
Programming Methodology (1)

Transcript of Programming Methodology (1). Implementing Methods main.

Page 1: Programming Methodology (1). Implementing Methods main.

Programming Methodology (1)

Page 2: Programming Methodology (1). Implementing Methods main.

Implementing Methods

main

Page 3: Programming Methodology (1). Implementing Methods main.

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.

Page 4: Programming Methodology (1). Implementing Methods main.

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

Page 5: Programming Methodology (1). Implementing Methods main.

Using methods: a first example…

Page 6: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );

Page 7: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 8: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 9: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 10: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 11: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 12: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 13: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 14: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 15: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 16: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 17: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 18: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 19: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 20: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 21: Programming Methodology (1). Implementing Methods main.

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( )private static void

displayMessage( );

displayMessage( );// more code here

Page 22: Programming Methodology (1). Implementing Methods main.

Another example:

The DisplayStars program

Page 23: Programming Methodology (1). Implementing Methods main.

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( );

Page 24: Programming Methodology (1). Implementing Methods main.

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

* * *

* * *

* * *

Page 25: Programming Methodology (1). Implementing Methods main.

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

* * * * *

* * * * *

* * * * *

* * * * *

* * * * *

Page 26: Programming Methodology (1). Implementing Methods main.

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( );}

Page 27: Programming Methodology (1). Implementing Methods main.

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

num

showStars( num );

int )

numIn

numIn

numIn

num

numIn

?actual parameter

formal parameter

Page 28: Programming Methodology (1). Implementing Methods main.

Remember parameters are copies!

Page 29: Programming Methodology (1). Implementing Methods main.

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

Page 30: Programming Methodology (1). Implementing Methods main.

Some more examples of writing methods: add

Page 31: Programming Methodology (1). Implementing Methods main.

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) ;

Page 32: Programming Methodology (1). Implementing Methods main.

public static void main (String [ ] args)

{

}

int x, y, z; x = 10; y = 20;

x + y;

private static add ( )int num1, int num2int{

}

return num1 + num2;

add (x, y) ;z =System.out.println( ); x + yadd (x, y)

Page 33: Programming Methodology (1). Implementing Methods main.

Some more examples of writing methods: isEven

Page 34: Programming Methodology (1). Implementing Methods main.

if( ){

}else{

}

isEven( ) int numberIn private static boolean

{

return true;

return false;

numberIn % 2 == 0

}

return (numberIn % 2 == 0 );

}

Page 35: Programming Methodology (1). Implementing Methods main.

Using the isEven method….

Page 36: Programming Methodology (1). Implementing Methods main.

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

Page 37: Programming Methodology (1). Implementing Methods main.

Revisiting the DisplayStars Program

* * *

* * *

* * *

Page 38: Programming Methodology (1). Implementing Methods main.

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( );

Page 39: Programming Methodology (1). Implementing Methods main.

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

Page 40: Programming Methodology (1). Implementing Methods main.

Method overloading

Page 41: Programming Methodology (1). Implementing Methods main.

Drawing a rectangle instead of a square..

* * * * *

* * * * * * * * * *

Page 42: Programming Methodology (1). Implementing Methods main.

private static void showStars( int{ for(int i = 1; i <= ; i++) {

for (int j = 1; j<= ; j++) {

System.out.print("*"); } System.out.println(); }}

numIn

numIn

numIn) )rowIn

colIn

rowIn

colIn, int

Page 43: Programming Methodology (1). Implementing Methods main.

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”

Page 44: Programming Methodology (1). Implementing Methods main.

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 );

Page 45: Programming Methodology (1). Implementing Methods main.

Using methods in menu driven programs.

Page 46: Programming Methodology (1). Implementing Methods main.

*** 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

Page 47: Programming Methodology (1). Implementing Methods main.

*** 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!

Page 48: Programming Methodology (1). Implementing Methods main.

*** 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

Page 49: Programming Methodology (1). Implementing Methods main.

*** 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

Page 50: Programming Methodology (1). Implementing Methods main.

*** 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

Page 51: Programming Methodology (1). Implementing Methods main.

public static void main (String [ ] args){ char choice; // declare more variables do {

} while (choice != ‘4’);}

// display menu

// get choice

// process choice (using ‘switch’)

Page 52: Programming Methodology (1). Implementing Methods main.

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;

break;

break;case ‘4’: System.out.println(“Goodbye”);

break;

Page 53: Programming Methodology (1). Implementing Methods main.

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;

break;

break;

option1( );

case ‘4’: System.out.println(“Goodbye”);break;

Page 54: Programming Methodology (1). Implementing Methods main.

switch (choice){

}

case ‘1’:

case ‘2’:

case ‘3’: System.out.println(“11.00am”);

default: System.out.println(“1-4 only”);

break;

break;

break;

option1( );

option2( );

case ‘4’: System.out.println(“Goodbye”);break;

Page 55: Programming Methodology (1). Implementing Methods main.

switch (choice){

}

case ‘1’:

case ‘2’:

case ‘3’:

default: System.out.println(“1-4 only”);

break;

break;

break;

option1( );

option2( );

option3( );

case ‘4’: System.out.println(“Goodbye”);break;

Page 56: Programming Methodology (1). Implementing Methods main.

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”);}

Page 57: Programming Methodology (1). Implementing Methods main.
Page 58: Programming Methodology (1). Implementing Methods main.

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?

Page 59: Programming Methodology (1). Implementing Methods main.

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?

Page 60: Programming Methodology (1). Implementing Methods main.

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?

Page 61: Programming Methodology (1). Implementing Methods main.

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

Page 62: Programming Methodology (1). Implementing Methods main.

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?

3firstIn

Page 63: Programming Methodology (1). Implementing Methods main.

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

Page 64: Programming Methodology (1). Implementing Methods main.

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

Page 65: Programming Methodology (1). Implementing Methods main.

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

Page 66: Programming Methodology (1). Implementing Methods main.

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

Page 67: Programming Methodology (1). Implementing Methods main.

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?

Page 68: Programming Methodology (1). Implementing Methods main.

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?

Page 69: Programming Methodology (1). Implementing Methods main.

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

Page 70: Programming Methodology (1). Implementing Methods main.

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

Page 71: Programming Methodology (1). Implementing Methods main.

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

Page 72: Programming Methodology (1). Implementing Methods main.

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

Page 73: Programming Methodology (1). Implementing Methods main.

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

Page 74: Programming Methodology (1). Implementing Methods main.

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

Page 75: Programming Methodology (1). Implementing Methods main.

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

Page 76: Programming Methodology (1). Implementing Methods main.

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

Page 77: Programming Methodology (1). Implementing Methods main.

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

Page 78: Programming Methodology (1). Implementing Methods main.

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

Page 79: Programming Methodology (1). Implementing Methods main.

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?

-218

Page 80: Programming Methodology (1). Implementing Methods main.

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);

}}

Page 81: Programming Methodology (1). Implementing Methods main.

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);