用JAVA编写的计算器程序设计报告Word下载.docx

上传人:b****5 文档编号:18883789 上传时间:2023-01-02 格式:DOCX 页数:25 大小:221.53KB
下载 相关 举报
用JAVA编写的计算器程序设计报告Word下载.docx_第1页
第1页 / 共25页
用JAVA编写的计算器程序设计报告Word下载.docx_第2页
第2页 / 共25页
用JAVA编写的计算器程序设计报告Word下载.docx_第3页
第3页 / 共25页
用JAVA编写的计算器程序设计报告Word下载.docx_第4页
第4页 / 共25页
用JAVA编写的计算器程序设计报告Word下载.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

用JAVA编写的计算器程序设计报告Word下载.docx

《用JAVA编写的计算器程序设计报告Word下载.docx》由会员分享,可在线阅读,更多相关《用JAVA编写的计算器程序设计报告Word下载.docx(25页珍藏版)》请在冰豆网上搜索。

用JAVA编写的计算器程序设计报告Word下载.docx

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组件.importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

2.1.2在AWT组件,

(1)使用了面板和按钮:

Panelp1,p2,p3,p4,p5,p6;

Buttonb1,b2,b3,b4,b5,b6,b7,b8,b9,b0;

ButtonbDiv,bSqrt,bMulti,bMinus,bPercent,bPlus,bReciprocal,bEqual,bDot,bNegative;

ButtonbBackspace,bCE,bC,bMR,bMS,bMC,bM;

(2)界面设计也包括了AWT的委托事件模型,该程序设计在java.awt.event包中定义了窗口事件类

publicvoidwindowClosing(WindowEvente)

{

System.exit(0);

}

单击事件类.

publicvoidactionPerformed(ActionEvente)

{

//key0to9

if(this.keyAvailable&

&

e.getActionCommand().length()==1&

e.getActionCommand().compareTo("

0"

)>

=0&

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

doubletemp=Double.parseDouble(e.getActionCommand());

for(inti=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).

publicclassCalculatorextendsWindowAdapterimplementsActionListener,KeyListener

2.1.4程序设计中,使用了布局管理:

(1)用流布局管理器(FlowLayout)设置面板

p4=newPanel(newFlowLayout());

p5=newPanel(newFlowLayout());

p6=newPanel(newFlowLayout());

(2)用边布局管理器(BorderLayout)设置计算器容器北西组件的大小:

f.setLayout(newBorderLayout(4,4));

f.add(p5,BorderLayout.NORTH);

`f.add(p4,BorderLayout.CENTER);

f.add(p3,BorderLayout.WEST);

(3)用网格布局管理器(GridLayout)设置面板

p1=newPanel(newGridLayout(1,3,5,5));

p2=newPanel(newGridLayout(4,5,5,5));

p3=newPanel(newGridLayout(5,1,5,5));

2.2概要设计

该部分主要阐述整个程序包括哪些类,各个类的类名、功能,以及各类中具有什么样的public成员方法(方法访问修饰符、返回值类型、名字、参数列表、方法的功能),以及这些类的对象之间有什么样的关系(或类和类之间有什么关系,即,函数调用关系)。

计算器的整个程序包括:

Calculator类和一个main主类.

2.2.1Calculator类中的类名是Calculator.它的功能是使用图形用户来实现计算器的界面设计和运算功能以及一些科学运算方法.

(1)在Calculator类中具有设置计算器界面布局和颜色的成员方法,使用了两个接口单击事件监听器接ActionListener口和键盘事件监听器接口(KeyListener).

(2)返回值类型是布尔类型.

2.2.2main主类.中调用了cal.display来实现计算器的功能.

3实现

(程序的实现代码)

importjava.awt.*;

publicclassCalculatorextendsWindowAdapterimplementsActionListener,KeyListener

{

doubledResult=0;

doubledNowInput=0;

doubledMemory;

intn=0;

intnOperation=1;

intnBitsNum=0;

charch;

booleanalreadyHaveDot=false;

booleankeyAvailable=true;

booleanalreadyClickedEqueal=false;

booleanisTempNowInput=false;

JFramef;

TextFieldtf1,tf2;

Buttonb1,b2,b3,b4,b5,b6,b7,b8,b9,b0;

ButtonbDiv,bSqrt,bMulti,bMinus,bPercent,bPlus,bReciprocal,bEqual,bDot,bNegative;

ButtonbBackspace,bCE,bC,bMR,bMS,bMC,bM;

publicvoiddisplay()

f=newJFrame("

Calculator"

);

f.setForeground(Color.BLUE);

f.setSize(290,223);

f.setLocation(220,220);

f.setBackground(Color.PINK);

f.setResizable(false);

p1=newPanel(newGridLayout(1,3,5,5));

p2=newPanel(newGridLayout(4,5,5,5));

p3=newPanel(newGridLayout(5,1,5,5));

p4=newPanel(newFlowLayout());

p5=newPanel(newFlowLayout());

p6=newPanel(newFlowLayout());

p4.add(p1);

p4.add(p2);

tf1=newTextField(35);

tf1.setText("

0."

tf1.setEditable(false);

p5.add(tf1);

f.add(p4,BorderLayout.CENTER);

f.add(p3,BorderLayout.WEST);

tf1.addKeyListener(this);

b1=newButton("

1"

b2=newButton("

2"

b3=newButton("

3"

b4=newButton("

4"

b5=newButton("

5"

b6=newButton("

6"

b7=newButton("

7"

b8=newButton("

8"

b9=newButton("

b0=newButton("

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);

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=newButton("

/"

bSqrt=newButton("

sqrt"

bMulti=newButton("

*"

bMinus=newButton("

-"

bPercent=newButton("

%"

bPlus=newButton("

+"

bReciprocal=newButton("

1/x"

bEqual=newButton("

="

bDot=newButton("

."

bNegative=newButton("

+/-"

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);

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=newButton("

Backspace"

bCE=newButton("

CE"

bC=newButton("

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=newTextField

(2);

tf2.setEnabled(false);

tf2.setBackground(Color.PINK);

bMC=newButton("

MC"

bMR=newButton("

MR"

bMS=newButton("

MS"

bM=newButton("

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);

publicvoidactionPerformed(ActionEvente)

//keydot

e.getActionCommand()=="

if(this.alreadyHaveDot==false)

this.alreadyHaveDot=true;

this.n=-1;

//key"

"

||e.getActionCommand()=="

){

if(this.alreadyClickedEqueal)

this.dNowInput=this.dResult;

this.isTe

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1