JAVA课程设计任务书科学计算器.docx
- 文档编号:14478629
- 上传时间:2023-06-23
- 格式:DOCX
- 页数:20
- 大小:95.22KB
JAVA课程设计任务书科学计算器.docx
《JAVA课程设计任务书科学计算器.docx》由会员分享,可在线阅读,更多相关《JAVA课程设计任务书科学计算器.docx(20页珍藏版)》请在冰点文库上搜索。
JAVA课程设计任务书科学计算器
《高级语言程序设计
(2)》课程设计报告
专业:
班级:
学号:
姓名:
完成日期:
指导教师:
1、程序设计说明书
【设计题目】科学计算器
【问题描述】本程序为设计一个科学计算器,用于进行数值计算。
可以进行数据的加减乘除,对数等计算。
【软件功能】
1图形界面:
应用AWT和Swing设计工作界面,按下数字时显示数字,按下“=”时显示结果。
2计算器基本功能:
加减乘除四则运算,倒数,开平方等。
3附加运算功能:
三角函数,对数,阶乘,度转化为弧度等。
4计算器的存储功能:
清除中间结果,使用中间结果,存储中间结果及累加中间结果
5用不同颜色进行分区
【算法思想】
本程序设计一个主类和四个内部类。
通过在主类中调用四个类来实现程序的各个功能。
【类的设计】
1主类SmartCalculator类,继承了JFrame类
成员变量
tf是JTextField对象,单行文本框,用来显示数据。
panel1,panel2,panel3,panel4是JPanel对象,用来盛放按钮。
Panel1盛放“←”,“CE”,“C”按钮,Panel2盛放功能区按钮,Panel3盛放记忆区按钮,Panel4盛放面板Panel1和Panel2
isResult,flag是布尔型变量
result是double型变量,存储结果
SpecialNumActionListener是SpecialNum类的对象,监听特殊数字按钮
df是DecimalFormat类的对象,控制输出格式
方法
SmartCauculator是构造方法,完成面板的布局并调用init方法
init方法调用addButton方法向面板中增加按钮
addButtion方法统一设置按钮的使用方式,参数包括panel,name,action,color,每次完成向面板添加按钮,设置名字,添加键盘监听器和设置按钮颜色
getResult方法实现四则运算,并输出
main方法,程序入口
2内部类FunctionSigns,继承接口ActionListener,实现接口的actionProform抽象方法,完成开平方,求对数等操作
3内部类ClearArea,继承接口ActionListener,实现接口的actionProform抽象方法,对清除区三个按钮“←”,“CE”,“C”进行监听,实现删除功能。
4内部类SpecialNum,继承接口ActionListener,实现接口的actionProform抽象方法,对π,e,及数字键监听
5Dot对小数点进行监听
【用户界面】
【模块划分及调用关系】
Dot
SpecialNum
ClearArea
Functions
程序分为五个模块,在主类中对剩余四个模块进行调用
【模块流程图】
SmartCalculator类流程图
ClearArea流程图
N
N
Y
YN
Y
Y
【运行说明】
程序运行后会弹出计算器界面,用户使用鼠标点击按钮就可以进行数据计算。
2、程序上机调试报告
【语法错误及其排除】
在使用if()elseif()嵌套语句时经常出现语法错误,在查阅书籍后排除
【算法错误及其排除】
在对抽象方法actionPerformed进行现实时总出现错误,最后排除。
3、程序测试结果
【测试数据】
计算36+47,99÷3,
,输出π,e的值,将180度转换成弧度,
【输出结果】
【收获及体会】
从本次课程设计的完成中,首先我发现了我有很多不足的地方,最突出的是所学的东西太少了,根本满足不了平时编写程序的需要。
此外,了解了课程设计的要求与方法,学会了程序设计基本步骤。
同时,使我感觉到,一个优秀的程序,不仅仅是可以运行的,更应该具较高的效率,合理的结构,良好的可读性和一定的容错性。
除此之外,一个程序的成功完成需要严谨的务实态度。
认真的态度是课程设计取得成功的一项非常重要的保证。
在以后的学习中,我将虚心向学力争编写出更有意义的程序。
4、源程序代码(Java语言,要求有尽可能多的注释语句)
package课程设计之计算器;
importjava.awt.*;
importjava.awt.event.*;
importjava.text.DecimalFormat;
importjavax.swing.*;
publicclassSmartCalculatorextendsJFrame{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
privateJTextFieldtf;
privateJPanelpanel1,panel2,panel3,panel4;
privateStringback;
privatebooleanisResult=true,flag=false;
privateStringoperator="=";
privatedoubleresult=0;
privateSpecialNumSpecialNumActionListener;
privateDecimalFormatdf;
publicSmartCalculator(){
super("科学计算器");//设置标题栏
df=newDecimalFormat("#.####");//保留四位小数
this.setLayout(newBorderLayout(10,5));//边界布局
panel1=newJPanel(newGridLayout(1,3,10,10));//1行3列
panel2=newJPanel(newGridLayout(5,6,5,5));//5行6列,水平间隔和竖直间隔都设为5
panel3=newJPanel(newGridLayout(5,1,5,5));//5行1列,水平间隔和竖直间隔都设为5
panel4=newJPanel(newBorderLayout());//边界布局
SpecialNumActionListener=newSpecialNum();//实现对特殊数字的监听
tf=newJTextField();//文本区域,即计算器的显示区域
tf.setEditable(true);//文本区域可编辑,^C复制结果
tf.setBackground(Color.WHITE);//文本区域背景色为白
tf.setHorizontalAlignment(JTextField.RIGHT);//文字右对齐
tf.setText("0");
tf.setBorder(BorderFactory.createLineBorder(Color.RED));
tf.setEditable(false);//设为不可编辑
init();//调用init方法初始化计算器
}
/**
*初始化界面
*/
privatevoidinit(){
addButton(panel1,"←",newClearArea(),Color.BLACK);//黑色
addButton(panel1,"CE",newClearArea(),Color.BLACK);
addButton(panel1,"C",newClearArea(),Color.BLACK);
addButton(panel2,"1/x",newFunctionSigns(),Color.magenta);//洋红色
addButton(panel2,"log",newFunctionSigns(),Color.magenta);
addButton(panel2,"7",SpecialNumActionListener,Color.blue);//蓝色
addButton(panel2,"8",SpecialNumActionListener,Color.blue);
addButton(panel2,"9",SpecialNumActionListener,Color.blue);
addButton(panel2,"÷",newFunctionSigns(),Color.BLACK);
addButton(panel2,"n!
",newFunctionSigns(),Color.magenta);
addButton(panel2,"sqrt",newFunctionSigns(),Color.magenta);
addButton(panel2,"4",SpecialNumActionListener,Color.blue);
addButton(panel2,"5",SpecialNumActionListener,Color.blue);
addButton(panel2,"6",SpecialNumActionListener,Color.blue);
addButton(panel2,"×",newFunctionSigns(),Color.BLACK);
addButton(panel2,"sin",newFunctionSigns(),Color.magenta);
addButton(panel2,"x^2",newFunctionSigns(),Color.magenta);
addButton(panel2,"1",SpecialNumActionListener,Color.blue);
addButton(panel2,"2",SpecialNumActionListener,Color.blue);
addButton(panel2,"3",SpecialNumActionListener,Color.blue);
addButton(panel2,"-",newFunctionSigns(),Color.BLACK);
addButton(panel2,"cos",newFunctionSigns(),Color.magenta);
addButton(panel2,"x^3",newFunctionSigns(),Color.magenta);
addButton(panel2,"0",SpecialNumActionListener,Color.blue);
addButton(panel2,"-/+",newClearArea(),Color.blue);
addButton(panel2,".",newDot(),Color.blue);
addButton(panel2,"+",newFunctionSigns(),Color.BLACK);
addButton(panel2,"tan",newFunctionSigns(),Color.magenta);
addButton(panel2,"%",newFunctionSigns(),Color.magenta);
addButton(panel2,"π",SpecialNumActionListener,Color.gray);//灰色
addButton(panel2,"e",SpecialNumActionListener,Color.gray);
addButton(panel2,"度变弧度",newFunctionSigns(),Color.gray);
addButton(panel2,"=",newFunctionSigns(),Color.BLACK);
JButtonbtns=newJButton("记忆区");
btns.setBorder(BorderFactory.createLoweredBevelBorder());//凹入斜面效果
btns.setEnabled(false);//按钮不可操作
btns.setPreferredSize(newDimension(20,20));
panel3.add(btns);//加入按钮
addButton(panel3,"MC",null,Color.BLACK);
addButton(panel3,"MR",null,Color.BLACK);
addButton(panel3,"MS",null,Color.BLACK);
addButton(panel3,"M+",null,Color.BLACK);
panel4.add(panel1,BorderLayout.NORTH);
panel4.add(panel2,BorderLayout.CENTER);
this.add(tf,BorderLayout.NORTH);
this.add(panel3,BorderLayout.WEST);
this.add(panel4);
pack();
this.setResizable(false);//窗口不可改变大小
this.setLocation(300,200);//窗口定位
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭按钮
}
/**
*统一设置按钮的使用方式,参数包括panel,name,action,color
*/
privatevoidaddButton(JPanelpanel,Stringname,ActionListeneraction,Colorcolor){
JButtonbt=newJButton(name);
panel.add(bt);//向面板添加按钮
bt.addActionListener(action);//增加监听事件
bt.setForeground(color);//设置文字颜色
}
/**
*四则运算,参数X表示用户输入数字
*/
privatevoidgetResult(doublex){
if(operator=="+"){result+=x;}
elseif(operator=="-"){result-=x;}
elseif(operator=="×"){result*=x;}
elseif(operator=="÷"){result/=x;}
elseif(operator=="="){result=x;}
tf.setText(df.format(result));
}
/**
*函数区运算符号事件监听
*/
classFunctionSignsimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
/*用ActionEvent对象的getActionCommand()方法
*取得与引发事件对象相关的字符串
*/
Stringstr=e.getActionCommand();//求平方根
if(str.equals("sqrt")){
doublei=Double.parseDouble(tf.getText());
if(i>=0){
/*
*String.valueof()转换为字符串
*df.format()按要求保留四位小数
*Math.sqrt()求算数术平方根
*/
tf.setText(String.valueOf(df.format(Math.sqrt(i))));
}
else{
tf.setText("负数不能开平方根");
}
}
elseif(str.equals("log")){//求常用对数
doublei=Double.parseDouble(tf.getText());
if(i>0){
tf.setText(String.valueOf(df.format(Math.log(i))));
}else{
tf.setText("负数不能求对数");
}
}
elseif(str.equals("%")){//求百分比
tf.setText(df.format(Double.parseDouble(tf.getText())/100));
}
elseif(str.equals("1/x")){//求倒数
if(Double.parseDouble(tf.getText())==0){
tf.setText("除数不能为零");
}else{
tf.setText(df.format(1/Double.parseDouble(tf.getText())));
}
}
elseif(str.equals("sin")){//求正弦函数
doublei=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.sin(i))));
}
elseif(str.equals("cos")){//求余弦函数
doublei=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.cos(i))));
}
elseif(str.equals("tan")){//求正切函数
doublei=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(Math.tan(i))));
}
elseif(str.equals("n!
")){//求阶乘
doublei=Double.parseDouble(tf.getText());
if((i%2==0)||(i%2==1))//判断为整数放进行阶乘操作
{
intj=(int)i;//强制类型转换
intresult=1;
for(intk=1;k<=j;k++)
result*=k;
tf.setText(String.valueOf(result));
}
else{tf.setText("无法进行阶乘");}
}
elseif(str.equals("x^2")){//求平方
doublei=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(i*i)));
}
elseif(str.equals("x^3")){//求立方
doublei=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(i*i*i)));
}
/**度变弧度---将角度转换成弧度
*/
elseif(str.equals("度变弧度")){
doublei=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(df.format(i/180*Math.PI)));
}
else{
if(flag){
isResult=false;
}
if(isResult){
operator=str;
}else{
getResult(Double.parseDouble(tf.getText()));
operator=str;
isResult=true;
}
}
}
}
/**
*清除区三个按钮的事件监听
*/
classClearAreaimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
Stringstr=e.getActionCommand();
if(str=="C"){
tf.setText("0");
isResult=true;
result=0;
}elseif(str=="-/+"){
doublei=(-1)*Double.parseDouble(tf.getText().trim());
tf.setText(df.format(i));
}elseif(str=="←"){
if(Double.parseDouble(tf.getText())>0){
if(tf.getText().length()>1){
tf.setText(tf.getText().substring(0,tf.getText().length()-1));
}else{
tf.setText("0");
isResult=true;
}
}
}elseif(str=="CE"){
tf.setText("0");
isResult=true;
}
}
}
/*
*特殊数字去两个按钮的事件监听
*/
classSpecialNumimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
Stringstr=e.getActionCommand();
if(i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA课程设计任务书 科学计算器 JAVA 课程设计 任务书 科学 计算器
![提示](https://static.bingdoc.com/images/bang_tan.gif)