Simple calculator made by JAVA
Contents
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 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 | learn github and creat a simple caculator |
MU STU ID and FZU STU ID | <21126844><832102226> |
Project complete code | https://github.com/JiNJiE1/Caculator |
3.PSP Table
Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
---|---|---|
Planning | 25 | 40 |
Estimate | 15 | 15 |
Development | 50 | 40 |
Analysis | 30 | 50 |
Design Spec | 20 | 10 |
Design Review | 50 | 20 |
Coding Standard | 20 | 15 |
Design | 40 | 30 |
Coding | 180 | 200 |
Code Review | 60 | 60 |
Test | 30 | 40 |
Reporting | 60 | 80 |
Sum | 580 | 600 |
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:
-
Enter the test data manually
-
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.