Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

TheMathGame: Your task is to develop a program that will teach youngsters the ba

ID: 3813793 • Letter: T

Question

TheMathGame: Your task is to develop a 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.   
WARNING: Note that your main() function should consist of mostly function calls and not be greater than 100 lines in total; however, your entire program will be closer to 500 - 800 lines of code!
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
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

please include splash screen to include name of the game title: The Math Game, by, my name "Timothy Kim", please also write the code in the programming language java, make sure no crashes

Explanation / Answer

//GameField.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JPanel;

public class GameField extends JPanel {

   private int[] getNumbers;

   private boolean checkk;

   public int[] getGetNumbers() {

       // getNumbers = (new GameModel).getFinalAnswer();
      
       return getNumbers;
   }

   public GameField() {

       setLayout(new BorderLayout());
       setVisible(true);
   }

   public void paintComponent(Graphics g) {
       super.paintComponent(g);
       Graphics2D g2 = (Graphics2D) g;

       GameModel model = new GameModel();

       int[] numbers = model.getFinalAnswer();
       String operand = model.getOperand();
       getNumbers = numbers;

       checkk = model.checkAnswer();

       int width = getWidth();
       int height = getHeight();

       Font font = new Font("AR JULIAN", Font.TRUETYPE_FONT, 55);

       g2.setFont(font);
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);
       g.setFont(font);

       g.setColor(new Color(230, 225, 230));
       g.fillRect(0, 0, width, height);

       g.setColor(Color.RED);
       g.drawString(numbers[0] + " " + operand + " " + numbers[1] + " = "
               + numbers[2], width / 7, 3 * height / 5);

   }

   public boolean getCheck() {
       System.out.println(checkk);
       return checkk;
   }
}

//GameModel.java

import java.util.Random;

public class GameModel {

   private static Random ran;
   public int randomNumb1;
   public int randomNumb2;
   private String operand = " ";
   private static int[] threeNumbers;
   private static int[] finalAnswer;
   public static boolean check;
   public boolean lastCheck;

   public GameModel() {
       solutionChances();
       solution();
   }

   public String getOperand() {
       return operand;
   }

   public int[] ranNumbers() {

       int count = MainFrame.getCount();
       int[] numbers = new int[2];
       if (count < 10) {
           ran = new Random();
           numbers[0] = ran.nextInt(50);
           ran = new Random();
           numbers[1] = ran.nextInt(50);

       } else if (count < 30) {
           ran = new Random();
           numbers[0] = ran.nextInt(100);
           ran = new Random();
           numbers[1] = ran.nextInt(100);

       } else if (count < 50) {
           ran = new Random();
           numbers[0] = ran.nextInt(250);
           ran = new Random();
           numbers[1] = ran.nextInt(250);

       } else {
           ran = new Random();
           numbers[0] = ran.nextInt(500);
           ran = new Random();
           numbers[1] = ran.nextInt(500);
       }
       return numbers;
   }

   public static String operatori() {

       ran = new Random();
       int stringRan = ran.nextInt(4);

       String[] operatoret = { "+", "-", "*", "/" };
       String answer = "";

       switch (stringRan) {
       case 0:
           answer = operatoret[0];
           break;
       case 1:
           answer = operatoret[1];
           break;
       case 2:
           answer = operatoret[2];
           break;
       case 3:
           answer = operatoret[3];
           break;
       }

       return answer;
   }

   public int[] solutionChances() {
       ran = new Random();

       finalAnswer = new int[3];

       int ranNumb = ran.nextInt(2) + 1;

       int[] answer = new int[3];
       int[] ranNumbers = new int[3];
       ranNumbers = ranNumbers();
       operand = operatori();
       switch (ranNumb) {
      
       case 1:
           switch (operand) {
           case "+":
               answer[2] = ranNumbers[0] + ranNumbers[1];
               operand = "+";
               break;
           case "-":
               answer[2] = ranNumbers[0] - ranNumbers[1];
               operand = "-";
               break;
           case "*":
               answer[2] = (ranNumbers[0]) * ranNumbers[1];
               operand = "*";
               break;
           case "/":
               if (ranNumbers[1] == 0) {
                   ranNumbers[1] += 1;

                   answer[2] = ranNumbers[0] / ranNumbers[1];
               } else
                   answer[2] = ranNumbers[0] / ranNumbers[1];
               operand = "/";
               break;
           }

           break;

       case 2:
           switch (operand) {
           case "+":
               answer[2] = (ranNumbers[0]) + ranNumbers[1] + ran.nextInt(50);
               operand = "+";
               break;
           case "-":
               answer[2] = ranNumbers[0] - ranNumbers[1] + ran.nextInt(50);
               operand = "-";
               break;
           case "*":
               answer[2] = ranNumbers[0] * ranNumbers[1] + ran.nextInt(50);
               operand = "*";
               break;
           case "/":
               if (ranNumbers[1] == 0) {
                   // nese eshte 0 e shton nje qe mos te pjestohet me 0 pastaj
                   // qe te qet perjashtim
                   ranNumbers[1] += 1;
                   answer[2] = ranNumbers[0] / ranNumbers[1];
               } else {
                   answer[2] = ranNumbers[0] / ranNumbers[1];
               }

               operand = "/";
               break;
           }
           break;
       }
       answer[0] = ranNumbers[0];
       answer[1] = ranNumbers[1];

       finalAnswer = answer;

       return answer;
   }

