作品基本信息
这个作业属于哪个课程 | https://bbs.csdn.net/forums/ssynkqtd-05 |
---|---|
这个作业的要求在哪里 | https://bbs.csdn.net/topics/617294583 |
这个作业的目标 | 实现基本功能,具有基本功能的计算器,实现加、减、乘、除、归零基本操作;附加功能:具有科学计算的计算器,实现次方、幂、三角函数等操作 |
其他参考文献 | … |
成果展示
Gitcode项目地址
PSP表格
PSP | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 30 | 50 |
• Estimate | • 估计这个任务需要多少时间 | 10 | 60 |
Development | 开发 | 40 | 50 |
• Analysis | • 需求分析 (包括学习新技术) | 100 | 115 |
• Design Spec | • 生成设计文档 | 20 | 25 |
• Design Review | • 设计复审 | 10 | 25 |
• Coding Standard | • 代码规范 (为目前的开发制定合适的规范) | 15 | 15 |
• Design | • 具体设计 | 30 | 40 |
• Coding | • 具体编码 | 200 | 240 |
• Code Review | • 代码复审 | 10 | 20 |
• Test | • 测试(自我测试,修改代码,提交修改) | 10 | 20 |
Reporting | 报告 | 30 | 90 |
• Test Repor | • 测试报告 | 15 | 15 |
• Size Measurement | • 计算工作量 | 5 | 5 |
• Postmortem & Process Improvement Plan | • 事后总结, 并提出过程改进计划 | 20 | 25 |
合计 | 545 | 780 |
解题思路描述
1.选择语言。我原本打算使用vscode用c++制作计算器的,但是由于电脑限制做到一半还是放弃选择了java;
2.设计界面和核心功能。设计界面相对而言比较简单,通过JPanel来制作计算器的页面和布局。因为个人能力和水平的限制,我只用java完成了最基本的加减乘除和归零运算。
3.exe打包。
4.进行单元测试。
关键代码展示
代码一:
设置需要的计算机图形按键
public void addCenterButton(){
String btn_text = "123+456-789*0.=/";
String regex = "[\\+\\-*/.=]";
this.jp_center.setLayout(new GridLayout(4,4));
for(int i=0;i<16;i++)
{
String temp = btn_text.substring(i,i+1);
JButton btn = new JButton();
btn.setText(temp);
if(temp.matches(regex)){
btn.setFont(new Font("粗体",Font.BOLD,16));
btn.setForeground(Color.blue);
}
代码二:
实现按压“±*/”运算符键时计算清零
public void actionPerformed(ActionEvent e) {
String clickStr = e.getActionCommand();
if (".0123456789".indexOf(clickStr) != -1) {
this.input_text.setText(input_text.getText() + clickStr);
this.input_text.setHorizontalAlignment(JTextField.RIGHT);
// JOptionPane.showMessageDialog(this, clickStr);
}else if(clickStr.matches("[\\+\\-*/]{1}")){
operator = clickStr;
firstInput = this.input_text.getText();
this.input_text.setText("");
} else if (clickStr.equals("=")) {
Double a = Double.valueOf(firstInput);
Double b = Double.valueOf(this.input_text.getText());
Double result = null;
switch (operator){
case "+":
result = a+b;
break; case "-":
result = a-b;
case "*":
result = a*b;
case "/":
if(b!=0){
result = a/b;
}
break;
}
this.input_text.setText(result.toString());
}
}
单元测试
测试代码
```java
public class Test {
carculator j = new carculator();
@org.junit.Test
public void Test_Add(){
Double res = j.operator(6, "+", 14);
assertEquals(res, (Double)20.0);
}
@org.junit.Test
public void Test_Subs(){
Double res = j.operator(10, "-", 6);
assertEquals(res, (Double)4.0);
}
@org.junit.Test
public void Test_multiply(){
Double res = j.operator(6, "*", 6);
assertEquals(res, (Double)36.0);
}
@org.junit.Test
public void Test_Division(){
Double res = j.operator(12, "/", 4);
assertEquals(res,(Double)3.0);
}
}
``
心得体会
通过本次实验,我学到了如何制作可视化界面以及java相关知识,更加了解了idea的功能和用法。学会了如何用markdown写报告,了解了制作一个软件相关的流程。