Answer to Question #191285 in Java | JSP | JSF for Aadee

Question #191285
  1. Write the Arithmetic Functions program with the following instructions:                      [2 x 5 = 10]
  2. Create class 1 containing the methods Add, Subtract, Multiply, Divide and Modulus
  3. Class 1 should not contain main method
  4. All the methods written in class 1 should be given the Values as Parameters (at least 2 integers)
  5. Create class 2 with the main method only
  6. Create the Object of class 1 and call all the methods of class 1 using the object.
1
Expert's answer
2021-05-10T13:41:38-0400
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:


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS