how to create a java class for arithmetic operations that includes all of? a) A
ID: 3647329 • Letter: H
Question
how to create a java class for arithmetic operations that includes all of?a) A constructor
b) Declare the appropriate variables
b) A method that prompts for two integers
c) A method for adding the two numbers
d) A method for subtracting the two numbers
e) A method for multiplying the two numbers
f) A method for diving the two numbers
g) A method for using modulo with the two numbers
h. Inside of a looping structure
i) Prompt the user for a pair of integers
j) Display a menu that lists the various arithmetic operations with a referencing number
k) Use a switch structure to process the various arithmetic operations, including one for incorrect entries [An example of a working switch structure is at the end of the assignment. Avoid redundant code.]
L) After the switch structure, display a text that includes the two integers, the type of operation performed, and the result of the operation
m) Prompt the user to continue using the same integers or a new set of integers
n) Prompt the user to end the loop with an appropriate sentinel
o. When the user ends the program include a final line of text:
Explanation / Answer
import java.util.Scanner; public class arithmetic { private int num1; private int num2; Scanner s = null; public static void main(String[] args) { int res,exit = 1; arithmetic ob = new arithmetic(); do{ ob.promt(); System.out.println("Select a operation to perform on the two numbers:"); System.out.println("1. Add"); System.out.println("2. subtract"); System.out.println("3. multiply"); System.out.println("4. divide"); System.out.println("5. modulo"); System.out.println("6. exit"); res = ob.s.nextInt(); switch(res){ case 1: ob.add(); break; case 2: ob.subtract(); break; case 3: ob.multiply(); break; case 4: ob.divide(); break; case 5: ob.modulo(); break; case 6: System.out.println("Thanks for using my program. [Your name]"); exit = 0; } }while(exit == 1); } public arithmetic(){ s = new Scanner(System.in); } public void promt(){ System.out.print("Enter First num"); num1 = s.nextInt(); System.out.print("Enter Second num"); num2 = s.nextInt(); } public int add(){ return num1 + num2; } public int subtract(){ return num1 - num2; } public int multiply(){ return num1 * num2; } public int divide(){ return num1 / num2; } public int modulo(){ return num1 % num2; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.