   public int[] getFinalAnswer() {
      
       return finalAnswer;
   }

   public boolean solution() {

       int[] solutionsNumbers = new int[3];
       int[] numbers = getFinalAnswer();

       threeNumbers = new int[3];
       threeNumbers = numbers;
       solutionsNumbers = threeNumbers;

       int numb1 = solutionsNumbers[0];
       int numb2 = solutionsNumbers[1];
       int solution = solutionsNumbers[2];

       boolean answer = false;

       switch (operand) {
       case "+":
           answer = (numb1 + numb2) == solution;
           break;
       case "-":
           answer = (numb1 - numb2) == solution;
           break;
       case "*":
           answer = (numb1 * numb2) == solution;
           break;
       case "/":
           answer = (numb1 / numb2) == solution;
           break;
       }
  
       check = answer;
       return answer;
   }

   public static boolean checkAnswer() {
       return check;
   }


}

//LeaderLabel.java

import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class LeaderLabel extends JPanel {
   private JLabel label;

   public LeaderLabel() {
      
       Dimension dim = getPreferredSize();
      
      
       int width = 350;

       dim.width = width;
       setPreferredSize(dim);
      
       setBorder(BorderFactory.createEtchedBorder());
      
       Border innerBorder = BorderFactory.createTitledBorder("Hign scores!");
       Border outerBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);

       setBorder(BorderFactory.createCompoundBorder(outerBorder, innerBorder));
   }
}

//StartFrame.java

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class StartFrame extends JPanel {
   private JRadioButton modTime;
   private JRadioButton testMod;
   private ButtonGroup genderGroup;

   public StartFrame() {

       modTime = new JRadioButton("Time");
       testMod = new JRadioButton("Test");
       genderGroup = new ButtonGroup();

       JButton play = new JButton("Play");

       play.setBounds(285, 290, 85, 40);
       play.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               new MainFrame();
           }
       });

       add(play);

       // set up gender ratios
       genderGroup.add(modTime);
       genderGroup.add(testMod);

       modTime.setSelected(true);

       setLayout(null);

       // ModTime radio button

       // first mod

       modTime.setBounds(260, 240, 50, 50);
       add(modTime);

       // second mod
       testMod.setBounds(345, 240, 50, 50);
       add(testMod);
   }

   public void paintComponent(Graphics g) {
       super.paintComponent(g);
       Graphics2D g2 = (Graphics2D) g;

       int width = 700;
       int height = 400;

       g.setColor((new Color(230, 235, 240)));
       g.fillRect(0, 0, width, height);
       g.setColor(Color.RED);
       g.fillRect(width / 4, height / 5, width / 2, height / 3);

       g.setColor(Color.black);
       g.drawString("Mods:", width / 2 - 30, 3 * height / 5);

       g.setColor(Color.white);

       Font font = new Font("Arial Narrow", Font.TRUETYPE_FONT, 55);

       g2.setFont(font);
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);

       g.drawString("MATH", 2 * width / 7, 4 * height / 12);
       g.drawString("+ effects", 3 * width / 7, 4 * height / 9);
   }

   public static void main(String[] args) {
       StartFrame panel = new StartFrame();

       JFrame frame = new JFrame("Math Game For Children");

       JMenuBar mb = new JMenuBar();
       frame.setJMenuBar(mb);
       JMenu file = new JMenu("File");
       mb.add(file);
       JMenuItem exit = new JMenuItem("Exit");
       file.add(exit);

       exit.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent arg0) {
               System.exit(0);
           }

       });

       JMenu help = new JMenu("Help");
       mb.add(help);
       JMenuItem about = new JMenuItem("About");
       help.add(about);

       about.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent arg0) {
               JOptionPane
                       .showMessageDialog(
                               null,
                               "- Math +effects - @author: bharat. "
                                       + " To be continued...!");
           }
       });

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.add(panel);
       frame.setSize(700, 400);
       frame.setVisible(true);
   }

}

