|
that can help you remember the construct of a class
They are similar in the following manner:
Class (in Java) | Book Chapter (English) |
---|---|
A class contains a number of related methods | A chapter contains a number of related paragraphs |
A method contains a number of statements | A paragraph contains a number of sentences |
A statement is the smallest unit of execution in the Java language | A sentence is the smallest unit of readable text in the English language |
|
|
|
|
if ( x < y ) { min = x; // x is the smaller value } else { min = y; // y is the smaller value } |
|
Remember: a method is always contained inside some class in Java
public class ToolBox // Methods must be contained inside a class { /* ---------------------------------------- A method called "min" that contains the statements to find the smaller of 2 values a and b --------------------------------------- */ public static double min ( double a, double b ) { double m = 0; if ( a < b ) { m = a; // a is the smaller value } else { m = b; // b is the smaller value } return(m); // Output of the method } ... (You can define more methods inside a class) } |
Explanation:
|
public class MyProgram { public static void main(String[] args) { double r; r = ToolBox.min( 1.0, 4.0 ); System.out.println(r); r = ToolBox.min( 3.7, -2.9 ); System.out.println(r); r = ToolBox.min( -9.9, 3.8 ); System.out.println(r); } } |
Notice the advantage of using methods:
|
How to run the program:
|
Output:
1.0 -2.9 -9.9 |
A note on the Java compiler:
|
|
public class ToolBox // Methods must be contained inside a class { /* ---------------------------------------- A method called "min" that contains the statements to find the smaller of 2 values a and b --------------------------------------- */ public static double min ( double a, double b ) { double m = 0; if ( a < b ) { m = a; // a is the smaller value } else { m = b; // b is the smaller value } return(m); // ****** A "return" statement ******* } ... (You can define more methods inside a class) } |
form 1: return ; form 2: return EXPRESSION ; |
|
|
Note: the construct must appear inside some class, so it will look like this:
Explanation:
|
|
public class ToolBox // contains 2 min methods { /* ---------------------------------------- min with 2 double formal parameters ---------------------------------------- */ public static double min ( double a, double b ) { double m = 0; System.out.println("You are using min1"); if ( a < b ) m = a; else m = b; return(m); } /* ---------------------------------------- min with 2 int formal parameters ---------------------------------------- */ public static int min ( int a, int b ) { int m = 0; System.out.println("*** You are using min2 !!!"); if ( a < b ) m = a; else m = b; return(m); } } |
Toolbox.min( 3.0, 1.0 ); // will invoke the first min method Toolbox.min( 3, 1 ); // will invoke the second min method |
How to run the program:
|
Output:
>>> java MyProgram You are using min1 1.0 *** You are using min2 !!! 1.0 |
|
You can do this:
ToolBox.min( 3.0, (double) 1 ); // will use min(double, double) ToolBox.min( (int) 3.0, 1 ); // will use min(int, int) |
public class MyProgram { public static double min ( double a, double b ) { double m = 0; if ( a < b ) { m = a; // a is the smaller value } else { m = b; // b is the smaller value } return(m); // **** Return statement **** } public static void main(String[] args) { double r; r = MyProgran.min( 1.0, 4.0 ); // The name is now: "MyProgram.min" ! System.out.println(r); r = MyProgram.min( 3.7, -2.9 ); System.out.println(r); r = MyProgram.min( -9.9, 3.8 ); System.out.println(r); } } |
Note:
|
How to run the program:
|
public class MyProgram { public static double min ( double a, double b ) { double m = 0; if ( a < b ) { m = a; // a is the smaller value } else { m = b; // b is the smaller value } return(m); // **** Return statement **** } public static void main(String[] args) { double r; r = MyProgram.min( 1.0, 4.0 ); // The name is now: "MyProgram.min" ! System.out.println(r); r = MyProgram.min( 3.7, -2.9 ); System.out.println(r); r = MyProgram.min( -9.9, 3.8 ); System.out.println(r); } } |
contains 2 methods
|
The method main invokes (calls) the method min.
|
Example:
public class MyProgram { public static double min ( double a, double b ) { double m = 0; if ( a < b ) { m = a; // a is the smaller value } else { m = b; // b is the smaller value } return(m); // **** Return statement **** } public static void main(String[] args) { double r; r = min( 1.0, 4.0 ); // ****** Shorthand name "min" used System.out.println(r); r = min( 3.7, -2.9 ); System.out.println(r); r = min( -9.9, 3.8 ); System.out.println(r); } } |
How to run the program:
|
|
But:
|
|
|
|