JAVA Help! TheMathGame: Your task is to develop a java program that will teach y
ID: 3815659 • Letter: J
Question
JAVA Help!
TheMathGame:
Your task is to develop a java program that will teach youngsters the basic math facts of addition, subtraction, multiplication and division. The program 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.
PROGRAM REQUIREMENTS
1. Generate simple math fact problems:
a. Addition (the total must be an integer >= 0)
b. Subtraction (the difference must be an integer >= 0)
c. Multiplication (the product must be an integer >= 0)
d. 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:
a. The user’s name
b. The total correct answers
c. The total wrong answers
d. 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:
a. Statistics are read from the file at the start of the game (if the user is a returning player).
b. 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:
a. credits //This function is used to display your name and what the program does
b. menu // This function is used to display the menu with various options
c. validateUserResponse // This function is used to validate user input from the menu
d. validateUserAnswer // This function is used to validate user input and ensure that ONLY numeric answers are entered by the user.
e. checkUserAnswer // given a math problem, this function is used to check if the answer the user entered is correct or incorrect
f. updateStats // This function is used to keep a running total of game statistics (in RAM)
g. displayStats // This function is used to display statistics on screen
h. 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.
i. saveStats // This function is used to save player statistics on an external txt file.
j. 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.
GENERAL RESTRICTIONS FOR ALL QUIZZES, MIDTERM AND FINAL EXAM
1. No infinite loops, examples include:
a. for(;;)
b. while(1)
c. while(true)
d. do{//code}while(1);
2. No break statements to exit loops SPECIFIC RESTRICTIONS FOR THIS ACTIVITY
See above
Program Flow
1. At the start of the game, an initial “Splash” screen must be displayed which includes:
a. The game’s title
b. Your name
After the “Splash” screen a prompt must ask the user for his/her name (validate input: no numbers, no blanks)
Enter your name and pressExplanation / Answer
/******************************************************************************
* Name : TheMathGame.java
* Author : sitaram.chhimpa
* Date : Apr 12, 2017
*****************************************************************************/
package sample;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
/**
* TODO: Type Description
*/
class record
{
private String userName;
private int correctAns;
private int wrongAns;
private double totalEarnings;
public record(String name, int correct, int wrong, double earning)
{
this.userName = name;
this.correctAns = correct;
this.wrongAns = wrong;
this.totalEarnings = earning;
}
/**
* Getter method for userName
*
* @return userName
*/
public String getUserName()
{
return this.userName;
}
/**
* Setter method for userName
*
* @param userName Value for userName
*/
public void setUserName(String userName)
{
this.userName = userName;
}
/**
* Getter method for correctAns
*
* @return correctAns
*/
public int getCorrectAns()
{
return this.correctAns;
}
/**
* Setter method for correctAns
*
* @param correctAns Value for correctAns
*/
public void setCorrectAns(int correctAns)
{
this.correctAns = correctAns;
}
/**
* Getter method for wrongAns
*
* @return wrongAns
*/
public int getWrongAns()
{
return this.wrongAns;
}
/**
* Setter method for wrongAns
*
* @param wrongAns Value for wrongAns
*/
public void setWrongAns(int wrongAns)
{
this.wrongAns = wrongAns;
}
/**
* Getter method for totalEarnings
*
* @return totalEarnings
*/
public double getTotalEarnings()
{
return this.totalEarnings;
}
/**
* Setter method for totalEarnings
*
* @param totalEarnings Value for totalEarnings
*/
public void setTotalEarnings(double totalEarnings)
{
this.totalEarnings = totalEarnings;
}
};
public class TheMathGame
{
static Scanner reader = new Scanner(System.in);
public static void menu()
{
System.out.println("CHOOSE A PROBLEM");
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");
}
static boolean validateUserResponse(int input)
{
if (input < 1 || input > 5)
{
return false;
}
else
{
return true;
}
}
static boolean validateUserAnswer(int answer)
{
return false;
}
static boolean checkUserAnswer(int option, int a, int b, int ans)
{
if (option == 1)
{
return ((a + b) == ans);
}
else if (option == 2)
{
return ((a - b) == ans);
}
else if (option == 3)
{
return ((a * b) == ans);
}
else if (option == 4)
{
return ((a / b) == ans);
}
return false;
}
static void updateStats(boolean correct)
{
}
static void displayStats(record r)
{
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(r.getUserName());
System.out.println("Total Earnings " + formatter.format(r.getTotalEarnings()));
System.out.println("Total correct " + r.getCorrectAns());
System.out.println("Total incorrect " + r.getWrongAns());
}
static void retireveStats()
{
}
static void saveStats()
{
}
static boolean generateAddition()
{
Random rand = new Random();
int a = rand.nextInt(50) + 1;
int b = rand.nextInt(50) + 1;
System.out.println("ADDITION");
System.out.println(a + " + " + b + " = ?");
int ans = reader.nextInt();
if (checkUserAnswer(1, a, b, ans))
{
return true;
}
else
{
return false;
}
}
static boolean generateSubtraction()
{
Random rand = new Random();
int b = rand.nextInt(50) + 1;
int a = rand.nextInt(50) + 1 + b;
System.out.println("SUBTRACTION");
System.out.println(a + " - " + b + " = ?");
int ans = reader.nextInt();
if (checkUserAnswer(2, a, b, ans))
{
return true;
}
else
{
return false;
}
}
static boolean generateMultiplication()
{
Random rand = new Random();
int a = rand.nextInt(50) + 1;
int b = rand.nextInt(50) + 1;
System.out.println("MULTIPLICATION");
System.out.println(a + " * " + b + " = ?");
int ans = reader.nextInt();
if (checkUserAnswer(3, a, b, ans))
{
return true;
}
else
{
return false;
}
}
static boolean generateDivision()
{
Random rand = new Random();
int b = rand.nextInt(50) + 1;
int a = b * (rand.nextInt(50) + 1);
System.out.println("DIVIDE");
System.out.println(a + " / " + b + " = ?");
int ans = reader.nextInt();
if (checkUserAnswer(4, a, b, ans))
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args)
{
Map<String, record> recordMap = new HashMap<String, record>();
// initial spalsh screen
System.out.println("The Math Game...y/Y to continue, any other character to exit");
char ch = reader.next().charAt(0);
if (ch == 'y' || ch == 'Y')
{
System.out.println("Enter your name and press <ENTER>");
String UserName = reader.next();
System.out.println("Name : " + UserName);
record r = new record(UserName, 0, 0, 0);
if (recordMap.containsKey(UserName))
{
r = recordMap.get(UserName);
}
else
{
recordMap.put(UserName, r);
}
menu();
int input = reader.nextInt();
while (validateUserResponse(input))
{
if (input == 1)
{
if (generateAddition())
{
System.out.println("RIGHT!");
r.setCorrectAns(r.getCorrectAns() + 1);
r.setTotalEarnings(r.getTotalEarnings() + 0.05);
}
else
{
System.out.println("WRONG!");
r.setWrongAns(r.getWrongAns() + 1);
r.setTotalEarnings(r.getTotalEarnings() - 0.03);
}
}
else if (input == 2)
{
if (generateSubtraction())
{
System.out.println("RIGHT!");
r.setCorrectAns(r.getCorrectAns() + 1);
r.setTotalEarnings(r.getTotalEarnings() + 0.05);
}
else
{
System.out.println("WRONG!");
r.setWrongAns(r.getWrongAns() + 1);
r.setTotalEarnings(r.getTotalEarnings() - 0.03);
}
}
else if (input == 3)
{
if (generateMultiplication())
{
System.out.println("RIGHT!");
r.setCorrectAns(r.getCorrectAns() + 1);
r.setTotalEarnings(r.getTotalEarnings() + 0.05);
}
else
{
System.out.println("WRONG!");
r.setWrongAns(r.getWrongAns() + 1);
r.setTotalEarnings(r.getTotalEarnings() - 0.03);
}
}
else if (input == 4)
{
if (generateDivision())
{
System.out.println("RIGHT!");
r.setCorrectAns(r.getCorrectAns() + 1);
r.setTotalEarnings(r.getTotalEarnings() + 0.05);
}
else
{
System.out.println("WRONG!");
r.setWrongAns(r.getWrongAns() + 1);
r.setTotalEarnings(r.getTotalEarnings() - 0.03);
}
}
else if (input == 5)
{
displayStats(r);
}
menu();
System.out.println("Please enter a number between 1 to 5 ");
input = reader.nextInt();
}
}
else
{
System.out.println("Exit from Game");
}
}
}
//output:
Enter your name and press <ENTER>
sitaram
CHOOSE A PROBLEM
1.ADD
2.SUBTRACT
3.MULTIPLY
4.DIVIDE
5.STATS
n/N TO QUIT
1
ADDITION
23 + 23 = ?
46
RIGHT!
CHOOSE A PROBLEM
1.ADD
2.SUBTRACT
3.MULTIPLY
4.DIVIDE
5.STATS
n/N TO QUIT
Please enter a number between 1 to 5
2
SUBTRACTION
43 - 38 = ?
5
RIGHT!
CHOOSE A PROBLEM
1.ADD
2.SUBTRACT
3.MULTIPLY
4.DIVIDE
5.STATS
n/N TO QUIT
Please enter a number between 1 to 5
3
MULTIPLICATION
49 * 43 = ?
443
WRONG!
CHOOSE A PROBLEM
1.ADD
2.SUBTRACT
3.MULTIPLY
4.DIVIDE
5.STATS
n/N TO QUIT
Please enter a number between 1 to 5
4
DIVIDE
308 / 7 = ?
44
RIGHT!
CHOOSE A PROBLEM
1.ADD
2.SUBTRACT
3.MULTIPLY
4.DIVIDE
5.STATS
n/N TO QUIT
Please enter a number between 1 to 5
5
sitaram
Total Earnings 0.12
Total correct 3
Total incorrect 1
CHOOSE A PROBLEM
1.ADD
2.SUBTRACT
3.MULTIPLY
4.DIVIDE
5.STATS
n/N TO QUIT
Please enter a number between 1 to 5
22
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.