Simple calculator made by JAVA

Simple calculator made by JAVA

1.Introduction

In this blog, we will make a simple calculator using java language. Calculators can add, subtract, multiply, divide, and clear functions. Also learn how to use github repositories to upload code.

2.Persional information

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 Assignmentlearn github and creat a simple caculator
MU STU ID and FZU STU ID<21126844><832102226>
Project complete codehttps://github.com/JiNJiE1/Caculator

3.PSP Table

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning2540
Estimate1515
Development5040
Analysis3050
Design Spec2010
Design Review5020
Coding Standard2015
Design4030
Coding180200
Code Review6060
Test3040
Reporting6080
Sum580600

4. Display

在这里插入图片描述

caculator

5.Learn github

I learned common github commands
请添加图片描述
I then created my own github repository and uploaded some basic files.
请添加图片描述

After that I got the ssh key and cloned my github repository into my local machine so I could upload the code later.
请添加图片描述

6.Code

Thought about the basic functions of the calculator, including addition, subtraction, multiplication, division, and clear functions. I then looked up relevant programming knowledge, including the java language and the swing library.

The computational logic is implemented using java’s standard library and the BigDecimal class.

import java.math.BigDecimal;
import java.math.BigInteger;

public class BigDecimalMath {
    private final int accuracy; 
    private final BigDecimal accuracyNum; 

    private final BigDecimal _105_095; 
    private final BigDecimal log_105_095; // log(1.05/0.95)
    private BigDecimal log10; // log(10)
    private final BigDecimal atan05; // arctan(0.5)
    private final BigDecimal PI2; // PI/2

    // Constants
    private static final BigDecimal PI = new BigDecimal("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679");
    private static final BigDecimal E = new BigDecimal("2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274");

    // Constructor
    public BigDecimalMath(int ac) { // Set up the basic values
        accuracy = ac + 2; // Calculation accuracy is 2 digits higher than the set accuracy

        accuracyNum = BigDecimal.ONE.divide(BigDecimal.TEN.pow(accuracy)); // 1/10^-accuracy
        _105_095 = new BigDecimal("1.05").divide(new BigDecimal("0.95"),accuracy,BigDecimal.ROUND_HALF_EVEN); // Calculate 1.05/0.95
        log_105_095 = log_095_105(_105_095); // Calculate ln(1.05/0.95)
        atan05 = atan(new BigDecimal("0.5")); // Calculate arctan(0.5)
        PI2 = PI.divide(new BigDecimal("2"),accuracy,BigDecimal.ROUND_HALF_EVEN); // Calculate PI/2

        log10 = new BigDecimal("0");
        for(int i=0;i<10;i++){
            log10 = log10.add(log_095_105(new BigDecimal("1.25")));
        }
        log10 = log10.add(log_095_105(new BigDecimal("1.073741824")));
    }

    // Methods
    public BigDecimal atan(BigDecimal x) {
        boolean isMinus = false; // Check if the parameter is negative
        boolean isGt1 = false; // Check if the parameter is greater than 1
        boolean isGt05 = false; // Check if the parameter is greater than 0.5

        if(x.signum() == -1){ // If the parameter is negative, change the sign of the parameter and the result
            x = x.negate();
            isMinus = true;
        }

        if(x.compareTo(BigDecimal.ONE) > 0){ // If the parameter is greater than 1, take the reciprocal
            x = BigDecimal.ONE.divide(x,accuracy,BigDecimal.ROUND_HALF_EVEN);
            isGt1 = true;
        }

        if(x.compareTo(new BigDecimal("0.5")) > 0){ // If the parameter is greater than 0.5, convert it to the form (x-0.5)/(x+0.5)
            BigDecimal fm = x.multiply(new BigDecimal("0.5")).add(BigDecimal.ONE);
            x = x.subtract(new BigDecimal("0.5")).divide(fm,accuracy,BigDecimal.ROUND_HALF_EVEN);
            isGt05 = true;
        }

        BigDecimal res = new BigDecimal("0"); 
        BigDecimal term = new BigDecimal("0"); 
        int i = 0;

        do{ 
            term = x.pow(4*i+1).divide(new BigDecimal(4*i+1),accuracy,BigDecimal.ROUND_HALF_EVEN);
            term = term.subtract(x.pow(4*i+3).divide(new BigDecimal(4*i+3),accuracy,BigDecimal.ROUND_HALF_EVEN));
            res = res.add(term);
            i++;
        }while(term.compareTo(accuracyNum) > 0); 

        if(isGt05){ 
            res = atan05.add(res);
        }
        if(isGt1){ 
            res = PI2.subtract(res);
        }
        if(isMinus){ 
            res = res.negate();
        }

        return res.setScale(accuracy,BigDecimal.ROUND_HALF_EVEN); 
    }

