Bob Simple Calculator


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 Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617332156
The Aim of This AssignmentCreate 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 StagesEstimated Time(minutes)Actual Time(minutes)
Planning4550
• Estimate4550
Development405450
• Analysis3030
• Design Spec1515
• Design Review3030
• Coding Standard3030
• Design4045
• Coding160200
• Code Review4040
• Test6060
Reporting100110
• Test Repor6065
• Size Measurement1015
• Postmortem & Process Improvement Plan3030
Sum550610

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.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值