Hello, Please help write MathGame program in JAVA that generates random math pro
ID: 3816620 • Letter: H
Question
Hello,
Please help write MathGame program in JAVA that generates random math problems for students to answer. Players get a small reward for correct answers and suffer a small penalty for incorrect ones. User statistics need to be recorded in a text file so that they me loaded back into the program when the player returns to play the game again. In addition, the youngster should be allowed to see how (s)he is doing at any time while (s)he is playing the game.
Please follow all requirement, since this work is Midterm
PROGRAM REQUIREMENTS:
1) Generate simple math fact problems:
- Addition (the total must be an integer >= 0)
- Subtraction (the difference must be an integer >= 0)
- Multiplication (the product must be an integer >= 0)
- Division (the quotient must be an integer >= 0)
2) Validate user input at every opportunity. If your program crashes because you allow the user to make an invalid entry and you did not write code to handle such potential exceptions, you will NOT pass the midterm!
3) The program should keep track of the following statistics:
- The user’s name
- The total correct answers
- The total wrong answers
- The total earnings ($0.05 is awarded for every correct response and $0.03 is subtracted from every incorrect response)
4) A separate text file must be created for every user:
- Statistics are read from the file at the start of the game (if the user is a returning player).
- Statistics are recorded at the end of every game.
5) The program must be developed using functions so that the main() function consists mostly of function calls. Below is a list of most of the required functions, you may add your own functions if you like:
- credits //This function is used to display your name and what the program does
- menu // This function is used to display the menu with various options
- validateUserResponse // This function is used to validate user input from the menu
- validateUserAnswer // This function is used to validate user input and ensure that ONLY numeric answers are entered by the user.
- checkUserAnswer // given a math problem, this function is used to check if the answer the user entered is correct or incorrect
- updateStats // This function is used to keep a running total of game statistics (in RAM)
- displayStats // This function is used to display statistics on screen
- retireveStats // This function is used to retrieve player statistics from external txt file when the game starts, assuming the player is a returning player, else create a text file to store the stats.
- saveStats // This function is used to save player statistics on an external txt file.
- You may also want to consider the following four functions: generateAddition, generateSubtraction, generateMultiplication and generateDivision // use these to generate a problem of the appropriate type.
6) You must use meaningful variable names.
7) You must comment your code.
8) You must use variables of the correct type and initialize them with a proper value.
9) Code must be commented
PROGRAM FLOW WITH SCREENSHOTS:
1) At the start of the program, an initial “Splash” screen must be displayed which includes:
a. The game’s title
b. Your name
2) After the "Splash" screen a prompt must ask the user for his/her name (validate input: no numbers, no blanks).
3) Once you have the user's name, display a menu with the options listed in the graphic below (validate inout: only options 1, 2, 3, 4, 5, n and Nmust be allowed):
4) If the user chooses option 1 or 2 or 3 or 4 a randomly generated problem is displayed ( validate input: only positive integers must be allowed):
5) If the user's answer is correct, a screen is displayed with proper feedback:
6) STATS are displayed if the user chooses option 5, see graphic below (display earnings with two decimal places):
******TheMathGame****** ****** By Prof. ****** ****** Flores ******* y/Y to continue, any other char to exitExplanation / Answer
Hi ,
Please se below the classes. Please comment for anyq ueries/feedbacks.
Thanks,
MathGame.java
import java.util.Random;
import java.util.Scanner;
public class MathGame {
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
String continueVal ="";
String name ="";
boolean menuContinue =true;
boolean validName =false;
String choice ="";
String question ="";
String questionHeading ="";
String answer ="";
boolean validAnswer = false;
int answerInt =0;
boolean correctAnswer = false;
int correctAns =0;
double earning =0.0;
int totalCorrect=0;
int totalInCorrect=0;
boolean action =true;
System.out.println("************************");
System.out.println("************************");
System.out.println("************************");
System.out.println("****** ******");
System.out.println("****** TheMathGame******");
System.out.println("****** By Prof. ******");
System.out.println("****** Flores ******");
System.out.println("****** ******");
System.out.println("************************");
System.out.println("************************");
System.out.println("************************");
System.out.println("y/Y to continue, any other char to exit");
continueVal = scan.nextLine();
if("Y".equalsIgnoreCase(continueVal)){
System.out.println("Enter your name and press <ENTER>");
name = scan.nextLine();
while(!validName){
if(name.contains(" ") || name.contains("0") || name.contains("1") ||name.contains("2")
|| name.contains("3") || name.contains("4") || name.contains("5")
|| name.contains("6") || name.contains("6") || name.contains("8")
|| name.contains("9") ){
System.out.println("Invalid Name! Please enter a valid name.");
name = scan.nextLine();
}
else{
validName = true;
}
}
do{
System.out.println("******CHOOSE A PROBLEM******");
System.out.println("****************************");
System.out.println("****************************");
System.out.println("****** ******");
System.out.println("****** 1.ADD ******");
System.out.println("****** 2.SUBTRACT ******");
System.out.println("****** 3.MULTIPLY ******");
System.out.println("****** 4.DIVIDE ******");
System.out.println("****** 5.STATS ******");
System.out.println("****** n/N to QUIT ******");
System.out.println("****** ******");
System.out.println("****************************");
System.out.println("****************************");
choice = scan.nextLine();
Random rand = new Random();
int n1 = rand.nextInt(50) + 1;
int n2 = rand.nextInt(50) + 1;
if("1".equalsIgnoreCase(choice)){
questionHeading =" ADDITION ";
question =n1+" + "+n2+"=?"+" ";
correctAns = n1+n2;
}
else if ("2".equalsIgnoreCase(choice)){
questionHeading =" SUBTRACTION ";
question =+n1+" - "+n2+"=?"+" ";;
correctAns = n1 - n2;
}
else if ("3".equalsIgnoreCase(choice)){
questionHeading ="MULTIPLICATION";
question =n1+" * "+n2+"=?"+" ";;
correctAns = n1 * n2;
}
else if ("4".equalsIgnoreCase(choice)){
questionHeading =" DIVISION ";
question =n1+" / "+n2+"=?"+" ";;
correctAns = n1 / n2;
}
else if ("5".equalsIgnoreCase(choice)){
action = false;
System.out.println("****************************");
System.out.println("****************************");
System.out.println("****** ******");
System.out.println("********* "+name+" ***********");
System.out.println("****** Total earnings $"+earning + " ***");
System.out.println("****** Total correct $"+totalCorrect + " ***");
System.out.println("****** Total Incorrect $"+totalInCorrect + " ***");
System.out.println("****** ******");
System.out.println("****************************");
System.out.println("****************************");
}
if("N".equalsIgnoreCase(choice)){
System.out.println("*******THANK YOU!******");
System.exit(0);
}
if(action){
System.out.println("****** "+questionHeading+" ******");
System.out.println("****************************");
System.out.println("****************************");
System.out.println("****** "+question+" ******");
System.out.println("****************************");
System.out.println("****************************");
answer = scan.nextLine();
answerInt = Integer.valueOf(answer);
while(!validAnswer){
if(answerInt < 0){
System.out.println("Invalid Answer! Please enter a valid answer.");
answer = scan.nextLine();
answerInt = Integer.valueOf(answer);
}
else{
validAnswer = true;
}
}
if( correctAns == answerInt){
earning = earning +.5;
totalCorrect++;
System.out.println("***********RIGHT!***********");
}
else{
System.out.println("***********WRONG!***********");
totalInCorrect++;
earning = earning -.2;
}
}
}while(menuContinue);
}
else{
System.exit(0);
}
}
}
Sample output
************************
************************
************************
****** ******
****** TheMathGame******
****** By Prof. ******
****** Flores ******
****** ******
************************
************************
************************
y/Y to continue, any other char to exit
Y
Enter your name and press <ENTER>
ALLEN
******CHOOSE A PROBLEM******
****************************
****************************
****** ******
****** 1.ADD ******
****** 2.SUBTRACT ******
****** 3.MULTIPLY ******
****** 4.DIVIDE ******
****** 5.STATS ******
****** n/N to QUIT ******
****** ******
****************************
****************************
1
****** ADDITION ******
****************************
****************************
****** 4 + 27=? ******
****************************
****************************
31
***********RIGHT!***********
******CHOOSE A PROBLEM******
****************************
****************************
****** ******
****** 1.ADD ******
****** 2.SUBTRACT ******
****** 3.MULTIPLY ******
****** 4.DIVIDE ******
****** 5.STATS ******
****** n/N to QUIT ******
****** ******
****************************
****************************
2
****** SUBTRACTION ******
****************************
****************************
****** 23 - 18=? ******
****************************
****************************
34
***********WRONG!***********
******CHOOSE A PROBLEM******
****************************
****************************
****** ******
****** 1.ADD ******
****** 2.SUBTRACT ******
****** 3.MULTIPLY ******
****** 4.DIVIDE ******
****** 5.STATS ******
****** n/N to QUIT ******
****** ******
****************************
****************************
3
****** MULTIPLICATION ******
****************************
****************************
****** 27 * 5=? ******
****************************
****************************
135
***********RIGHT!***********
******CHOOSE A PROBLEM******
****************************
****************************
****** ******
****** 1.ADD ******
****** 2.SUBTRACT ******
****** 3.MULTIPLY ******
****** 4.DIVIDE ******
****** 5.STATS ******
****** n/N to QUIT ******
****** ******
****************************
****************************
5
****************************
****************************
****** ******
********* ALLEN ***********
****** Total earnings $0.8 ***
****** Total correct $2 ***
****** Total Incorrect $1 ***
****** ******
****************************
****************************
******CHOOSE A PROBLEM******
****************************
****************************
****** ******
****** 1.ADD ******
****** 2.SUBTRACT ******
****** 3.MULTIPLY ******
****** 4.DIVIDE ******
****** 5.STATS ******
****** n/N to QUIT ******
****** ******
****************************
****************************
n
*******THANK YOU!******
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.