JAVA PROGRAM - no functions/methods Display the menu shown below and get the use
ID: 3725635 • Letter: J
Question
JAVA PROGRAM - no functions/methods
Display the menu shown below and get the user’s choice.
1. Addition
2. Subtraction (first operand larger)
3. Subtraction (first operand smaller)
4. Multiplication
5. Exit the program
If the user does not enter a valid choice, output "Valid choices are 1-5; please re-enter"
Get the lower and upper bound values the user wants to use for the math problems. Any integer values can be used. Upper bound must be greater than the lower bound. If the value entered for the upper bound is not greater than the lower bound, output an "upper bound must be > lower bound" and prompt for another value to be entered.
Display 5 problems of the user-requested type. For instance, if the user chooses option 1, your program should display an addition problem, using randomly-generated operands between the lower bound and the upper bound. Then it should get the user’s answer, check the answer and output message based on whether or not it is correct(if the answer was incorrect, the message should include the correct answer). Display problem/get answer/check answer steps should repeat a total of five times.
Repeat steps 1, 2, and 3 until the user chooses the exit option.
Keep track of the number of problems attempted and the number of problems correct. When the user exits,output the number of problems correct, the number of problems attempted, and the percentage of problems correct to 2 decimal places.
Use a while loop to repeat the menu display/get user choice action.
Use while loop for validation of the upper bound.
Use for loop to repeat the display problem/get answer/check answer steps
Do not use functions.
For random numbers, use
(int)(lowerBound + Math.random() * (upperBound – lowerBound + 1))
Explanation / Answer
pArithmeticOperations.java
import java.util.Scanner;
public class ArithmeticOperations {
public static void main(String[] args) {
//Declaring variables
int a,b,res,user,choice,qCount=0,cCount=0;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
/* This while loop continues to execute
* until the user enters a valid number
*/
while(true)
{
//displaying the menu
System.out.println(" ::Menu::");
System.out.println("1. Addition");
System.out.println("2. Subtraction (first operand larger)");
System.out.println("3. Subtraction (first operand smaller)");
System.out.println("4. Multiplication");
System.out.println("5. Exit the program");
//getting the choice entered by the user
System.out.print("Enter Choice :");
choice=sc.nextInt();
//Based on the user choice the corresponding case will be executed
switch(choice)
{
case 1:{
qCount++;
a=(int)(1 + Math.random() * (100));
b=(int)(1 + Math.random() * (100));
System.out.print(a+" + "+b+" = ");
user=sc.nextInt();
res=a+b;
if(res==user)
{
System.out.println("CORRECT!");
cCount++;
}
else
{
System.out.println("WRONG!");
System.out.println("Answer is :"+res);
}
continue;
}
case 2:{
qCount++;
a=(int)(1 + Math.random() * (100));
while(true)
{
b=(int)(1 + Math.random() * (100));
if(b>=a)
continue;
else
break;
}
System.out.print(a+" - "+b+" = ");
user=sc.nextInt();
res=a-b;
if(res==user)
{
System.out.println("CORRECT!");
cCount++;
}
else
{
System.out.println("WRONG!");
System.out.println("Answer is :"+res);
}
continue;
}
case 3:{
qCount++;
a=(int)(1 + Math.random() * (100));
while(true)
{
b=(int)(1 + Math.random() * (100));
if(b<=a)
continue;
else
break;
}
System.out.print(a+" - "+b+" = ");
user=sc.nextInt();
res=a-b;
if(res==user)
{
System.out.println("CORRECT!");
cCount++;
}
else
{
System.out.println("WRONG!");
System.out.println("Answer is :"+res);
}
continue;
}
case 4:{
qCount++;
a=(int)(1 + Math.random() * (100));
b=(int)(1 + Math.random() * (100));
System.out.print(a+" * "+b+" = ");
user=sc.nextInt();
res=a*b;
if(res==user)
{
System.out.println("CORRECT!");
cCount++;
}
else
{
System.out.println("WRONG!");
System.out.println("Answer is :"+res);
}
continue;
}
case 5:{
break;
}
default:{
System.out.println("** Invalid.Must be between 1-5 **");
continue;
}
}
break;
}
//Displaying total no of questions generated
System.out.println("Total No of Questions :"+qCount);
//Displaying total no of questions correctly answered
System.out.println("No of Correct Answers :"+cCount);
//Displaying the Correct Answers Percentage
System.out.printf("Percentage of Correct Answers :%.2f%%",((double)cCount/qCount)*100);
}
}
_______________________
Output:
::Menu::
1. Addition
2. Subtraction (first operand larger)
3. Subtraction (first operand smaller)
4. Multiplication
5. Exit the program
Enter Choice :1
43 + 36 = 79
CORRECT!
::Menu::
1. Addition
2. Subtraction (first operand larger)
3. Subtraction (first operand smaller)
4. Multiplication
5. Exit the program
Enter Choice :2
49 - 39 = 10
CORRECT!
::Menu::
1. Addition
2. Subtraction (first operand larger)
3. Subtraction (first operand smaller)
4. Multiplication
5. Exit the program
Enter Choice :3
2 - 49 = -47
CORRECT!
::Menu::
1. Addition
2. Subtraction (first operand larger)
3. Subtraction (first operand smaller)
4. Multiplication
5. Exit the program
Enter Choice :4
10 * 7 = 70
CORRECT!
::Menu::
1. Addition
2. Subtraction (first operand larger)
3. Subtraction (first operand smaller)
4. Multiplication
5. Exit the program
Enter Choice :5
Total No of Questions :4
No of Correct Answers :4
Percentage of Correct Answers :100.00%
____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.