import java.util.Scanner;
class ArithmeticFunctions {
public double Add(double number1, double number2) {
return number1 + number2;
}
public double Subtract(double number1, double number2) {
return number1 - number2;
}
public double Multiply(double number1, double number2) {
return number1 * number2;
}
public double Divide(double number1, double number2) {
return number1 / number2;
}
public int Modulus(double number1, double number2) {
return (int) (number1 % number2);
}
}
public class Q191285 {
/**
* *
* Main method
*
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String ch = "";
ArithmeticFunctions arithmeticOperations = new ArithmeticFunctions();
while (ch.compareTo("6") != 0) {
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Modulus");
System.out.println("6. Exit");
System.out.print("Select the operation: ");
ch = input.nextLine();
double number1 = 0;
double number2 = 0;
if (ch.compareTo("1") == 0
|| ch.compareTo("2") == 0
|| ch.compareTo("3") == 0
|| ch.compareTo("4") == 0
|| ch.compareTo("5") == 0) {
System.out.print("Enter the first number: ");
number1 = input.nextDouble();
System.out.print("Enter the second number: ");
number2 = input.nextDouble();
}
if (ch.compareTo("1") == 0) {
System.out.println(number1 + " + " + number2 + " = " + arithmeticOperations.Add(number1, number2));
} else if (ch.compareTo("2") == 0) {
System.out.println(number1 + " - " + number2 + " = " + arithmeticOperations.Subtract(number1, number2));
} else if (ch.compareTo("3") == 0) {
System.out.println(number1 + " * " + number2 + " = " + arithmeticOperations.Multiply(number1, number2));
} else if (ch.compareTo("4") == 0) {
System.out.println(number1 + " / " + number2 + " = " + arithmeticOperations.Divide(number1, number2));
} else if (ch.compareTo("5") == 0) {
System.out.println(number1 + " % " + number2 + " = " + arithmeticOperations.Modulus(number1, number2));
} else if (ch.compareTo("6") == 0) {
} else {
System.out.println("\nSelect correct menu item.\n");
}
input.nextLine();
}
input.close();
}
}
Example:
Comments
Leave a comment