Software Engineering Project------Constructing a Simple 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 Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/forums/ssynkqtd-04
The Aim of This AssignmentCreate a Calculator by Visualization
MU STU ID and FZU STU ID21126348_832102223

Work Procedure

PSP

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning2025
• Estimate2025
Development405450
• Analysis3530
• Design Spec4045
• Design Review1515
• Coding Standard1520
• Design90100
• Coding200180
• Code Review3045
• Test1015
Reporting180140
• Test Repor6040
• Size Measurement6040
• Postmortem & Process Improvement Plan6060
Sum605615

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值