Programming Methodology (1). Implementing Methods main.
-
Upload
carson-denier -
Category
Documents
-
view
221 -
download
2
Transcript of Programming Methodology (1). Implementing Methods main.
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.
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
Using methods: a first example…
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( );
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Another example:
The DisplayStars program
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( );
Let’s look at the version of DisplayStars where the user chooses the size of the square..
* * *
* * *
* * *
Let’s look at the version of DisplayStars where the user chooses the size of the square..
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
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( );}
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
Remember parameters are copies!
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
Some more examples of writing methods: add
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) ;
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)
Some more examples of writing methods: isEven
if( ){
}else{
}
isEven( ) int numberIn private static boolean
{
return true;
return false;
numberIn % 2 == 0
}
return (numberIn % 2 == 0 );
}
Using the isEven method….
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
Revisiting the DisplayStars Program
* * *
* * *
* * *
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( );
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
Method overloading
Drawing a rectangle instead of a square..
* * * * *
* * * * * * * * * *
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
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”
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 );
Using methods in menu driven programs.
*** 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
*** 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!
*** 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
*** 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
*** 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
public static void main (String [ ] args){ char choice; // declare more variables do {
} while (choice != ‘4’);}
// display menu
// get choice
// process choice (using ‘switch’)
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;
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;
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;
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;
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”);}
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?
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?
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?
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
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
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
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
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
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
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?
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?
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
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
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
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
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
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
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
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
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
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
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
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);
}}
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);