Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help creating this JAVA code. Create a program that randomly generates tw

ID: 3910403 • Letter: I

Question

I need help creating this JAVA code.

Create a program that randomly generates two numbers, then depending on a choice will Add, Subtract, Multiply or Divide the two numbers. The program should repeat the entire process until the user decides to quit. At the end display how many times the program repeated and how many correct answers were provided. How to prevent division by zero generate two numbers if the second number is zero you can add 1 to it for this exercise. NOTE: you can divide into 0, but not by 0, so we are not worried about num1 being a zero.. How to prevent negative subtraction results; IF the second number is larger than the first number: int temp = num1; num1 = num2; num2 = temp; How to only get whole numbers from division: do Not show problem until you do the next line int newDividend = num1 * num2; System.out.print(newDividend + " / " + num1 + " = " ) So of course the answer is now equal to num2...

Explanation / Answer

code:

import java.util.Random;
import java.util.Scanner;

class arithmeticoprn {
//member function for adding two numbers

public static void add(int a, int b) {
System.out.print(" " + a + " + " + b + " = " + (a + b));
}
//member function for subtracting two numbers

public static void subtract(int a, int b) {
if (a > b) {
System.out.print(" " + a + " - " + b + " = " + (a - b));
} else {
System.out.print(" " + b + " - " + a + " = " + (b - a));
}
}
//member function for multiplying two numbers

public static void multiply(int a, int b) {
System.out.print(" " + a + " * " + b + " = " + (a * b));
}
//member function for dividing two numbers

public static void divide(int a, int b) {
if (b == 0) {
b = 1;
}
int newDividend = a / b;
if (newDividend > 0) {
System.out.print(" " + a + " / " + b + " = " + (a / b));
} else {
newDividend = a * b;
System.out.print(" " + newDividend + " / " + a + " = " + (newDividend / a));
}
}

public static void main(String arg[]) {
Random rand = new Random();
Scanner input = new Scanner(System.in);
int ch, count = 0;
do {
//generate the two random numbers
int num1 = rand.nextInt(50) + 1;

int num2 = rand.nextInt(50) + 1;
//displays the choice
System.out.print(" Select your choice: 1.Addition 2.subtraction 3..Multiplication 4.Division 5.Exit");
System.out.print(" Enter the choice :");
ch = input.nextInt();//accepts the choice
//calls the member function based on the choice
switch (ch) {
case 1:
add(num1, num2);
count++;
break;
case 2:
subtract(num1, num2);
count++;
break;
case 3:
multiply(num1, num2);
count++;
break;
case 4:
divide(num1, num2);
count++;
break;
case 5:
System.out.println(" operation has been done " + count + " times");
System.out.println(" Have a great day...bye");
break;
default:
System.out.println("plz Enter the choice between(1-5) ");
}

} while (ch != 5); //performs until quit.

}

}

output:

run:

Select your choice:

1.Addition

2.subtraction

3..Multiplication

4.Division

5.Exit

Enter the choice :1

15 + 10 = 25

Select your choice:

1.Addition

2.subtraction

3..Multiplication

4.Division

5.Exit

Enter the choice :3

43 * 7 = 301

Select your choice:

1.Addition

2.subtraction

3..Multiplication

4.Division

5.Exit

Enter the choice :4

1472 / 32 = 46

Select your choice:

1.Addition

2.subtraction

3..Multiplication

4.Division

5.Exit

Enter the choice :5

operation has been done 3 times

Have a great day...bye

BUILD SUCCESSFUL (total time: 33 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote