This week we looked at an example math program that would display the answer to
ID: 3595733 • 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
Implemented code as per the requirement but there is no specific programming language mentioned, so I have developed the code in Java. Please leave a comment if you want it to be done any other language. I'm proficient in C/C++/C#/Java/Python.
Code:
--------------
import java.util.Random;
import java.util.Scanner;
public class Calculator {
private static int tot_q=0, corr_q=0;
public static void main(String[] args) {
Random r = new Random();
int Low = 1;
int High = 100;
int i1 = 0, i2=0, res;
Scanner sc = new Scanner(System.in);
int sel = 0;
while (true) {
i1 = r.nextInt(High-Low) + Low;
i2 = r.nextInt(High-Low) + Low;
System.out.print("1) Addition " +
"2) Subtraction " +
"3) Multiplication " +
"4) Division " +
"5) Exit Enter your selection: ");
sel = sc.nextInt();
if(sel==1) {
res = i1+i2;
System.out.print(i1+" + "+i2+" = ");
sel = sc.nextInt();
if(res == sel) {
tot_q++;
corr_q++;
System.out.println("Yes, that is the correct answer!");
}
else {
System.out.println("I’m sorry, the correct answer was "+res);
}
}
else if(sel==2) {
res = i1-i2;
System.out.print(i1+" - "+i2+" = ");
sel = sc.nextInt();
if(res == sel) {
tot_q++;
corr_q++;
System.out.println("Yes, that is the correct answer!");
}
else {
System.out.println("I’m sorry, the correct answer was "+res);
}
}
else if(sel==3) {
res = i1*i2;
System.out.print(i1+" * "+i2+" = ");
sel = sc.nextInt();
if(res == sel) {
tot_q++;
corr_q++;
System.out.println("Yes, that is the correct answer!");
}
else {
System.out.println("I’m sorry, the correct answer was "+res);
}
}
else if(sel==4) {
float ans = i1/i2;
System.out.print(i1+" / "+i2+" = ");
sel = sc.nextInt();
if(ans == sel) {
tot_q++;
corr_q++;
System.out.println("Yes, that is the correct answer!");
}
else {
System.out.println("I’m sorry, the correct answer was "+ans);
}
}
else if(sel==5) {
if(tot_q==0) {
System.out.println("Thanks for playing. You answered "+ corr_q+" out of "+ tot_q+" questions correct for a score of "+0+"%");
System.exit(0);
}
else {
System.out.println("Thanks for playing. You answered "+ corr_q+" out of "+ tot_q+" questions correct for a score of "+((corr_q/tot_q)*100) +"%");
System.exit(0);
}
}
else {
System.out.println("Please enter valid input!!!");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.