Java计算器源码(只实现了加减乘除四种简单的运算):
1 import java.math.BigDecimal;
2 import java.text.Format;
3 public class Caculator {
4 private BigDecimal num1;
5 private BigDecimal num2;
6 private String result;
7
8 public void setNum1(double num1) {
9     this.num1 = new BigDecimal(num1);
10 }
11
12 public void setNum2(double num2) {
13     this.num2 = new BigDecimal(num2);
14 }
15 private double _format(){
16     BigDecimal _num1 = new BigDecimal(result);
17     BigDecimal _num2 = new BigDecimal(1);
18     return _num1.divide(_num2, 5,BigDecimal.ROUND_HALF_UP).doubleValue();
19 }
20 public String getResult() {
21     if(!result.equals("ERROR"))
22     {
23         double _res = _format();
24         if(_res>=Double.MAX_VALUE||_res<=-Double.MAX_VALUE)
25             result = "ERROR";
26         else
27             result = Double.toString(_format());
28     }
29     return result;
30 }
31 public void Add() {
32     BigDecimal _result;
33     _result = num1.add(num2);
34     result = _result.toString();
35 }
36 public void Sub() {
37     BigDecimal _result;
38     _result = num1.subtract(num2);
39     result = _result.toString();
40 }
41 public void Mul() {
42     BigDecimal _result;
43     _result = num1.multiply(num2);
44     result = _result.toString();
45 }
46 public void Div() {
47     BigDecimal _result;
48     if(num2.doubleValue()!=0){
49         _result = num1.divide(num2);
50         result = _result.toString();
51     }
52     else
53         result = "ERROR";
54     }
55     }
  设计测试用例: