Change the program menuExample.java to randomnly generate problems for the user
ID: 3625463 • Letter: C
Question
Change the program menuExample.java to randomnly generate problems forthe user to answer. Have the user answer the problem and let them know if they
answered correctly or incorrectly. Do this for each menu option
(addition, subtraction, multiplication, division, modulus).
Only generate random numbers in the range 1-15.
import java.util.*; //used to inlcude scanner
public class menuExample
{
public static void main(String args[])
{
//declare a scanner for user Input
Scanner userInput = new Scanner(System.in);
int choice;
int value1, value2;
double number; //hold the result of integer division
do
{
//display our menu
System.out.println("***Calculator v1.0***");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Modulus");
System.out.println("6. Exit");
System.out.println("*********************");
System.out.println("Please enter your choice:");
//get user input
choice = userInput.nextInt();
//switch the choice from user
switch(choice)
{
case 1://addition
System.out.println("addition");
System.out.println("Please enter two values to be added:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " + " + value2 + " = " + (value1+value2));
break;
case 2://subtraction
System.out.println("subtraction");
System.out.println("Please enter two values to be subtracted:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " - " + value2 + " = " + (value1-value2));
break;
case 3://multiplication
System.out.println("multiplication");
System.out.println("Please enter two values to be multiplied:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " * " + value2 + " = " + (value1*value2));
break;
case 4://division
System.out.println("division");
System.out.println("Please enter two values to be divided:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
number = (double)value1 / value2; //type cast to avoid integer division
System.out.println(value1 + " / " + value2 + " = " + number);
break;
case 5: //modulus
System.out.println("multiplication");
System.out.println("Please enter two values to be divided:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " % " + value2 + " = " + (value1%value2));
break;
case 6://exit
System.out.println("You have chose exit!");
break;
default://default
System.out.println("You entered an invalid choice");
}
}while(choice != 6);
}//main
}//class
Explanation / Answer
import java.util.*; //used to inlcude scanner
public class menuExample
{
public static void main(String args[])
{
//declare a scanner for user Input
Scanner userInput = new Scanner(System.in);
int choice,correct=0,incorrect=0,total=0;
int value1, value2,addcount=0,subcount=0,mulcount=0,divcount=0,modcount=0;
double number; //hold the result of integer division
do
{
//display our menu
System.out.println("***Calculator v1.0***");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Modulus");
System.out.println("6. Exit");
System.out.println("*********************");
System.out.println("Please enter your choice:");
//get user input
choice = userInput.nextInt();
total++;
//switch the choice from user
switch(choice)
{
case 1://addition
System.out.println("addition");
System.out.println("Please enter two values to be added:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " + " + value2 + " = " + (value1+value2));
correct++;
addcount++;
break;
case 2://subtraction
System.out.println("subtraction");
System.out.println("Please enter two values to be subtracted:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " - " + value2 + " = " + (value1-value2));
correct++;
subcount++;
break;
case 3://multiplication
System.out.println("multiplication");
System.out.println("Please enter two values to be multiplied:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " * " + value2 + " = " + (value1*value2));
correct++;
mulcount++;
break;
case 4://division
System.out.println("division");
System.out.println("Please enter two values to be divided:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
number = (double)value1 / value2; //type cast to avoid integer division
System.out.println(value1 + " / " + value2 + " = " + number);
correct++;
divcount++;
break;
case 5: //modulus
System.out.println("multiplication");
System.out.println("Please enter two values to be divided:");
value1 = userInput.nextInt();
value2 = userInput.nextInt();
System.out.println(value1 + " % " + value2 + " = " + (value1%value2));
correct++;
modcount++;
break;
case 6://exit
System.out.println("You have chose exit!");
break;
default://default
incorrect++;
System.out.println("You entered an invalid choice");
}
}while(choice != 6);
System.out.println(" Total number of problems attempted:"+total);
System.out.println("Total addition problem attempts:"+addcount);
System.out.println("Total subtraction problem attempts:"+subcount);
System.out.println("Total multiplication problem attempts:"+mulcount);
System.out.println("Total division problem attempts:"+divcount);
System.out.println("Total mod problem attempts:"+modcount);
System.out.println(" Total number of problems attempted:"+incorrect);
System.out.println(" Percentage of correct:"+(correct/total)*100);
}//main
}//class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.