JAVA作業 演算法測試:編寫演算法測試小軟體
阿新 • • 發佈:2019-02-14
題目:
演算法測試:編寫算數測試小軟體,三個類組成,Teacher類負責出題,並判斷回答是否正確;ComputerFrame類負責提供檢視,使用者可通過此類GUI看到題目,並編寫答案;MainClass是軟體入口類。
思想:
1、Teacher類負責出題,因此需要三個屬性int num1、int num2、String operator分別表示兩個運算數以及一個運算子,保證出題隨即性,運算數與運算子均為隨機生成;並判斷回答是否正確,因此編寫函式boolean isRight(double result)用於判斷使用者輸入的答案是否正確。
2、ComputerFrame類負責提供檢視,使用者可通過此類GUI看到題目,並編寫答案,因此編寫ComputerFrame類繼承於JFrame類,屬性為所需要的各個按鈕、輸入框、標籤、使用者輸入的答案以及Teacher類物件。
3、MainClass是軟體入口類,即生成ComputerFrame類物件並設定其屬性。
原始碼:
MainClass.java
Teacher.javapackage countTest; import javax.swing.JFrame; /** * @author TX * @time 2016年12月3日 上午10:30:03 */ public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub ComputerFrame frame = new ComputerFrame("算術測試"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 120); frame.setLocation(500, 200); frame.setVisible(true); } }
package countTest; import java.util.Random; /** * @author TX * @time 2016年12月3日 上午10:30:34 */ public class Teacher { private int num1; private int num2; private String operator; public Teacher() { Random random = new Random(); num1 = random.nextInt(100); num2 = random.nextInt(100); int opNum = random.nextInt(4); if(opNum == 0) operator = "+"; if(opNum == 1) operator = "-"; if(opNum == 2) operator = "*"; if(opNum == 3) operator = "/"; } public boolean isRight(double result) { System.out.println(num1+ operator + num2 + "=" + result); if(operator.equals("+")) { System.out.println(num1+num2); return result == (num1+num2); } else if(operator.equals("-")) { System.out.println(num1-num2); return result == (num1-num2); } else if(operator.equals("*")) { System.out.println(num1*num2); return result == (num1*num2); } else if(operator.equals("/")) { System.out.println(((double)num1/(double)num2)); return Math.abs(result-(double)num1/(double)num2) < 0.001; } return false; } public int getNum1() { return num1; } public void setNum1(int num1) { this.num1 = num1; } public int getNum2() { return num2; } public void setNum2(int num2) { this.num2 = num2; } public String getOperator() { return operator; } public void setOperator(String operator) { this.operator = operator; } }
ComputerFrame.java
package countTest;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* @author TX
* @time 2016年12月3日 上午10:30:56
*/
public class ComputerFrame extends JFrame{
Teacher teacher;
JButton getQuestion, confirmAnswer;
JTextField text1, text2, text3;
JLabel label1, label2, checkLabel;
public int resultNum;
public ComputerFrame(String s){
super(s);
teacher = new Teacher();
setLayout(new FlowLayout());
text1 = new JTextField(6);
text1.setEditable(false);
text2 = new JTextField(6);
text2.setEditable(false);
text3 = new JTextField(6);
label1 = new JLabel("+");
label2 = new JLabel("=");
checkLabel = new JLabel();
getQuestion = new JButton("獲取題目");
getQuestion.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent arg0) {
teacher = new Teacher();
label1.setText(teacher.getOperator());
text1.setText(String.valueOf(teacher.getNum1()));
text2.setText(String.valueOf(teacher.getNum2()));
}});
confirmAnswer = new JButton("確認答案");
confirmAnswer.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent arg0) {
if(text3.getText().length() == 0)
{
checkLabel.setText("請輸入答案!");
}
else
{
double result = Double.valueOf(text3.getText());
if(teacher.isRight(result))
{
checkLabel.setText("回答正確!");
}
else
{
checkLabel.setText("回答錯誤!");
}
}
}});
label1.setText(teacher.getOperator());
text1.setText(Integer.toString(teacher.getNum1()));
text2.setText(Integer.toString(teacher.getNum2()));
this.add(getQuestion);
this.add(text1);
this.add(label1);
this.add(text2);
this.add(label2);
this.add(text3);
this.add(confirmAnswer);
this.add(checkLabel);
setVisible(true);
validate();
}
}