第二次作业
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第二次作业相关的知识,希望对你有一定的参考价值。
GitHub地址
https://git.coding.net/songofjoy/softwareProject.git
PSP
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
20 |
10 |
·Estimate |
· 预估时间 |
10 |
10 |
Development |
开发 |
370 |
330 |
·Analysis |
· 需求分析(包括学习新技术) |
30 |
40 |
·Design Spec |
· 生成设计文档 |
10 |
30 |
·Design Review |
· 设计复审 |
20 |
30 |
·Coding Standard |
· 代码规范 |
40 |
30 |
·Design |
· 具体设计 |
60 |
100 |
·Coding |
· 具体编码 |
220 |
180 |
·Code Review |
· 代码复审 |
30 |
40 |
·Test |
· 测试(自我测试,修改代码,提交修改) |
140 |
170 |
Reporting |
报告 |
250 |
250 |
·Test Report |
· 测试报告 |
220 |
220 |
·Size Measurement |
· 计算工作量 |
20 |
20 |
·Postmortem&Process Improvement Plan |
· 事后总结并提出过程改进计划 |
20 |
30 |
合计 |
|
630 |
830 |
需求分析
目标
本软件的主要目标为:四则混合运算题目生成及计算
用户特点
本软件最终用户为小学生(或小学教师),会基本的四则混合运算,不会负数计算。故我们应基于上述用户特点进行软件开发。
假定和约束
开发方法:面向对象的开发技术
开发语言:Java
开发期限:1周
需求规定
由于软件规模较小,在此只考虑功能需求。以小学生为主要服务对象,对软件的功能初步设定为:
a.表达式生成功能。分为操作数随机生成,运算符随机生成以及最终表达式生成。操作数包括100以内正整数和真分数;运算符包括( +, -, *, ÷);最终表达式需要包含带括号的子表达式。其中操作数及运算符序列个数也随机生成。
b. 表达式计算功能。要求计算带括号的混合运算的结果,若最后结果为分数,则需要对分数进行化简。
c. 用户交互功能。首先判断用户输入是否非法,例如输入字母时要求用户重新输入;再对用户答案进行正确性判断:若正确,记得分,若错误,显示正确答案。最后显示用户的最终得分。
d. 用命令行参数n控制题目数量。
运行环境规定
设备:PC
支持:PC支持Windows, Linux等操作系统
控制:本软件主要通过cmd运行,输入命令即可控制软件,无特殊要求。
详细设计
设计思路
- 生成操作数数组:随机生成正整数或真分数,可由随机数决定。特别注意的是生成分数时,分子应小于分母。将操作数数目作为该方法变量,循环生成操作数,存入数组。
b. 生成运算符数组:随机生成四种运算符,通过switch()实现。当生成的运算符为乘或除时,考虑插入括号,且保证括号插入在低优先级运算符两侧:若乘/除号前一个是加/减号,可以插入括号,是否插入可由随机数决定。将操作数数目作为该方法变量,循环生成操作数,存入数组,当运算符数量达到最大值时,最后插入”=”。
c. 生成表达式:将a,b步骤生成的序列按序插入就得到一个符合逻辑的表达式。首先遍历操作数数组,当有括号时,判断是否为左括号,若是,则先插入括号,再依次插入数字和运算符和对应的右括号。
d. 表达式计算:在这里并未采用逆波兰表达式计算方法,而是参考了其他实现方法:先定义无括号的方法,具体实现为将表达式拆分分别放入list(分为操作数list和运算符list),接下来取运算符,取对应索引处左右的操作数,计算,并将计算结果放回当前索引位置。有括号的方法,具体实现为查找表达式中的括号,取出括号内的子表达式,调用无括号方法进行计算,用该计算结果替换该子串,得到新的表达式,再递归调用有括号方法。
e. 用户交互界面:制作了一个简单的UI界面,使操作更加方便。
f. 得分记录缓存:每次关闭系统都会将正确率、正确题目、错误题目存储到本地,下次再次登录时会调出之前的得分记录。
g.带有计时功能
用户交互界面展示
界面说明
1.右下角是可以实现简体繁体英文的切换。
2.左下角是历史的正确率、正确题目和错误题目,关闭系统数据依然保留。
3.“重置”按钮是使正确率、正确题目和错误题目归0。
4.“清除”按钮是使TextField清空。
5.“退格”按钮是使TextField删除末尾的字符。
6.’=’与=的区别:
(1)’=’指在TextField添加一个’=’字符到末尾。
(2)= 指运算或者判断对错,如果TextField里面没有含=,则计算表达式的结果;如果TextField里面含=,就判断该式子是否正确。
7.“计”实现了计时功能,第一次点击是开始计时,第二此点是停止,第三次点是清0.
核心代码说明
ComputeController.java 加减乘除计算控制器
package calculator;
/**
* 加减乘除计算
* 冒号:表示一个分数,左边是分子,右边是分母
*
* @author Song
*
*/
public class ComputeController {
/**
* 执行计算
* @param data1
* @param data2
* @param sign
* @return
*/
public static String exec(String a1,String a2,String sign){
String retString="";
switch (sign) {
case "+":retString=add(a1, a2);break;
case "-":retString=substract(a1, a2);break;
case "×":retString=mutiply(a1, a2);break;
case "÷":retString=devide(a1, a2);break;
}
return retString;
}
/**
* 是否是加减乘除运算符
* @param string
* @return
*/
public static boolean isSign(String string){
if(string.equals("+")||string.equals("-")||
string.equals("×")||string.equals("÷")){
return true;
}
return false;
}
/**
* 化简分数并转化为String格式
* @param fac
* @return
*/
public static String simplify(int [] fac){
if(fac[0]==0){return "0";}
if(fac[0]%fac[1]==0){return String.valueOf(fac[0]/fac[1]);}
int gcd=gcd(fac[0],fac[1]);
fac[0]/=gcd;
fac[1]/=gcd;
return fac[0]+"/"+fac[1];
}
/**
* 求最大公约数
* @param a1
* @param a2
* @return
*/
public static int gcd(int a1,int a2){
if(a1==0||a2==0){System.out.println("Error:the data is zero.");}
int min=a1;
int max=a1;
if(a1<a2){
max=a2;
}
else{
min=a2;
}
if(max%min==0){
return min;
}
int temp=max%min;
max=min;
min=temp;
while(max%min!=0){
temp=max%min;
max=min;
min=temp;
}
return min;
}
/**
* 求最小公倍数
* @param a1
* @param a2
* @return
*/
public static int lcm(int a1,int a2){
if(a1==0||a2==0){System.out.println("Error:the data is zero.");}
int gcd=gcd(a1,a2);
return a1/gcd*a2;
}
/**
* stringdata转化为分数的表示形式
* @param data
* @return
*/
public static int [] string2fac(String data){
int [] retfac=new int[2];
if(data.contains("/")){
String strings[]=data.split("/");
retfac[0]=Integer.valueOf(strings[0]);
retfac[1]=Integer.valueOf(strings[1]);
}
else{
int integer=Integer.valueOf(data);
retfac[0]=integer;
retfac[1]=1;
}
return retfac;
}
/**
* 加法
* @param a1
* @param a2
* @return
*/
public static String add(String a1,String a2){
if(!a1.contains("/")&&!a2.contains("/")){
int a1int=Integer.valueOf(a1);
int a2int=Integer.valueOf(a2);
return String.valueOf(a1int+a2int);
}
int []a1fac=string2fac(a1);
int []a2fac=string2fac(a2);
//分母化同并实现运算
a1fac[0]=a1fac[0]*a2fac[1]+a1fac[1]*a2fac[0];
a1fac[1]=a1fac[1]*a2fac[1];
return simplify(a1fac);
}
/**
* 减法
* @param a1
* @param a2
* @return
*/
public static String substract(String a1,String a2){
if(!a1.contains("/")&&!a2.contains("/")){
int a1int=Integer.valueOf(a1);
int a2int=Integer.valueOf(a2);
return String.valueOf(a1int-a2int);
}
int []a1fac=string2fac(a1);
int []a2fac=string2fac(a2);
//分母化同并实现运算
a1fac[0]=a1fac[0]*a2fac[1]-a1fac[1]*a2fac[0];
a1fac[1]=a1fac[1]*a2fac[1];
return simplify(a1fac);
}
/**
* 乘法
* @param a1
* @param a2
* @return
*/
public static String mutiply(String a1,String a2){
if(!a1.contains("/")&&!a2.contains("/")){
int a1int=Integer.valueOf(a1);
int a2int=Integer.valueOf(a2);
return String.valueOf(a1int*a2int);
}
int []a1fac=string2fac(a1);
int []a2fac=string2fac(a2);
//分母化同并实现运算
a1fac[0]=a1fac[0]*a2fac[0];
a1fac[1]=a1fac[1]*a2fac[1];
return simplify(a1fac);
}
/**
* 除法
* @param a1
* @param a2
* @return
*/
public static String devide(String a1,String a2){
if(!a1.contains("/")&&!a2.contains("/")){
int []aint=new int[2];
aint[0]=Integer.valueOf(a1);
aint[1]=Integer.valueOf(a2);
return simplify(aint);
}
int []a1fac=string2fac(a1);
int []a2fac=string2fac(a2);
//分母化同并实现运算
a1fac[0]=a1fac[0]*a2fac[1];
a1fac[1]=a1fac[1]*a2fac[0];
return simplify(a1fac);
}
}
DoubleStack.java 处理计算优先级(双栈和逆波兰表达式)
package calculator;
import java.util.ArrayList;
import java.util.Stack;
/**
* 高级软件工程
* @author Song
*
*/
public class DoubleStack {
private ArrayList<String> testString=new ArrayList<>();
private Stack<String> stack = null;
private ArrayList<String> createStrings=new ArrayList<>();
public DoubleStack(String testString) {
this.testString = string2array(testString);
this.stack = new Stack<String>();
}
/**
* 打印逆波兰表达式和
*/
public void printCreString(){
System.out.println(createStrings);
}
/**
* 根据逆波兰表达式计算值
*/
public String compute(){
String ERRORINFO="The Function Is Wrong!";
try{
if(createStrings==null||createStrings.size()==0){
return ERRORINFO;
}
while(createStrings.size()!=1){
int i;
for(i=0;i<createStrings.size();i++){
if(ComputeController.isSign(createStrings.get(i))){
if(i-2<0){return ERRORINFO;}
String compute=ComputeController.exec(createStrings.get(i-2), createStrings.get(i-1), createStrings.get(i));
createStrings.set(i-2, compute);
createStrings.remove(i);
createStrings.remove(i-1);
break;
}
else if(i==createStrings.size()-1){return ERRORINFO;}
}
}
}
catch(Exception e){
return ERRORINFO;
}
return createStrings.get(0);
}
/**
* 逆波兰 双栈
*/
public ArrayList<String> analysisString() {
for (int i = 0; i < testString.size(); i++) {
String c = testString.get(i);
if (c.equals("+") || c.equals("-")) {
if (stack.isEmpty() || stack.peek().equals("(")) {
stack.push(c);
} else {
while (!stack.isEmpty()
&& (stack.peek().equals("×") || stack.peek().equals("÷")
|| stack.peek().equals("+") || stack.peek().equals("-"))) {
createStrings.add(stack.pop());
}
stack.push(c);
}
} else if (c.equals("×")|| c.equals("÷")) {
if (stack.isEmpty() || stack.peek().equals("+")
|| stack.peek().equals("-") || stack.peek().equals("(")) {
stack.push(c);
} else {
while (!stack.isEmpty()
&& (stack.peek().equals("÷") || stack.peek().equals("×"))) {
createStrings.add(stack.pop());
}
stack.push(c);
}
} else if (c.equals("(")) {
stack.push(c);
} else if (c.equals(")") ){
String temp = "";
while (!(temp = stack.pop()).equals("(")) {
createStrings.add(temp);
}
} else {
createStrings.add(c);
}
}
if (!stack.isEmpty()) {
while (!stack.isEmpty()) {
createStrings.add(stack.pop());
}
}
return createStrings;
}
/**
* string转array
* @param input
* @return
*/
public static ArrayList<String> string2array(String input){
String curString="";
ArrayList<String> strings=new ArrayList<>();
for(int i=0;i<input.length();i++){
char curchar=input.charAt(i);
if(curchar<58&&curchar>47||curchar==‘/‘){curString+=curchar;}
else{
if(!curString.equals("")){strings.add(curString);}
strings.add(""+curchar);curString="";
}
}
if(!curString.equals("")) {strings.add(curString);}
return strings;
}
}
Frame.java 简易GUI用户界面
package calculator;
import java.awt.EventQueue;
public class Frame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
JLabel lblNewLabel_1 = new JLabel("0");
JLabel label_2 = new JLabel("0");
JLabel label_3 = new JLabel("0.0%");
static JLabel lblNewLabel = new JLabel("正确题目");
static JLabel label = new JLabel("错误题目");
static JLabel label_1 = new JLabel("正确率");
static JButton button_20 = new JButton("重置");
static JButton button_19 = new JButton("清除");
static JButton button_18 = new JButton("退格");
static JButton button_23 = new JButton("简");
static JButton button_21 = new JButton("繁");
static JButton button_22 = new JButton("英");
static final String EN="resource_english";
static final String CH="resource_zh_CN_1";
static final String HARD="resource_hard";
static Score curScore=new Score();
DecimalFormat df= new DecimalFormat("######0.0");
/**
* 字体中英繁切换
* @param resource
*/
public static void changeCharacter(String resource){
ResourceBundle rb = ResourceBundle.getBundle(resource);
lblNewLabel.setText(rb.getString("rightAmount"));
label.setText(rb.getString("wrongAmount"));
label_1.setText(rb.getString("radioAmount"));
button_20.setText(rb.getString("reset"));
button_19.setText(rb.getString("clear"));
button_18.setText(rb.getString("back"));
button_23.setText(rb.getString("simply"));
button_21.setText(rb.getString("hard"));
button_22.setText(rb.getString("english"));
}
/**
* Launch the application.
* @throws IOException
* @throws ParseException
*/
public static void main(String[] args) throws Exception{
curScore=Util.readScore();
changeCharacter(CH);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame frame = new Frame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Frame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 540, 431);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(10, 10, 502, 30);
contentPane.add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("1");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"1");
}
});
btnNewButton.setBounds(28, 68, 52, 48);
contentPane.add(btnNewButton);
JButton button = new JButton("2");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"2");
}
});
button.setBounds(113, 68, 52, 48);
contentPane.add(button);
JButton button_1 = new JButton("3");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"3");
}
});
button_1.setBounds(195, 68, 52, 48);
contentPane.add(button_1);
JButton button_2 = new JButton("4");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"4");
}
});
button_2.setBounds(28, 145, 52, 48);
contentPane.add(button_2);
JButton button_3 = new JButton("5");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"5");
}
});
button_3.setBounds(113, 145, 52, 48);
contentPane.add(button_3);
JButton button_4 = new JButton("6");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"6");
}
});
button_4.setBounds(195, 145, 52, 48);
contentPane.add(button_4);
JButton button_5 = new JButton("7");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"7");
}
});
button_5.setBounds(28, 220, 52, 48);
contentPane.add(button_5);
JButton button_6 = new JButton("8");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"8");
}
});
button_6.setBounds(113, 220, 52, 48);
contentPane.add(button_6);
JButton button_7 = new JButton("9");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"9");
}
});
button_7.setBounds(195, 220, 52, 48);
contentPane.add(button_7);
JButton button_8 = new JButton("0");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"0");
}
});
button_8.setBounds(283, 68, 52, 48);
contentPane.add(button_8);
JButton button_9 = new JButton("+");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"+");
}
});
button_9.setBounds(370, 68, 52, 48);
contentPane.add(button_9);
JButton button_10 = new JButton("(");
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"(");
}
});
button_10.setBounds(283, 145, 52, 48);
contentPane.add(button_10);
JButton button_11 = new JButton("×");
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"×");
}
});
button_11.setBounds(370, 145, 52, 48);
contentPane.add(button_11);
JButton button_12 = new JButton(")");
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+")");
}
});
button_12.setBounds(283, 220, 52, 48);
contentPane.add(button_12);
JButton button_13 = new JButton("/");
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"/");
}
});
button_13.setBounds(370, 220, 52, 48);
contentPane.add(button_13);
JButton button_14 = new JButton("-");
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"-");
}
});
button_14.setBounds(446, 68, 52, 48);
contentPane.add(button_14);
JButton button_15 = new JButton("÷");
button_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"÷");
}
});
button_15.setBounds(446, 145, 52, 48);
contentPane.add(button_15);
JButton button_16 = new JButton("‘=‘");
button_16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"=");
}
});
button_16.setBounds(446, 220, 52, 48);
contentPane.add(button_16);
JButton button_17 = new JButton("=");
button_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(textField.getText().contains("=")){
String[] strings=textField.getText().split("=");
DoubleStack testStack = new DoubleStack(strings[0]);
testStack.analysisString();
String right=testStack.compute();
if(strings[1].equals(right)){
textField.setText("Right!");
curScore.setRightAmount(curScore.getRightAmount()+1);
System.out.println(curScore.getRightAmount());
System.out.println(curScore.getWrongAmount());
System.out.println(curScore.getRadioAmount());
curScore.setRadioAmount((double)curScore.getRightAmount()/(double)(curScore.getRightAmount()+curScore.getWrongAmount()));
lblNewLabel_1.setText(String.valueOf(curScore.getRightAmount()));
label_2.setText(String.valueOf(curScore.getWrongAmount()));
label_3.setText(String.valueOf(df.format(curScore.getRadioAmount()*100))+"%");
try {
Util.saveScore(curScore);
} catch (IOException | ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else{
textField.setText("Wrong!");
curScore.setWrongAmount(curScore.getWrongAmount()+1);
curScore.setRadioAmount((double)curScore.getRightAmount()/(double)(curScore.getRightAmount()+curScore.getWrongAmount()));
lblNewLabel_1.setText(String.valueOf(curScore.getRightAmount()));
label_2.setText(String.valueOf(curScore.getWrongAmount()));
label_3.setText(String.valueOf(df.format(curScore.getRadioAmount()*100))+"%");
try {
Util.saveScore(curScore);
} catch (IOException | ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}else{
DoubleStack testStack = new DoubleStack(textField.getText());
testStack.analysisString();
textField.setText(testStack.compute());
}
}
});
button_17.setBounds(422, 293, 76, 48);
contentPane.add(button_17);
lblNewLabel.setBounds(28, 293, 52, 15);
contentPane.add(lblNewLabel);
label.setBounds(28, 318, 52, 15);
contentPane.add(label);
label_1.setBounds(28, 343, 52, 15);
contentPane.add(label_1);
lblNewLabel_1.setBounds(86, 293, 54, 15);
lblNewLabel_1.setText(String.valueOf(curScore.getRightAmount()));
contentPane.add(lblNewLabel_1);
label_2.setBounds(86, 318, 54, 15);
label_2.setText(String.valueOf(curScore.getWrongAmount()));
contentPane.add(label_2);
label_3.setBounds(86, 343, 54, 15);
label_3.setText(String.valueOf(df.format(curScore.getRadioAmount()*100))+"%");
contentPane.add(label_3);
button_18.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int length=textField.getText().length();
if(length>0){
textField.setText(textField.getText().substring(0,length-1));
}
}
});
button_18.setBounds(322, 293, 76, 48);
contentPane.add(button_18);
button_19.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
button_19.setBounds(224, 293, 76, 48);
contentPane.add(button_19);
button_20.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
curScore=new Score();
lblNewLabel_1.setText("0");
label_2.setText("0");
label_3.setText("0.0%");
try {
Util.saveScore(curScore);
} catch (IOException | ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
button_20.setBounds(127, 293, 76, 48);
contentPane.add(button_20);
button_22.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
changeCharacter(EN);
}
});
button_22.setBounds(446, 351, 52, 31);
contentPane.add(button_22);
button_21.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
changeCharacter(HARD);
}
});
button_21.setBounds(384, 351, 52, 31);
contentPane.add(button_21);
button_23.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
changeCharacter(CH);
}
});
button_23.setBounds(318, 351, 52, 31);
contentPane.add(button_23);
}
}
Score.java 评分模型
package calculator;
public class Score {
private int rightAmount;
private int wrongAmount;
private double radioAmount;
public int getRightAmount() {
return rightAmount;
}
public void setRightAmount(int rightAmount) {
this.rightAmount = rightAmount;
}
public int getWrongAmount() {
return wrongAmount;
}
public void setWrongAmount(int wrongAmount) {
this.wrongAmount = wrongAmount;
}
public double getRadioAmount() {
return radioAmount;
}
public void setRadioAmount(double radioAmount) {
this.radioAmount = radioAmount;
}
}
Util.java 公共类(文件的读取写入)
package calculator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.Date;
public class Util {
static String FILENAME="score.dat";
public static String SPLITSTRING = "&HHS&";
//创建Score
public static void createScore() throws IOException {
Score score=new Score();
FileWriter writer = new FileWriter(FILENAME);
writer.write(score.getRightAmount() + SPLITSTRING);
writer.write(score.getWrongAmount() + SPLITSTRING);
writer.write(score.getRadioAmount() + SPLITSTRING);
writer.close();
}
//读取分数
public static Score readScore() throws IOException, ParseException {
Score score=new Score();
BufferedReader reader = new BufferedReader(new FileReader(FILENAME));
String line = reader.readLine();
String []infos=line.split(SPLITSTRING);
score.setRightAmount(Integer.valueOf(infos[0]));
score.setWrongAmount(Integer.valueOf(infos[1]));
score.setRadioAmount(Double.valueOf(infos[2]));
return score;
}
//写分数
public static void saveScore(Score score) throws IOException, ParseException {
FileWriter writer = new FileWriter(FILENAME);
writer.write(score.getRightAmount() + SPLITSTRING);
writer.write(score.getWrongAmount() + SPLITSTRING);
writer.write(score.getRadioAmount() + SPLITSTRING);
writer.close();
}
/**
*
* 获取目录下的所有文件名 By Hhs
*/
public static String[] getFileName(String path) {
File file = new File(path);
String[] fileName = file.list();
return fileName;
}
}
resource_zh_CN_1.properties等配置文件
实现了java语言的国际化,可以实现中文英文简体繁体的切换。
项目小结
这个作业功能是实现了,但是有些细节还需要改进,比如当计算结果取=时,如果输入下一个式子,TextField里面的内容不会自动清空,后期可以对这个进行一个优化,加一个字段的判断,如果按下=,该字段置为true,表示下个字符如果是数字就首先把TextField清空再放入数据。
在测试的时候出现一些小bug,比如对负数的处理等等,会得到令人意想不到的结果,当输入的表达式不合法时也加入了一个异常处理,返回The Function is Wrong!给前台界面,后期可以对此做改进,将异常返回做的细一点,比如是分母不能为0或者是数字太大或者是表达式里面有特殊字符,将错误信息尽可能细的返回给用户。
以上是关于第二次作业的主要内容,如果未能解决你的问题,请参考以下文章