Best way to develop and maintain a large program is to construct it from small, simple pieces, or modules.
import java.util.Scanner; public class Choice { public static void main( String[] args ) { int selection = 0; Scanner input = new Scanner(System.in); menu(); // call a static method without an object. selection = input.nextInt(); System.out.printf("Your choice is %d\n", selection); } public static void menu() { System.out.println("Which of the following would you like to do?"); System.out.println("\t1. Do addition."); System.out.println("\t2. Do subtraction."); System.out.println("\t3. Do multiplication."); System.out.println("\t4. Do division."); System.out.println("\t5. quit"); System.out.print("Enter your selection ( 1 to 5 ): "); } }
import java.util.Scanner; public class Choice { public static void main( String[] args ) { int selection = 0; Scanner input = new Scanner(System.in); while(selection != 5) { menu(); // call a static method without an object. selection = input.nextInt(); System.out.printf("Your choice is %d\n", selection); } } public static void menu() { System.out.println("Which of the following would you like to do?"); System.out.println("\t1. Do addition."); System.out.println("\t2. Do subtraction."); System.out.println("\t3. Do multiplication."); System.out.println("\t4. Do division."); System.out.println("\t5. quit"); System.out.print("Enter your selection ( 1 to 5 ): "); } }
import java.util.Scanner; public class Choice { public static void main( String[] args ) { int selection = 0; Scanner input = new Scanner(System.in); while(selection != 5) { menu(); // call a static method without an object. selection = input.nextInt(); System.out.printf("Your choice is %d\n", selection); echo(selection); } } public static void menu() { System.out.println("Which of the following would you like to do?"); System.out.println("\t1. Do addition."); System.out.println("\t2. Do subtraction."); System.out.println("\t3. Do multiplication."); System.out.println("\t4. Do division."); System.out.println("\t5. quit"); System.out.print("Enter your selection ( 1 to 5 ): "); }
public static void echo(int i) { switch(i) { case 1: System.out.println("You would like to do addition."); break; case 2: System.out.println("You would like to do subtraction."); break; case 3: System.out.println("You would like to do multiplication."); break; case 4: System.out.println("You would like to do division."); break; case 5: System.out.println("Good Bye!"); break; default: System.out.println("Please select 1 to 5."); } } }
import java.util.Scanner; public class Choice { public static void main( String[] args ) { int selection = 0; Scanner input = new Scanner(System.in); while(selection != 5) { menu(); // call a static method without an object. selection = input.nextInt(); System.out.printf("Your choice is %d\n", selection); echo(selection); } } public static void menu() { System.out.println("Which of the following would you like to do?"); System.out.println("\t1. Do addition."); System.out.println("\t2. Do subtraction."); System.out.println("\t3. Do multiplication."); System.out.println("\t4. Do division."); System.out.println("\t5. quit"); System.out.print("Enter your selection ( 1 to 5 ): "); } public static void echo(int i) { if (i == 5) System.out.println("Good Bye!"); else { if(i < 5) { Couple obj = new Couple(); switch(i) { case 1: obj.add(); break; case 2: obj.subtract(); break; case 3: obj.multiply(); break; case 4: obj.divide(); break; } } else System.out.println("Please select 1 to 5."); } } } |
Create a class named
Couple with two instance
int variables, a, b. The constructor will take nothing
from caller, but take two numbers from user. There are four
methods, add(), subtract(), multiply(), and divide(). They
will perform the tasks as following:
For example, |
//There is another version for declaring an object in each case. public static void echo(int i) { if (i == 5) System.out.println("Good Bye!"); else { switch(i) { case 1: Couple obj = new Couple(); obj.add(); break; case 2: Couple obj1 = new Couple(); obj1.subtract(); break; case 3: Couple obj2 = new Couple(); obj2.multiply(); break; case 4: Couple obj3 = new Couple(); obj3.divide(); break; default: System.out.println("Please select 1 to 5."); } } }
|