    public BigDecimal acos(BigDecimal x){

        return PI2.subtract(asin(x)).setScale(accuracy,BigDecimal.ROUND_HALF_EVEN); 
    }

    public BigDecimal asin(BigDecimal x){

        boolean isMinus = false;

        if(x.signum() == -1){ 
            x = x.negate();
            isMinus = true;
        }

        if(x.compareTo(BigDecimal.ONE) > 0){ 
            System.out.println("该值无意义,定义域为[-1,1]");
            return BigDecimal.ZERO;
        }
        else if(x.compareTo(BigDecimal.ONE) == 0){ 
            return PI2;
        }

        BigDecimal res = pow(x,new BigDecimal("2"));
        res = BigDecimal.ONE.subtract(res);
        res = pow(res,new BigDecimal("0.5"));
        res = x.divide(res,accuracy,BigDecimal.ROUND_HALF_EVEN);
        res = atan(res);

        return (isMinus ? res.negate() : res).setScale(accuracy,BigDecimal.ROUND_HALF_EVEN);
    }
    public BigDecimal tan(BigDecimal x){
        if(x.abs().compareTo(PI2) >= 0){ 
            System.out.println("该值无意义,定义域为(-PI/2,PI/2)");
            return BigDecimal.ZERO;
        }

        return sin(x).divide(cos(x),accuracy,BigDecimal.ROUND_HALF_EVEN);
    }
    public BigDecimal cos(BigDecimal x){

        boolean isMinus = false;

        x = x.abs();
        int quotient = x.divideToIntegralValue(PI2).intValue(); 

        switch(quotient % 4){ 
            case 1:x = PI.subtract(x);isMinus = true;break; 
            case 2:isMinus = true;break; 
            case 3:x = PI.subtract(x);break; 
        }

        BigDecimal res = new BigDecimal("0");
        BigDecimal term = new BigDecimal("0");
        int i = 0;

        do{ 
            term = x.pow(2*i).divide(fac(2*i),accuracy,BigDecimal.ROUND_HALF_EVEN);
            res = res.add(i%2==1 ? term.negate() : term);
            i++;
        }while(term.compareTo(accuracyNum) > 0);

        return (isMinus ? res.negate() : res).setScale(accuracy,BigDecimal.ROUND_HALF_EVEN);
    }

    public BigDecimal sin(BigDecimal x){

        boolean isMinus = false;

        if(x.compareTo(BigDecimal.ZERO) < 0){
            x = x.negate();
            isMinus = !isMinus;
        }

        int quotient = x.divideToIntegralValue(PI2).intValue();
        x = x.remainder(PI);

        switch(quotient % 4){
            case 1:x = PI.subtract(x);break;
            case 2:isMinus = !isMinus;break;
            case 3:x = PI.subtract(x);isMinus = !isMinus;break;
        }

        BigDecimal res = new BigDecimal("0");
        BigDecimal term = new BigDecimal("0");
        int i = 0;

        do{
            term = x.pow(2*i+1).divide(fac(2*i+1),accuracy,BigDecimal.ROUND_HALF_EVEN);
            res = res.add(i%2==1 ? term.negate() : term);
            i++;
        }while(term.compareTo(accuracyNum) > 0);

        return (isMinus ? res.negate() : res).setScale(accuracy,BigDecimal.ROUND_HALF_EVEN);
    }

    public BigDecimal toRadians(BigDecimal deg){
        deg = deg.divide(new BigDecimal("180"),accuracy,BigDecimal.ROUND_HALF_EVEN);
        return deg.multiply(PI).setScale(accuracy,BigDecimal.ROUND_HALF_EVEN);
    }
}

Test

During testing, I used the following methods:

  1. Enter the test data manually

  2. Use automated testing tools

During manual testing, I entered a variety of different data and checked that the calculations were correct. In the process of automated testing, I used the JUnit framework to write test cases.

Summary

Through this practice of making a simple calculator, I learned a lot of things, but also learned how to build a github repository, but also gained a lot of experience. I hope this blog will help someone in need.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值