This week we looked at an example math program that would display the answer to
ID: 3594676 • Letter: T
Question
This week we looked at an example math program that would display the answer to a random math problem. Enhance this assignment to prompt the user to enter the solution to the displayed problem. After the user has entered their answer, the program should display a message indicating if the answer is correct or incorrect. If the answer is incorrect, it should display the correct answer. The division section should use Real data types instead of Integer data types. Keep track of how many correct/incorrect answers the user makes during the program and when they choose to exit, the program should display the percentage they got correct (# of correct / total # attempted).
HINT:
Keep in mind you will need to add 1 or more additional parameters to the addition, subtraction, multiplication, and division methods to support the ability to track the total attempted and total correct. Also, be aware with the way Real numbers are stored in memory, it will be nearly impossible for you to get a division problem correct. You don't need to worry about adjusting for this in your program, I just want to make you aware of this in case you run into this issue while testing your program. Let me know if you have questions or would like to discuss this further.
Example Output:
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Exit
Enter your selection:
1[Enter]
12 + 34 =
45 [Enter]
I’m sorry, the correct answer was 46
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Exit
Enter your selection:
2[Enter]
45 – 2 =
43[Enter]
Yes, that is the correct answer!
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Exit
Enter your selection:
5 [Enter]
Thanks for playing. You answered 1 out of 2 questions correct for a score of 50%
IMPORTANT:
PROGRAM MUST BE RAPTOR IN INTERMEDIATE MODE.
Explanation / Answer
import java.util.*;
class mathsproblem
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
int total=0; // variable to store total attempted answers
int correct=0; // variable to store correct answers
int i=0;
while(i!=1) // Looping till exit is clicked
{
System.out.println("1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit");
n=sc.nextInt();
switch(n)
{
case 1:
correct=correct+Addition();
total=total+1; //Increasing total number of attempted answers by one
break;
case 2:
correct=correct+Subtraction();
total=total+1;
break;
case 3:
correct=correct+Multiplication();
total=total+1;
break;
case 4:
correct=correct+Division();
total=total+1;
break;
case 5: End(total,correct);
i=1;
break;
}
}
}
public static int Addition()
{
Random rnum = new Random();
int a,b,c;
a=rnum.nextInt(200);
b=rnum.nextInt(200);
System.out.println(a+ " + " +b+" = ");
Scanner sc=new Scanner(System.in);
c=sc.nextInt();
if(c==(a+b))
{
System.out.println("Yes, that is the correct answer!");
return(1); //returnig 1 if answer is corrected which increases correct count.
}
else
{
System.out.println("I’m sorry, the correct answer was "+ (a+b));
return(0); //returnig 0 if answer is incorrected which keeps correct count same.
}
}
public static int Subtraction()
{
Random rnum = new Random();
int a,b,c;
a=rnum.nextInt(200);
b=rnum.nextInt(200);
System.out.println(a+ " - " +b+" = ");
Scanner sc=new Scanner(System.in);
c=sc.nextInt();
if(c==(a-b))
{
System.out.println("Yes, that is the correct answer!");
return(1);
}
else
{
System.out.println("I’m sorry, the correct answer was "+ (a-b));
return(0);
}
}
public static int Multiplication()
{
Random rnum = new Random();
int a,b,c;
a=rnum.nextInt(100);
b=rnum.nextInt(100);
System.out.println(a+ " * " +b+" = ");
Scanner sc=new Scanner(System.in);
c=sc.nextInt();
if(c==(a*b))
{
System.out.println("Yes, that is the correct answer!");
return(1);
}
else
{
System.out.println("I’m sorry, the correct answer was "+ (a*b));
return(0);
}
}
public static int Division()
{
Random rnum = new Random(); //generating random valus for each question
double c;
int a,b;
a=rnum.nextInt(200);
b=rnum.nextInt(200);
System.out.println(a+ " / " +b+" = ");
Scanner sc=new Scanner(System.in);
c=sc.nextDouble();
double roundOff = Math.round(((double)a/b) * 100.0) / 100.0; // rounding of output to 2 decimal points
if(c==roundOff)
{
System.out.println("Yes, that is the correct answer!");
return(1);
}
else
{
System.out.println("I’m sorry, the correct answer was "+ roundOff);
return(0);
}
}
public static void End(int total,int correct)
{
if(total==0 && correct ==0)
System.out.print("Thanks for playing.");
else
System.out.println("Thanks for playing. You answered "+correct+" out of "+total+" questions correct for a score of "+(correct/total)*100+"%");
}
}
This is a java solution if solution required in any other format please request.....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.