In: Computer Science
//Java code
public class MathOperation { //A int intNum1; int intNum2; float fNum1; float fNum2; int op; //B public MathOperation() { intNum1 = 0; intNum2 = 0; fNum1 = 0; fNum2 = 0; op = 0; } //C public MathOperation(int n1, int n2, int n) { intNum1 = n1; intNum2 = n2; fNum1 = 0; fNum2 = 0; op = n; } //D public MathOperation(float d1, float d2, int n) { fNum1 = d1; fNum2 = d2; intNum1 = 0; intNum2 = 0; op = n; } //E public String toString() { String str = "CALCULATOR OF LIEM LE"; String str2 = "", str3 = ""; int iRet= 0; float fRet = 0.0f; switch (op) { case 1: // add 2 int str2 = "ADD TWO INTEGERS"; iRet = intNum1 + intNum2; str3 = intNum1 + " + " + intNum2 + " = " + iRet; break; case 2: // minus 2 int str2 = "MINUS TWO INTEGERS"; iRet = intNum1 - intNum2; str3 = intNum1 + " - " + intNum2 + " = " + iRet; break; case 3: // multiply 2 int str2 = "MULTIPLY TWO INTEGERS"; iRet = intNum1 * intNum2; str3 = intNum1 + " * " + intNum2 + " = " + iRet; break; case 4: // divide 2 int str2 = "DIVIDE TWO INTEGERS"; iRet = intNum1 /intNum2; str3 = intNum1 + " / " + intNum2 + " = " + iRet; break; case 5: // add 2 float str2 = "ADD TWO DECIMAL NUMBERS"; fRet = fNum1 + fNum2; str3 = fNum1 + " + " + fNum2 + " = " + fRet; break; case 6: // minus 2 floaf str2 = "MINUS TWO DECIMAL NUMBERS"; fRet = fNum1 - fNum2; str3 = fNum1 + " - " + fNum2 + " = " + fRet; break; case 7: // multiply float str2 = "MULTIPLY TWO DECIMAL NUMBERS"; fRet = fNum1 * fNum2; str3 = fNum1 + " * " + fNum2 + " = " + fRet; break; case 8: // divide float str2 = "DIVIDE TWO DECIMAL NUMBERS"; fRet = fNum1 / fNum2; str3 = fNum1 + " / " + fNum2 + " = " + fRet; break; } return str + "\n" + str2 + "\n" + str3 + "\n"; } }
//================================================
import java.util.Scanner; public class Main { static Scanner input = new Scanner(System.in); public static void main(String[] args) { int choice=-1; while (true) { displayMenu(); choice = input.nextInt(); switch (choice) { case 1: calculate(choice); break; case 2: calculate(choice); break; case 3: calculate(choice); break; case 4: calculate(choice); break; case 5: calculate(choice); break; case 6: calculate(choice); break; case 7: calculate(choice); break; case 8: calculate(choice); break; case 0: break; default: System.err.println("Wrong Choice... try again.."); break; } if(choice==0) break; } } /** * Calculate the result and display * @param choice */ public static void calculate(int choice) { int num1,num2; float numf1,numf2; //Object of MAthOperation MathOperation mathOperation= null; //ask for int for choice 1-4 if(choice>=1 && choice<5) { System.out.println("Enter first number: "); num1 = input.nextInt(); System.out.println("Enter second number: "); num2 = input.nextInt(); mathOperation = new MathOperation(num1,num2,choice); System.out.println(mathOperation); } //ask for float numbers from 5-8 else { numf1 = input.nextFloat(); System.out.println("Enter second number: "); numf2 = input.nextFloat(); mathOperation = new MathOperation(numf1,numf2,choice); System.out.println(mathOperation); } } //Finction to display menu public static void displayMenu() { System.out.println("1. Add two integers\n" + "2. Subtract two integers\n" + "3. Multiply two integers\n" + "4. Divide two integers\n" + "5. Add two decimal numbers\n" + "6. Subtract two decimal numbers\n" + "7. Multiply two decimal numbers\n" + "8. Divide two decimal numbers\n" + "0. Exit\nEnter choice: "); } }
//UML Class Diagram
+ sign represents public
- sign represents private
underline method represents static
//Pseudocode
(1) Start the Main class and write main() method
(2) Create a static method displayMenu() which returns void and takes 0 arguments and print the menu
(3) Create a static method calculate(), which returns void and takes one argument of int type.
(4) in calculate(int choice),
(1) create an object of MathOperation() class and initialize it to null and write if-else statement
(2) if( choice >=1 && choice<5) then prompt for int numbers and call the constructor and pass two int numbers and choice
(3) else prompt for float numbers and call the constructor and pass two float numbers and choice
(5) create a infinite while loop in main()
while(true):
call the displayMenu()
and get the choice from user
swich(choice):
//create cases according to the menu followed by break statement
if(choice==0)
break the loop and exit program.
(6) End of main()
//output
// If you need any help regarding this solution .......... please leave a comment ..... thanks