面向对象计算器设计与实现文档格式.docx
《面向对象计算器设计与实现文档格式.docx》由会员分享,可在线阅读,更多相关《面向对象计算器设计与实现文档格式.docx(16页珍藏版)》请在冰豆网上搜索。
-128~127
char
0~65535
float
1.4E-45~3.4038335E38
double
4.9E-334~1.7975931348633157E
4.1.3输出的形式
int;
float;
double;
boolean
4.1.4测试数据
a.计算器的正确输入能够使其准确运算(如图)。
b.错误处理需求:
当用户如果按下的是除零的操作,显示框显示“被除数不为0”;
当用户如果多次按下运算符,计算器自动记忆第一次运算符,并且做防连续输入相同的运算符或者不同的运算符,当用户在同一个数据中多次输入小数点,运算器能够自动判断小数点重复,自动记忆第一个小数点,当用户在同一个数据中输入多个负号,计算器自动做重复检查,不显示多余的负号。
4.2总体设计
4.2.1本程序中用到的所有数据类型的定义
byte,short,int,long,float,double,char,boolean
4.2.2主程序的流程
4.2.3各程序模块之间的调用关系
计算器各模块之间的调用关系:
Computer首先通过extends继承JFrame实现对计算器外观的建立,再通过implements实现监听接口ActionListener。
用构造和重载等方式实现计算器的具体运算。
4.3详细设计
4.3.1每个小模块的流程图
a.计算器开方、幂、对数以及求余键设计流程图
b.计算器加减乘除设计流程图
4.3.2关键代码分析与设计
构造一个构造函数进行初始化
doublenum1,num2,num3;
booleanend,add,mul,sub,div,pow,mod,sqrt,log;
构建20个按钮对象
JButtonb1=newJButton("
1"
);
JButtonb2=newJButton("
2"
JButtonb3=newJButton("
3"
JButtonb4=newJButton("
+"
JButtonb5=newJButton("
xy"
JButtonb6=newJButton("
4"
JButtonb7=newJButton("
5"
JButtonb8=newJButton("
6"
JButtonb9=newJButton("
-"
JButtonb10=newJButton("
%"
JButtonb11=newJButton("
7"
JButtonb12=newJButton("
8"
JButtonb13=newJButton("
9"
JButtonb14=newJButton("
*"
JButtonb15=newJButton("
√"
JButtonb16=newJButton("
."
JButtonb17=newJButton("
0"
JButtonb18=newJButton("
="
JButtonb19=newJButton("
/"
JButtonb20=newJButton("
log"
JButtonclc=newJButton("
clear"
构建两个小面板对象p1andp2和p3
JPanelp1=newJPanel();
JPanelp2=newJPanel();
JPanelp3=newJPanel();
publicComputer(){
setTitle("
计算器"
setResizable(false);
setBounds(150,150,330,330);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoderLayout布局,对文本框进行设计
add(p1,"
North"
t.setEditable(false);
设置宽度
t.setColumns(20);
设置文本位置
t.setHorizontalAlignment(JTextField.RIGHT);
p1.add(t);
加入清屏按钮
add(p3,"
South"
p3.add(clc);
加入按钮
GridLayoutgl=newGridLayout(5,0);
gl.setVgap(10);
gl.setHgap(10);
p2.setLayout(gl);
add(p2,"
Center"
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b10);
p2.add(b11);
p2.add(b12);
p2.add(b13);
p2.add(b14);
p2.add(b15);
p2.add(b16);
p2.add(b17);
p2.add(b18);
p2.add(b19);
p2.add(b20);
设置西和东的布局
JLabelll=newJLabel();
ll.setPreferredSize(newDimension(10,0));
add(ll,"
West"
JLabelrl=newJLabel();
rl.setPreferredSize(newDimension(10,0));
add(rl,"
East"
设置是否可见
setVisible(true);
添加监听程序
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);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
b19.addActionListener(this);
b20.addActionListener(this);
clc.addActionListener(this);
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==clc)
t.setText("
"
按键进行响应
if(e.getSource()==b1)
num
(1);
if(e.getSource()==b2)
num
(2);
if(e.getSource()==b3)
num(3);
if(e.getSource()==b6)
num(4);
if(e.getSource()==b7)
num(5);
if(e.getSource()==b8)
num(6);
if(e.getSource()==b11)
num(7);
if(e.getSource()==b12)
num(8);
if(e.getSource()==b13)
num(9);
if(e.getSource()==b17)
num(0);
if(e.getSource()==b4)
sign
(1);
if(e.getSource()==b9)
sign
(2);
if(e.getSource()==b14)
sign(3);
if(e.getSource()==b19)
sign(4);
if(e.getSource()==b5)
sign(5);
if(e.getSource()==b10)
sign(6);
if(e.getSource()==b15)
sign(7);
if(e.getSource()==b20)
sign(8);
if(e.getSource()==b16)
point();
if(e.getSource()==b18)
eql();
publicvoidnum(inti){
Strings=String.valueOf(i);
if(end){//判断操作数输