Java计算器是一款基于Java语言开发的计算器,可以进行基本的算术运算、取反、求余、开方、平方、阶乘等运算。该计算器的源代码实现方法如下:
一、环境搭建
1.1 JDK环境安装
首先,需要安装JDK(Java Development Kit)环境,在官网上下载并安装JDK。
1.2 Eclipse集成开发环境安装
通过官网下载Eclipse安装包,安装Eclipse集成开发环境,用于编写、编译、执行Java代码。
二、编写源代码
在Eclipse中新建一个Java项目,创建Cal类和CalFrame类,其中Cal类包含计算器的算法和逻辑部分,CalFrame类则负责计算器的图形化界面的设计。
2.1 Cal类
Cal类主要包含计算器的算术运算方法和逻辑控制代码:
``` java
public class Cal{
private String value1;
private String value2;
private String operation;
public Cal(){
value1 = "";
value2 = "";
operation = "";
}
public void input(String string){
if(operation.equals("")){
if(value1.equals("") && string.equals(".")){
value1 = "0.";
}else{
value1 += string;
}
}else{
if(value2.equals("") && string.equals(".")){
value2 = "0.";
}else{
value2 += string;
}
}
}
public void clear(){
value1 = "";
value2 = "";
operation = "";
}
public double getResult(){
double result = 0.0;
double v1 = Double.parseDouble(value1);
double v2 = Double.parseDouble(value2);
switch(operation){
case "+":
result = v1 + v2;
break;
case "-":
result = v1 - v2;
break;
case "*":
result = v1 * v2;
break;
case "/":
if(v2 == 0){
return -1; //除数不能为0
}
result = v1 / v2;
break;
case "%":
if(v2 == 0){
return -1; //除数不能为0
}
result = v1 % v2;
break;
case "sqrt":
if(v1 < 0){
return -1; //负数不能开方
}
result = Math.sqrt(v1);
break;
case "^":
result = Math.pow(v1, v2);
break;
case "sin":
result = Math.sin(v1);
break;
case "cos":
result = Math.cos(v1);
break;
case "tan":
result = Math.tan(v1);
break;
case "log":
result = Math.log10(v1);
break;
case "fac":
if(v1 < 0){
return -1; //负数没有阶乘
}
result = fac(v1);
break;
}
return result;
}
private static double fac(double n){ //阶乘
if(n < 0){
return -1; //负数没有阶乘
}
if(n == 0){
return 1;
}
return n * fac(n - 1);
}
public void setOperation(String op){
operation = op;
}
}
```
2.2 CalFrame类
CalFrame类主要包含计算器的图形化界面设计和事件监听:
``` java
public class CalFrame extends JFrame implements ActionListener{
private JTextField display;
private JButton add, sub, mul, div, sqrt, pow, log, sin, cos, tan, fac, mod, equ, clear, dot, zero, one, two, three, four, five, six, seven, eight, nine;
private Cal cal;
public CalFrame(){
super("Java Calculator");
cal = new Cal();
initView();
addEvent();
}
private void initView(){
getContentPane().setLayout(new BorderLayout());
display = new JTextField("");
display.setHorizontalAlignment(JTextField.RIGHT);
display.setEditable(false);
getContentPane().add(display, BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 5, 3, 3));
clear = new JButton("Clear");
add = new JButton("+");
sub = new JButton("-");
mul = new JButton("*");
div = new JButton("/");
sqrt = new JButton("sqrt");
pow = new JButton("^");
log = new JButton("log");
sin = new JButton("sin");
cos = new JButton("cos");
tan = new JButton("tan");
fac = new JButton("fac");
mod = new JButton("%");
equ = new JButton("=");
dot = new JButton(".");
zero = new JButton("0");
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
panel.add(clear); panel.add(log);
panel.add(sin); panel.add(cos); panel.add(tan);
panel.add(seven); panel.add(eight); panel.add(nine); panel.add(add); panel.add(sqrt);
panel.add(four); panel.add(five); panel.add(six); panel.add(sub); panel.add(pow);
panel.add(one); panel.add(two); panel.add(three); panel.add(mul); panel.add(fac);
panel.add(dot); panel.add(zero); panel.add(mod); panel.add(div); panel.add(equ);
getContentPane().add(panel, BorderLayout.CENTER);
pack();
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
private void addEvent(){
clear.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
sqrt.addActionListener(this);
pow.addActionListener(this);
log.addActionListener(this);
sin.addActionListener(this);
cos.addActionListener(this);
tan.addActionListener(this);
fac.addActionListener(this);
mod.addActionListener(this);
equ.addActionListener(this);
dot.addActionListener(this);
zero.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
}
private void inputNum(String num){
cal.input(num);
display.setText(cal.getResult() + "");
}
private void inputOp(String op){
cal.setOperation(op);
}
public void actionPerformed(ActionEvent e){
String str = e.getActionCommand();
switch(str){
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case ".":
inputNum(str);
break;
case "+":
case "-":
case "*":
case "/":
case "%":
case "^":
case "fac":
case "sqrt":
case "sin":
case "cos":
case "tan":
case "log":
inputOp(str);
break;
case "=":
display.setText(cal.getResult() + "");
cal.clear();
break;
case "Clear":
display.setText("");
cal.clear();
break;
}
}
}
```
三、运行程序
在Eclipse中将CalFrame类设置为主类,然后单击运行按钮,就可以看到计算器的图形化界面了。
四、代码解释
4.1 Cal类方法解释
input(String string)方法
该方法用于输入数字或小数点,并将其保存在value1或value2中。
clear()方法
该方法清空value1、value2和operation变量。
getResult()方法
该方法返回运算结果,包括加、减、乘、除、取余、开方、幂、正弦、余弦、正切、对数和阶乘等运算。
setOperation(String op)方法
该方法用于设置当前的运算符。
4.2 CalFrame类方法解释
initView()方法
该方法用于初始化计算器的图形化界面,在该方法中设置了文本框、按钮等组件的属性和布局。
addEvent()方法
该方法用于添加事件监听器,当按钮被单击时,会回调actionPerformed(ActionEvent e)方法。
actionPerformed(ActionEvent e)方法
该方法用于处理按钮单击事件,根据按钮的标识符执行相应的操作。如果为数字或小数点按钮,则调用inputNum(String num)方法;如果为运算符按钮,则调用inputOp(String op)方法;如果为等号按钮,则返回结果并清空变量;如果为清除按钮,则清空文本框和变量。
五、总结
在本文中,我们详解了Java计算器的源代码实现方法。通过对该计算器的源代码进行分析,可以更好地理解Java语言的基本语法和面向对象编程的思想。借助Eclipse等集成开发环境,开发出更加复杂、功能更加强大的Java应用程序。