//MainFrame.java

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class MainFrame extends JFrame {

   private LeaderLabel leader;
   private GameField gameField;
   private Score score;
   private boolean check;
   private static int count;
   String name = null;
   public static String name1;

   public MainFrame() {
       super("Math Effects");

       setSize(700, 400);
       setVisible(true);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setResizable(false);

       leader = new LeaderLabel();
       gameField = new GameField();

       score = new Score();

       setLayout(null);

       JButton Yes = new JButton("Yes");

       Yes.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {
               check = GameModel.checkAnswer();
               if (check) {
                   score.incCorrect();
                   score.repaint();
                   gameField.repaint();
                   count++;

               } else {
                   score.incWrong();
                   score.repaint();
                   gameField.repaint();

                   count++;
               }
               if (Score.getWrong() > 5) {
                   JOptionPane.showMessageDialog(null, "Game Over");
                   score.reset();
                   score.repaint();
                   count = 0;
                   gameField.repaint();
               }

           }

       });

       JButton No = new JButton("No");

       No.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {

               check = GameModel.checkAnswer();
               if (check) {
                   score.incWrong();
                   score.repaint();
                   gameField.repaint();
                   count++;
               } else {
                   score.incCorrect();
                   score.repaint();
                   gameField.repaint();

                   count++;
               }

               if (Score.getWrong() > 5) {
                   JOptionPane.showMessageDialog(null, "Game Over.");
                   score.reset();
                   count = 0;
                   gameField.repaint();
               }
           }

       });

       Yes.setBounds(140, 305, 80, 37);
       add(Yes);
       No.setBounds(240, 305, 80, 37);
       add(No);

       leader.setBounds(480, 0, 220, 340);
       add(leader);

       score.setBounds(0, 0, 482, 40);
       add(score);

       gameField.setBounds(0, 40, 480, 255);
       add(gameField);

       JMenuBar mb = new JMenuBar();
       setJMenuBar(mb);
       JMenu file = new JMenu("File");
       mb.add(file);
       JMenuItem exit = new JMenuItem("Exit");
       file.add(exit);

       exit.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent arg0) {
               System.exit(0);
           }

       });

       JMenu help = new JMenu("Help");
       mb.add(help);

       JMenuItem howToPlay = new JMenuItem("How To Play");
       help.add(howToPlay);

       final String howTo = "Two random numbers are generated together with the operand(random). "
               + "Then some answer is generated. If its true and you press Yes, you have +1 Corrected, and if its true and you press No, you have +1 Wronged. "
               + " "
               + "Warning: All calculations are done with integers, so divide operator('/') is done with whole down part. "
               + "For Example if we have: 5/7, the result will be 0, because 5/7 is less than 1 and when we take the whole down part the result would be 0. "
               + "Another example: 10/3 = 3, 15/4 = 3, 20/3 = 6 etc.";

       howToPlay.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent arg0) {
               JOptionPane.showMessageDialog(null, howTo);
           }

       });

       JMenuItem about = new JMenuItem("About");
       help.add(about);

       about.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent arg0) {
               JOptionPane
                       .showMessageDialog(
                               null,
                               "- Math +effects - @copyright. All rights reserved. "
                                       + "@author: Ramadan Pajaziti. To be continued...!");
           }

       });

   }

   public static int getCount() {
       return count;
   }

   public static void main(String[] args) {
       new MainFrame();
   }

}

//Score.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Score extends JPanel {

   private int correct;
   private static int wrong;

   public void paintComponent(Graphics g) {
       super.paintComponent(g);
       Graphics2D g2 = (Graphics2D) g;
       setBorder(BorderFactory.createEtchedBorder());

       Dimension dim = getPreferredSize();

       int height = 25;

       dim.height = height;
       setPreferredSize(dim);

       g.setColor(Color.black);

       Font font = new Font("Arial Narrow", Font.ITALIC, 20);

       g2.setFont(font);
       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);

       g.drawString("Wrong: " + wrong, 250, 25);
       g.drawString("Correct: " + correct + " ; ", 150, 25);

   }
  
   public void reset(){
       correct = 0;
       wrong = 0;
       repaint();
   }

   public void incCorrect() {
       correct++;
   }

   public void incWrong() {
       wrong++;
   }

   public static int getWrong() {
       return wrong;
   }


}

just run the java StartFrame.java

by using java compiler