目 录
目 录 .....................................................................................................................................................................1 1 需求分析 .......................................................................................................................................................1
1.1 计算器的基本功能: ..............................................................................................................2
1.1.1 加法运算:用数字按钮和“+”按钮进行运算; .....................................................2 1.1.2 减法运算:用数字按钮和“-”按钮进行运算; ......................................................2 1.1.3 乘法运算:用数字按钮和“*”按钮进行运算; ......................................................2 1.1.4 除法运算:用数字按钮和“/”按钮进行运算; ......................................................2 1.2 退格键和清零键: 用”Backspace”和”C”按钮实现; ...........................................2 1.3 计算器的科学计算方法: .....................................................................................................2
1.3.1 开方:用数字按钮和“Sqrt”按钮进行运算; .........................................................2 1.3.2 百分比:用数字按钮和“%”按钮进行运算; ..........................................................2 1.3.3 求倒数:用数字按钮和“1/x”按钮进行运算; ......................................................2
2 设计 .................................................................................................................................................................2
2.1 用户界面设计 ............................................................................................................................2
2.1.1 该计算器程序的设计:用户界面包括Swing组件,不过程序中大都使用的是AWT组件. import java.awt.*; ...................................................................................................3 2.1.2 在AWT组件, ....................................................................................................................3 2.1.3 这个界面设计中包含了两个接口,单击事件接ActionListener口和键盘事件接口(KeyListener). ..................................................................................................4 2.1.4 程序设计中,使用了布局管理: .....................................................................................5 2.2 概要设计 .....................................................................................................................................5
2.2.1 Calculator类中的类名是Calculator.它的功能是使用图形用户来实现计算器的界面设计和运算功能以及一些科算方法. ...............................................................5 2.2.2 main主类.中调用了cal.display来实现计算器的功能 . ...................................5
3 实现 .................................................................................................................................................................5 4 测试 .............................................................................................................................................................. 17
4.1 实现加法运算:4+12=16 .................................................................................................. 18 4.2 实现减法运算:22-11=11 ................................................................................................ 18 4.3 实现乘法运算:3*9=27...................................................................................................... 19 4.4 实现除法运算:/32=2 ................................................................................................... 19 4.5 用”C’实现清零功能: ....................................................................................................... 20 4.6 用”Backspace”实现退格功能: ...................................................................................... 21 4.7 求倒数:1/4=0.25 .................................................................................................................... 21 5 总结和体会 ............................................................................................................... 错误!未定义书签。
资料
.
1 需求分析
(该部分主要阐述所要实现的程序具体具有什么样的功能,要细化,可以用图表作为辅助描述手段)
该计算器程序除了具备加减乘除基本功能外,还有清零键C和退格键Backspace,和一些部分的科学计算方法,包括开方、求倒、百分比,程序里面也写了键盘事件接口,不过由于时间仓促,还没能设计出来,所以该计算器不能实现此功能。
MC MR MS M+
7 4 1 0 Backspace 8 5 2 +/- 9 6 3 . CE / * - + C Sqrt % 1/x = 资料
.
1.1 计算器的基本功能:
1.1.1 加法运算:用数字按钮和“+”按钮进行运算;
1.1.2 减法运算:用数字按钮和“-”按钮进行运算;
1.1.3 乘法运算:用数字按钮和“*”按钮进行运算;
1.1.4 除法运算:用数字按钮和“/”按钮进行运算;
1.2 退格键和清零键: 用”Backspace”和”C”按钮实现;
1.3 计算器的科学计算方法:
1.3.1 开方:用数字按钮和“Sqrt”按钮进行运算;
1.3.2 百分比:用数字按钮和“%”按钮进行运算;
1.3.3 求倒数:用数字按钮和“1/x”按钮进行运算;
2 设计
(该部分主要要说明,在使用Java实现该程序前考虑的内容,主要包括下面两部分:用户界面设计和概要设计(这部分可简单看作是类设计))。
2.1 用户界面设计
(用图或文字阐述你的界面如何设计,如:包括哪些部分,使用什么样的布局管理器等)
资料
.
2.1.1 该计算器程序的设计:用户界面包括Swing组件,不过程序中大都使用的
是AWT组件. import java.awt.*;
import java.awt.event.*; import javax.swing.*;
2.1.2 在AWT组件,
(1) 使用了面板和按钮:
Panel p1,p2,p3,p4,p5,p6;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
Button
bDiv,bSqrt,bMulti,bMinus,bPercent,bPlus,bReciprocal,bEqual,bDot,bNegative; Button bBackspace,bCE,bC,bMR,bMS,bMC,bM;
(2) 界面设计也包括了AWT的委托事件模型,该程序设计在java.awt.event包中定义了窗口事件类
public void windowClosing(WindowEvent e) {
System.exit(0); }
单击事件类.
public void actionPerformed(ActionEvent e) {
//key 0 to 9
if(this.keyAvailable && e.getActionCommand().length()==1 e.getActionCommand().compareTo(\"0\")>=0 e.getActionCommand().compareTo(\"9\")<=0) {
if(this.isTempNowInput) {
this.dNowInput=0;
this.isTempNowInput=false; }
this.nBitsNum++;
if(this.alreadyHaveDot==false)
this.dNowInput=this.dNowInput*10+Double.parseDouble(e.getActionCommand()); else {
double temp=Double.parseDouble(e.getActionCommand()); for(int i=this.n;i<0;i++) {
&& &&
资料
.
temp*=0.1; }
this.dNowInput+=temp; this.n--; }
this.tf1.setText(Double.toString(this.dNowInput)); }
在程序中也注册了事件,里面包含了事件处理方法. /*add(b1);
add(b2); add(b3); add(b4); add(b5); add(b6); add(b7); add(b8); add(b9); add(b0);*/
b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); b0.addActionListener(this);
2.1.3 这个界面设计中包含了两个接口,单击事件接ActionListener口和键
盘事件接口(KeyListener).
public class Calculator extends WindowAdapter implements ActionListener, KeyListener
资料
.
2.1.4 程序设计中,使用了布局管理:
(1) 用流布局管理器(FlowLayout)设置面板
p4=new Panel(new FlowLayout());
p5=new Panel(new FlowLayout()); p6=new Panel(new FlowLayout());
(2) 用边布局管理器(BorderLayout)设置计算器容器北西组件的大小: f.setLayout(new BorderLayout(4,4)); f.add(p5,BorderLayout.NORTH); `f.add(p4,BorderLayout.CENTER); f.add(p3,BorderLayout.WEST);
(3) 用网格布局管理器(GridLayout)设置面板
p1=new Panel(new GridLayout(1,3,5,5));
p2=new Panel(new GridLayout(4,5,5,5)); p3=new Panel(new GridLayout(5,1,5,5));
2.2 概要设计
该部分主要阐述整个程序包括哪些类,各个类的类名、功能,以及各类中具有什么样的public成员方法(方法访问修饰符、返回值类型、名字、参数列表、方法的功能),以及这些类的对象之间有什么样的关系(或类和类之间有什么关系,即,函数调用关系)。
计算器的整个程序包括:Calculator类和一个main主类.
2.2.1 Calculator类中的类名是Calculator.它的功能是使用图形用户来实现计
算器的界面设计和运算功能以及一些科算方法.
(1) 在Calculator类中具有设置计算器界面布局和颜色的成员方法,使用了两个接口单
击事件接ActionListener口和键盘事件接口(KeyListener). (2) 返回值类型是布尔类型.
2.2.2 main主类.中调用了cal.display来实现计算器的功能 .
3 实现
(程序的实现代码)
import java.awt.*;
import java.awt.event.*; import javax.swing.*;
public class Calculator extends WindowAdapter implements ActionListener, KeyListener
资料
.
{
double dResult=0; double dNowInput=0; double dMemory; int n=0;
int nOperation=1; int nBitsNum=0; char ch;
boolean alreadyHaveDot=false; boolean keyAvailable=true;
boolean alreadyClickedEqueal=false; boolean isTempNowInput=false;
JFrame f;
Panel p1,p2,p3,p4,p5,p6; TextField tf1,tf2;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0; Button
bDiv,bSqrt,bMulti,bMinus,bPercent,bPlus,bReciprocal,bEqual,bDot,bNegative; Button bBackspace,bCE,bC,bMR,bMS,bMC,bM;
public void display() {
f=new JFrame(\"Calculator\"); f.setForeground(Color.BLUE); f.setSize(290,223); f.setLocation(220,220);
f.setBackground(Color.PINK); f.setResizable(false);
f.setLayout(new BorderLayout(4,4));
p1=new Panel(new GridLayout(1,3,5,5)); p2=new Panel(new GridLayout(4,5,5,5)); p3=new Panel(new GridLayout(5,1,5,5)); p4=new Panel(new FlowLayout()); p5=new Panel(new FlowLayout()); p6=new Panel(new FlowLayout()); p4.add(p1); p4.add(p2);
tf1=new TextField(35); tf1.setText(\"0.\");
tf1.setEditable(false);
资料
.
p5.add(tf1);
f.add(p5,BorderLayout.NORTH); f.add(p4,BorderLayout.CENTER); f.add(p3,BorderLayout.WEST); tf1.addKeyListener(this);
b1=new Button(\"1\"); b2=new Button(\"2\"); b3=new Button(\"3\"); b4=new Button(\"4\"); b5=new Button(\"5\"); b6=new Button(\"6\"); b7=new Button(\"7\"); b8=new Button(\"8\"); b9=new Button(\"9\"); b0=new Button(\"0\");
b1.setForeground(Color.BLUE); b2.setForeground(Color.BLUE); b3.setForeground(Color.BLUE); b4.setForeground(Color.BLUE); b5.setForeground(Color.BLUE); b6.setForeground(Color.BLUE); b7.setForeground(Color.BLUE); b8.setForeground(Color.BLUE); b9.setForeground(Color.BLUE); b0.setForeground(Color.BLUE);
/*add(b1); add(b2); add(b3); add(b4); add(b5); add(b6); add(b7); add(b8); add(b9); add(b0);*/
b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this);
资料
.
b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); b0.addActionListener(this); b1.addKeyListener(this); b2.addKeyListener(this); b3.addKeyListener(this); b4.addKeyListener(this); b5.addKeyListener(this); b6.addKeyListener(this); b7.addKeyListener(this); b8.addKeyListener(this); b9.addKeyListener(this); b0.addKeyListener(this);
bDiv=new Button(\"/\"); bSqrt=new Button(\"sqrt\"); bMulti=new Button(\"*\"); bMinus=new Button(\"-\"); bPercent=new Button(\"%\"); bPlus=new Button(\"+\");
bReciprocal=new Button(\"1/x\"); bEqual=new Button(\"=\"); bDot=new Button(\".\");
bNegative=new Button(\"+/-\"); bDiv.setForeground(Color.RED); bSqrt.setForeground(Color.RED); bMulti.setForeground(Color.RED); bMinus.setForeground(Color.RED); bPercent.setForeground(Color.RED); bPlus.setForeground(Color.RED);
bReciprocal.setForeground(Color.RED); bEqual.setForeground(Color.RED); bDot.setForeground(Color.RED);
bNegative.setForeground(Color.RED);
/*add(bDiv); add(bSqrt); add(bMulti); add(bMinus); add(bPercent); add(bPlus);
资料
.
add(bReciprocal); add(bEqual); add(bDot);
add(bNegative);*/
bDiv.addActionListener(this); bSqrt.addActionListener(this); bMulti.addActionListener(this); bMinus.addActionListener(this); bPercent.addActionListener(this); bPlus.addActionListener(this);
bReciprocal.addActionListener(this); bEqual.addActionListener(this); bDot.addActionListener(this);
bNegative.addActionListener(this); bDiv.addKeyListener(this); bSqrt.addKeyListener(this); bMulti.addKeyListener(this); bMinus.addKeyListener(this); bPercent.addKeyListener(this); bPlus.addActionListener(this); bReciprocal.addKeyListener(this); bEqual.addKeyListener(this); bDot.addKeyListener(this);
bNegative.addKeyListener(this);
p2.add(b7); p2.add(b8); p2.add(b9); p2.add(bDiv); p2.add(bSqrt); p2.add(b4); p2.add(b5); p2.add(b6); p2.add(bMulti); p2.add(bPercent); p2.add(b1); p2.add(b2); p2.add(b3);
p2.add(bMinus); p2.add(bReciprocal); p2.add(b0);
p2.add(bNegative); p2.add(bDot);
资料
.
p2.add(bPlus); p2.add(bEqual);
bBackspace=new Button(\"Backspace\"); bCE=new Button(\"CE\"); bC=new Button(\"C\");
bBackspace.setForeground(Color.GREEN); bCE.setForeground(Color.BLACK); bC.setForeground(Color.BLACK);
/*add(bBackspace); add(bCE); add(bC);*/
bBackspace.addActionListener(this); bCE.addActionListener(this); bC.addActionListener(this);
bBackspace.addKeyListener(this); bCE.addKeyListener(this); bC.addKeyListener(this);
p1.add(bBackspace); p1.add(bCE); p1.add(bC);
tf2=new TextField(2); tf2.setEnabled(false);
tf2.setBackground(Color.PINK); bMC=new Button(\"MC\"); bMR=new Button(\"MR\"); bMS=new Button(\"MS\"); bM=new Button(\"M+\");
bMC.setForeground(Color.BLUE); bMR.setForeground(Color.BLUE); bMS.setForeground(Color.BLUE); bM.setForeground(Color.BLUE); tf2.addKeyListener(this);
/*add(MC); add(MR); add(MS); add(M);*/
bMC.addActionListener(this); bMR.addActionListener(this);
资料
.
bMS.addActionListener(this); bM.addActionListener(this); bMC.addKeyListener(this); bMR.addKeyListener(this); bMS.addKeyListener(this); bM.addKeyListener(this);
p6.add(tf2); p3.add(p6); p3.add(bMC); p3.add(bMR); p3.add(bMS); p3.add(bM);
f.setVisible(true);
f.addWindowListener(this); }
public void actionPerformed(ActionEvent e) {
//key 0 to 9
if(this.keyAvailable && e.getActionCommand().length()==1 && e.getActionCommand().compareTo(\"0\")>=0 && e.getActionCommand().compareTo(\"9\")<=0) {
if(this.isTempNowInput) {
this.dNowInput=0;
this.isTempNowInput=false; }
this.nBitsNum++;
if(this.alreadyHaveDot==false)
this.dNowInput=this.dNowInput*10+Double.parseDouble(e.getActionCommand()); else {
double temp=Double.parseDouble(e.getActionCommand()); for(int i=this.n;i<0;i++) {
temp*=0.1; }
this.dNowInput+=temp; this.n--;
资料
.
}
this.tf1.setText(Double.toString(this.dNowInput)); }
// key dot
if(this.keyAvailable && e.getActionCommand()==\".\") {
if(this.alreadyHaveDot==false) {
this.nBitsNum++;
this.alreadyHaveDot=true; this.n=-1; } }
//key \"+\ if(this.keyAvailable && e.getActionCommand()==\"+\" e.getActionCommand()==\"-\" || e.getActionCommand()==\"*\" e.getActionCommand()==\"/\") {
if(this.alreadyClickedEqueal) {
this.dNowInput=this.dResult; this.isTempNowInput=true; } else {
switch(this.nOperation) {
case 1: this.dResult+=this.dNowInput; break; case 2: this.dResult-=this.dNowInput; break; case 3: this.dResult*=this.dNowInput; break; case 4: {
if(this.dNowInput==0) {
tf1.setText(\"除数不能为零\"); this.keyAvailable=false;
} else this.dResult=this.dResult/this.dNowInput; } }
if(this.keyAvailable)tf1.setText(Double.toString(this.dResult)); this.dNowInput=0; }
if(e.getActionCommand()==\"+\") {
|| ||
资料
.
this.nOperation=1; }
if(e.getActionCommand()==\"-\") {
this.nOperation=2; }
if(e.getActionCommand()==\"*\") {
this.nOperation=3; }
if(e.getActionCommand()==\"/\") {
this.nOperation=4; }
this.nBitsNum=0;
this.alreadyClickedEqueal=false; }
// key \"+/-\"
if(this.keyAvailable && e.getActionCommand()==\"+/-\") {
this.dNowInput=0-this.dNowInput;
tf1.setText(Double.toString(this.dNowInput)); }
// key \"C\"
if(e.getActionCommand()==\"C\") {
this.nBitsNum=0; this.dResult=0; this.dNowInput=0;
this.alreadyHaveDot=false; this.n=0;
this.nOperation=1; this.keyAvailable=true;
this.alreadyClickedEqueal=false; tf1.setText(\"0.\"); {ch='C';} }
// key \"CE\"
if(e.getActionCommand()==\"CE\") {
this.nBitsNum=0; this.dNowInput=0;
this.alreadyHaveDot=false; this.n=0;
资料
.
this.nOperation=1; this.keyAvailable=true; tf1.setText(\"0.\"); }
// key \"sqrt\"
if(this.keyAvailable && e.getActionCommand()==\"sqrt\") {
if(this.alreadyClickedEqueal) {
if(this.dResult>=0) {
this.dResult=Math.sqrt(this.dResult);
tf1.setText(Double.toString(this.dResult)); }
else {
tf1.setText(\"函数输入无效\"); this.keyAvailable=false; } } else {
if(this.dNowInput>=0) {
this.dNowInput=Math.sqrt(this.dNowInput); tf1.setText(Double.toString(this.dNowInput)); } else {
tf1.setText(\"函数输入无效\"); this.keyAvailable=false; } } }
// key \"1/x\"
if(this.keyAvailable && e.getActionCommand()==\"1/x\") {
if(this.dNowInput==0) {
tf1.setText(\"除数不能为零\"); this.keyAvailable=false; } else
资料
.
{
this.dNowInput=1/this.dNowInput;
tf1.setText(Double.toString(this.dNowInput)); } }
// key \"=\"
if(this.keyAvailable && e.getActionCommand()==\"=\") {
this.alreadyClickedEqueal=true; switch(this.nOperation) {
case 1: this.dResult+=this.dNowInput; break; case 2: this.dResult-=this.dNowInput; break; case 3: this.dResult*=this.dNowInput; break; case 4: {
if(this.dNowInput==0) {
tf1.setText(\"除数不能为零\"); this.keyAvailable=false;
} else this.dResult=this.dResult/this.dNowInput; } }
if(this.keyAvailable)tf1.setText(Double.toString(this.dResult)); }
// key \"MS\"
if(this.keyAvailable && e.getActionCommand()==\"MS\") {
this.dMemory=this.dNowInput; if(this.dMemory!=0) tf2.setText(\"M\"); }
// key \"MC\"
if(this.keyAvailable && e.getActionCommand()==\"MC\") {
this.dMemory=0; tf2.setText(\"\"); }
// key \"MR\"
if(this.keyAvailable && e.getActionCommand()==\"MR\") {
this.dNowInput=this.dMemory;
tf1.setText(Double.toString(this.dNowInput)); }
资料
.
// key \"M+\"
if(this.keyAvailable && e.getActionCommand()==\"M+\") {
this.dMemory+=this.dNowInput; if(this.dMemory!=0) tf2.setText(\"M\"); else tf2.setText(\"\"); }
// key \"%\"
if(this.keyAvailable && e.getActionCommand()==\"%\") {
this.dNowInput=(this.dResult*this.dNowInput)/100; tf1.setText(Double.toString(this.dNowInput)); }
// key \"Backspace\"
if(this.keyAvailable && e.getActionCommand()==\"Backspace\") {
if(!this.alreadyClickedEqueal) {
if(this.dNowInput!=0) {
if(this.alreadyHaveDot) {
if(this.n==-1) {
this.alreadyHaveDot=false; this.n=0; } else {
String str,str1; str=tf1.getText();
str1=str.substring(0,this.nBitsNum-1); this.nBitsNum--; this.n++;
this.dNowInput=Double.parseDouble(str1); tf1.setText(Double.toString(this.dNowInput)); } } else {
int temp;
temp=(int)(this.dNowInput/10); this.dNowInput=(double)temp;
资料
.
tf1.setText(Double.toString(this.dNowInput)); } } } } }
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {
char ch=e.getKeyChar(); System.out.println(ch+\" \"); }
public void processEvent(char ch) {}
public static void main(String args[]) {
Calculator cal=new Calculator(); cal.display(); }
public void windowClosing(WindowEvent e) {
System.exit(0); } }
4 测试
(针对需求分析部分提出的功能和要求进行对应的一项项的测试,并给出测试内容和测试结果)
资料
.
4.1 实
现加法运算:
4+12=16
4.2 实
现
减
法
运
算
:
22-11=11
资料
.
4.3 实
现乘法运算:
3*9=2
4.4 实
现
除
法
运
算
:
/32=2
资料
.
4.5 用”C’实现清零功
能:
资料
.
4.6 用”Backspace”实现退格功
能:
4.7 求
倒
数:1/4=0.25
资料
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- haog.cn 版权所有 赣ICP备2024042798号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务