Catalogue
I.Introduction
Here is a simple calculator about Assignment 1 on Software Engineering from Zhang Qin (BOB), which covers simple operations such as addition, subtraction, multiplication and division.
The Link Your Class | https://bbs.csdn.net/forums/ssynkqtd-04 |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/617332156 |
The Aim of This Assignment | Create a simple calculator based on visual interface |
MU STU ID and FZU STU ID | <21126101(MU)_832102212(FZU)> |
My Github Link: https://github.com/ZHANGQINBOB/Calculator.git
II.PSP form
Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
---|---|---|
Planning | 45 | 50 |
• Estimate | 45 | 50 |
Development | 405 | 450 |
• Analysis | 30 | 30 |
• Design Spec | 15 | 15 |
• Design Review | 30 | 30 |
• Coding Standard | 30 | 30 |
• Design | 40 | 45 |
• Coding | 160 | 200 |
• Code Review | 40 | 40 |
• Test | 60 | 60 |
Reporting | 100 | 110 |
• Test Repor | 60 | 65 |
• Size Measurement | 10 | 15 |
• Postmortem & Process Improvement Plan | 30 | 30 |
Sum | 550 | 610 |
III.Problem-solving ideas
1. How to think about the question
In this assignment, we need to consider how to design the user interface, the way the data is handled and the various types of mathematical operations.
For the user interface, our calculator should have a keyboard to provide visual input for the user to enter the data that the user wants to calculate and also a window to display the answer i.e. the output of the calculation.
In terms of data processing method, we need to process the input i.e. calculation and then display the result as output.
Finally, the calculator should have different functions to calculate the data.
2. Find the information
As I have learnt JAVA programming before, I tried to create a calculator using Android, and in the process I checked many tutorials on CSDN to help me build the user interface as well as the implementation of the calculator algorithms.
IV.Design and implementation process
1.Create a new project and files
I used the Android compiler, created a project about my calculator and configured the java environment. Created a user interface file, calculator various types of algorithm java file and main activity to set the click event.
2.Code Demonstration
Interface XML File:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/et_output"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="60dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<Button
android:id="@+id/bt_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:background="@drawable/selector_bt_grey"
android:text="7" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"
android:background="@drawable/selector_bt_grey"
android:id="@+id/bt_8"
android:text="8" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"
android:background="@drawable/selector_bt_grey"
android:id="@+id/bt_9"
android:text="9" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_bt"
android:textSize="20dp"
android:id="@+id/bt_add"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_bt_grey"
android:textSize="20dp"
android:id="@+id/bt_4"
android:text="4" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"
android:background="@drawable/selector_bt_grey"
android:id="@+id/bt_5"
android:text="5" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"
android:background="@drawable/selector_bt_grey"
android:id="@+id/bt_6"
android:text="6" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"
android:background="@drawable/selector_bt"
android:text="-"
android:id="@+id/bt_minus" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_bt_grey"
android:textSize="20dp"
android:id="@+id/bt_1"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_bt_grey"
android:textSize="20dp"
android:id="@+id/bt_2"
android:text="2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_weight="1"
android:background="@drawable/selector_bt_grey"
android:id="@+id/bt_3"
android:text="3" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_bt"
android:textSize="20dp"
android:text="*"
android:id="@+id/bt_multiple"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_bt_grey"
android:textSize="20dp"
android:id="@+id/bt_0"
android:text="0" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_bt"
android:textSize="20dp"
android:id="@+id/bt_ac"
android:text="AC" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_bt"
android:textSize="20dp"
android:id="@+id/bt_equal"
android:text="=" />
<Button
android:id="@+id/bt_divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_bt"
android:text="÷"
android:textSize="20dp" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector_bt"
android:textSize="20dp"
android:id="@+id/bt_delete"
android:text="←" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</layout>
Comment:
I constructed two text boxes at the top of the calculator, the top as the input numbers and operators, and the bottom as the output numerical results.
Next is the keyboard part, the keyboard part will be planned into four lines, and then the buttons are equally divided in each line. Buttons are divided into numbers 0-9, “+”, “-”, “*”, “÷” = ", “AC”, “←”.
Types of Algorithm Java File:
package com.example.myapplication;
public class Calculator {
public static double calculate(String expression) {
String[] elements = expression.split(" ");
double result = Double.parseDouble(elements[0]);
for (int i = 1; i < elements.length; i+=2) {
String operator = elements[i];
double num = Double.parseDouble(elements[i + 1]);
switch (operator) {
case "+":
result += num;
break;
case "-":
result -= num;
break;
case "*":
result *= num;
break;
case "÷":
result /= num;
break;
default:
throw new IllegalArgumentException("error operation");
}
}
return result;
}
}
Comment:
This code implements the calculation process of various functions of the Calculate button in the calculator, covering addition, subtraction, multiplication, division and other calculation laws.
Main Activity Java File:
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import com.example.myapplication.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ActivityMainBinding binding;
String input = "";
String output = "";
boolean canOpe = true;//刚刚是否用了加减乘除
boolean ifEqual = false;//是否点了=
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
initView();
binding.bt0.setOnClickListener(this);
binding.bt1.setOnClickListener(this);
binding.bt2.setOnClickListener(this);
binding.bt3.setOnClickListener(this);
binding.bt4.setOnClickListener(this);
binding.bt5.setOnClickListener(this);
binding.bt6.setOnClickListener(this);
binding.bt7.setOnClickListener(this);
binding.bt8.setOnClickListener(this);
binding.bt9.setOnClickListener(this);
binding.btAdd.setOnClickListener(this);
binding.btMinus.setOnClickListener(this);
binding.btMultiple.setOnClickListener(this);
binding.btDivide.setOnClickListener(this);
binding.btAc.setOnClickListener(this);
binding.btEqual.setOnClickListener(this);
binding.btDelete.setOnClickListener(this);
}
private void initView() {
input = binding.etInput.getText().toString();
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_0:
input += "0";
canOpe = true;
break;
case R.id.bt_1:
canOpe = true;
input += "1";
break;
case R.id.bt_2:
canOpe = true;
input += "2";
break;
case R.id.bt_3:
canOpe = true;
input += "3";
break;
case R.id.bt_4:
canOpe = true;
input += "4";
break;
case R.id.bt_5:
canOpe = true;
input += "5";
break;
case R.id.bt_6:
canOpe = true;
input += "6";
break;
case R.id.bt_7:
canOpe = true;
input += "7";
break;
case R.id.bt_8:
canOpe = true;
input += "8";
break;
case R.id.bt_9:
canOpe = true;
input += "9";
break;
case R.id.bt_add:
if (canOpe) {
input += " + ";
canOpe = false;
}
break;
case R.id.bt_minus:
if (canOpe) {
canOpe = false;
input += " - ";
}
break;
case R.id.bt_multiple:
if (canOpe) {
canOpe = false;
input += " * ";
}
break;
case R.id.bt_divide:
if (canOpe) {
canOpe = false;
input += " ÷ ";
}
break;
case R.id.bt_ac:
input = "";
break;
case R.id.bt_equal:
output = String.valueOf(Calculator.calculate(input));
binding.etOutput.setText(output);
ifEqual = true;
break;
case R.id.bt_delete:
if (' ' == input.charAt(input.length() - 1)) {
input = input.substring(0, input.length() - 3);
canOpe = true;
} else {
input = input.substring(0, input.length() - 1);
}
break;
}
if (ifEqual) {
binding.etInput.setText(input);
input = "";
ifEqual = false;
} else {
binding.etInput.setText(input);
}
}
}
Comment:
This code implements the setting of click events for each key of the calculator. For example, we can’t keep repeating the operator continuously; when we tap “=”, it makes the calculator perform the calculation and get the result, and so on.
V.User Interface Demonstration
Comment:
There are two display areas at the top the top end provides a place for the user to enter numbers and operators, and the second area serves as the output area for the results of the operations.
VI.Calculator Operation Demonstration
Calculator
VII.Flow Chat
Comment:
First, the user enters the number to be calculated and the algorithm for the calculation.
Then, the calculator then carries out data processing operations.
Finally, checking is carried out, and if the output result meets the requirements, the result data is obtained, the display shows the result data on the output interface, and finally it returns to the input procedure to wait for the new calculation requirements.
If it does not meet the requirements of the algorithm or there is an error in the calculation result, then it returns to the input function again and performs a new calculation again.
VIII.Summary
Through this project, I have gained an understanding of how to make a simple calculator and have practised making it.
I also learnt how to create a web calculator using front-end and back-end knowledge, and how to design the user interface and processing logic.
The user interface and simple calculation algorithms can be written by myself, but I need to borrow from others’ blogs again for other higher maths functions and more complex functions.
In the future I will be adding and implementing more features to the calculator. I also intend to make the UI of the calculator better, optimise the calculator and improve the overall requirements.