基于Java的界面构建和代码编写的简易 Calculator
Project Introduction:
In this project, I create a Java project to edit the code to build a calculator. The basic steps of creating the calculator are as follows.
The Link Your Class | https://bbs.csdn.net/forums/ssynkqtd-04 |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/forums/ssynkqtd-04 |
The Aim of This Assignment | Create a Calculator by Visualization |
MU STU ID and FZU STU ID | 21126348_832102223 |
Work Procedure
PSP
Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
---|---|---|
Planning | 20 | 25 |
• Estimate | 20 | 25 |
Development | 405 | 450 |
• Analysis | 35 | 30 |
• Design Spec | 40 | 45 |
• Design Review | 15 | 15 |
• Coding Standard | 15 | 20 |
• Design | 90 | 100 |
• Coding | 200 | 180 |
• Code Review | 30 | 45 |
• Test | 10 | 15 |
Reporting | 180 | 140 |
• Test Repor | 60 | 40 |
• Size Measurement | 60 | 40 |
• Postmortem & Process Improvement Plan | 60 | 60 |
Sum | 605 | 615 |
Problem-Solving Idea
In this task, I need to learn some basic visualization skills to accomplish the assignment. In this project I choose Java on eclipse platform to construct a calculator and then export this program to generate a software.
To build a calculator, first I need to construct the interface of this calculator. Then, I arrange the button on the platform. Next, I need to listen to the button, when I click the button, the number and the operator need to be record and then the corrsponding opearation should be finished. Finally, the result of the equation ought to be displayed in the text box, and then export the project to create a software.
Code
The code of Java on eclipse platform are shown as follows:
package cn.lidan.start;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import lindan.cn.util.Const;
public class Calculator extends JFrame implements ActionListener{
//North component:
private JPanel jp_north = new JPanel();
private JTextField input_text = new JTextField();
private JButton c_Button = new JButton("C");
//Central component:
private JPanel jp_centre = new JPanel();
public Calculator () throws HeadlessException{
this.init();
this.addNorthComponent();
this.addCentreButton();
}
//Doing initialization:
public void init() {
this.setTitle(Const.TITLE);
this.setSize(Const.FRAME_H,Const.FRAME_W);
this.setLayout(new BorderLayout());
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//Adding north component:
public void addNorthComponent() {
this.input_text.setPreferredSize(new Dimension(300,50));
jp_north.add(input_text);
this.c_Button.setForeground(Color.black);
jp_north.add(c_Button);
c_Button.setFont(new Font("粗体",Font.BOLD,16));
c_Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
input_text.setText("");
}
}
);
this.add(jp_north,BorderLayout.NORTH);
}
//Adding central component:
public void addCentreButton(){
String [] txt = {"1","2","3","+","^","4","5","6","-","sin","7","8","9","*","cos","0",".","=","/","tan"};
String reg = "[\\+\\-\\*\\/\\.\\=]";
this.jp_centre.setLayout(new GridLayout(4,5));
for (int i = 0;i<20;i++) {
String temp = txt[i];
JButton button = new JButton();
button.setText(temp);
if(temp.matches(reg)) {
button.setFont(new Font("粗体",Font.BOLD,24));
button.setForeground(Color.BLACK);
}else{
button.setFont(new Font("粗体",Font.BOLD,22));
button.setForeground(Color.GRAY);
}
button.addActionListener(this);
jp_centre.add(button);
}
this.add(jp_centre,BorderLayout.CENTER);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
private String preInput = null;
private String operator = null;
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String clickStr = e.getActionCommand();
if(".0123456789".indexOf(clickStr) != -1) {//if enter the numbers on the calculator
this.input_text.setText(input_text.getText() + clickStr);
this.input_text.setHorizontalAlignment(JTextField.RIGHT);
}else if(clickStr.matches("[\\+\\-\\*\\/\\^]{1}")) {//record the order of plus, minus, multiplication and division.
operator = clickStr;
preInput = this.input_text.getText();
this.input_text.setText("");
}
else if(clickStr.equals("=")&&(operator != "sin"||operator != "cos"||operator != "tan")) {
double preValue = Double.valueOf(preInput);
double result = 0;
boolean flag = true;
String error = null;
switch(operator) {
//doing calculation:
case "+":
double latValue1 = Double.valueOf(this.input_text.getText());
result = preValue + latValue1;
break;
case "-":
double latValue2 = Double.valueOf(this.input_text.getText());
result = preValue -latValue2;
break;
case "*":
double latValue3 = Double.valueOf(this.input_text.getText());
result = preValue*latValue3;
break;
case "/":
double latValue4 = Double.valueOf(this.input_text.getText());
if(latValue4 != 0) {
result = preValue/latValue4;
break;
}else {
flag = false;
error = "Invalid Operation!";
break;
}
case"^":
double latValue5 = Double.valueOf(this.input_text.getText());
result = Math.pow(preValue, latValue5);
break;
}
if(flag)
this.input_text.setText(String.valueOf(result));
else
this.input_text.setText(error);
}
}
}
The Constant Class is below:
package lindan.cn.util;
import java.awt.Toolkit;
public class Const {
public static final int FRAME_W = 300;
public static final int FRAME_H = 400;
public static final int SCREEN_W = Toolkit.getDefaultToolkit().getScreenSize().width;
public static final int SCREEN_H = Toolkit.getDefaultToolkit().getScreenSize().height;
public static final int FRAME_X = (SCREEN_W - FRAME_W)/2;
public static final int FRAME_Y = (SCREEN_H- FRAME_H)/2;
public static final String TITLE = "Calculator";
}
The Github Link of the Repositories of my code is : https://github.com/952609066/Calculator_work.git
Explanation of the Code:
The Const class stores the values for this calculator, which contains the length and width of the calculator, the title of the calculator, etc.
In the main class, I arrange the main platform of the calculator. Then I add buttons on the platform, and add text on each platform(Although I have placed the sin, cos and tan on the button, they cannot actually use since my lack of ability to realize them). Next is the calculation part. In this part the code checks the input of my calculator to decide how to perform next operation. Finally, I realize the computing function and then display the result on the text box.
FlowChart:
This is the flowchart of my project.
Results Display:
演示视频
Summary:
In this project, I have already learned the basic visualization skills on Java eclipse platform, which is a little bit hard for me. In the process of constructing the calculator, I went through some difficluties when coding and writing reports. I tried my best to work out lots of the problems but there are still some problems I failed to solve. For instance, I still fail to realize the function of doing sine,cosine and tangent calculation. Besides, I also learned how to export the project as a software, which is very useful for the practical work. Through this project I improved my software development ability and use Java eclipes platform more skillfully.For now, I still make efforts to improve my code, and try my best to make my project better.