1. Question requirements
Write a small arithmetic test software to train primary school students’ arithmetic ability. The program consists of3classes, of which The Teacherclass object is responsible for giving arithmetic questions and judging whether the answer of the respondent is correct:ComputerFrameTheGUIinterface provided by the class object sees the question and passes it TheGUIinterface gives answers to the questions;MailClassis the main class.
2. Question requirements
The running results are shown in the figure below. The variable names of each component are marked in the figure:
The running results are shown inthe followingpicture< strong>. The variable names of each component are marked in the figure:
After clicking “Confirm Answer”, if you enter an incorrect answer, it will display: “You answered incorrectly< strong>“, if the answer is correct, it will display “Your answeris correct”, if the input is not a number, it will display “Please enter numeric characters“
3. Code display
TeacherClass
public class Teacher { int numberOne,numberTwo; String operator=""; boolean right; public int giveNumberOne(int n){ numberOne=(int)(Math.random()*n) + 1; return numberOne; } public int giveNumberTwo(int n){ numberTwo=(int)(Math.random()*n) + 1; return numberTwo; } public String giveOperator(){ double d=Math.random(); if(d>=0.8) operator=" + "; else if(d<0.8 & &d>0.5) operator="-"; else if(d<=0.5 & amp; & amp;d>0.2) operator="*"; else if(d<=0.2) operator="/"; return operator; } public boolean getRight(int answer){ if(operator.equals(" + ")){ if(answer==numberOne + numberTwo) right=true; else right=false; } else if(operator.equals("-")){ if(answer==numberOne-numberTwo) right=true; else right=false; } else if(operator.equals("*")){ if(answer==numberOne*numberTwo) right=true; else right=false; } else if(operator.equals("/")){ if(answer==numberOne/numberTwo) right=true; else right=false; } return right; } }
ComputerFrameClass
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.*; public class ComputerJFrame extends JFrame implements ActionListener{ JTextField textOne, textTwo, textResult; JButton getProblem,giveAnswer; JLabel operatorLabel,message; Teacher teacher; ComputerJFrame(String s){ super(s); teacher = new Teacher(); setLayout(new FlowLayout()); textOne = new JTextField(10); textTwo = new JTextField(10); textResult = new JTextField(10); operatorLabel = new JLabel(" + "); //Create operatorLabel, initial value "+" message = new JLabel("You haven't answered yet"); //Create message, initial value "You haven't answered yet" getProblem = new JButton("Get the problem"); //Create getProblem, the initial value is "Get the problem" giveAnswer = new JButton("Confirm Answer"); //Create giveAnswer with initial value "Confirm Answer" //[Code 9]//Add several components to the window add(getProblem); add(textOne); add(operatorLabel); add(textTwo); add(new JLabel("=")); add(textResult); add(giveAnswer); add(message); textResult.requestFocus(); textOne.setEditable(false); textTwo.setEditable(false); getProblem.addActionListener(this); //Register the current window as the ActionEvent event monitor of getProblem giveAnswer.addActionListener(this); //Register the current window as the ActionEvent event monitor of giveAnwser textResult.addActionListener(this); //Register the current window as the ActionEvent event monitor of textResult setBounds(100,100,450,100); //Set the initial position when it appears on the screen, x=100, y=100, width=450, height=100 setLocationRelativeTo(null); //Set the form to be centered setVisible(true); //Window visibility settings validate(); addWindowFocusListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } } ); } public void actionPerformed(ActionEvent e){ if(e.getSource() == getProblem) { int number1 = teacher.giveNumberOne(10); //Get the value of the first operand int number2 = teacher.giveNumberTwo(10); //Get the value of the second operand String operator = teacher.giveOperator(); //get operator textOne.setText("" + number1); textTwo.setText("" + number2); textResult.setText(null); operatorLabel.setText(operator); message.setText("Please answer"); //Set the text of the text box and label on the graphical interface, including: textOne, textTwo, textResult, operatorLabel, message } if(e.getSource() == giveAnswer) { String answer = textResult.getText(); //Get the answer entered in the text box try{ int result = Integer.parseInt(answer); if(teacher.getRight(result) == true) message.setText("You answered correctly"); else{ message.setText("Your answer is wrong"); } //If the answer is correct, the message displays "Your answer is correct", if it is incorrect, the message displays "You answered incorrectly" } catch(NumberFormatException ex){ message.setText("Please enter numeric characters"); //If the input is not a number, it will display "Please enter a number character" } } textResult.requestFocus(); //textResult gets focus validate(); } }
public class MainClass { public static void main(String[] args) { ComputerJFrame frame; frame = new ComputerJFrame("Arithmetic Test"); } }
Four, the running results are as follows