基于java的简易计算器的设计.doc_第1页
基于java的简易计算器的设计.doc_第2页
基于java的简易计算器的设计.doc_第3页
基于java的简易计算器的设计.doc_第4页
基于java的简易计算器的设计.doc_第5页
已阅读5页,还剩115页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于java的简易计算器的设计基于java的简易计算器的设计摘要 自从java语言诞生以来,java语言就以不可抵挡的趋势很快成为国际上广泛流行的面向对象编程语言,它既具有高级语言的特点,又少了C语言中指针特点,因而变得简单了很多。Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,其具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。本文介绍了用java语言编写一个简易计算器。实现数学上的加、减、乘、除、乘方、开方、阶乘、正弦、余弦运算等相关运算。利用这个程序可以方便的进行日常性的小规模运算,小巧简易,是个实用的工具。关键词:程序设计;简易计算器;java语言THE DESIGN OF SIMPLE CALCULATOR BASED ON JAVA ABSTRACTSincetheinceptionofthejavalanguage,javalanguageTakeirresistibletrendsoonbecamewidespreadinternationalpopularobject-orientedprogramminglanguage,bothwithadvancedlanguagefeatures,andlessoftheClanguagepointercharacteristics,andthusbecomesalotsimpler.Javaisacross-platformapplicationsoftwarecanwriteobject-orientedprogramminglanguage,whichhasexcellentversatility,efficiency,platformportability,andsecurity,arewidelyusedinpersonalPC,datacenter,gamingconsoles,scientificsuper-computers,mobilephonesandtheInternet,alsohastheworldslargestdeveloperofprofessionalcommunity.IntheglobalcloudcomputingandmobileInternetindustryenvironment,Javaandmorehavesignificantadvantagesandbroadprospects.Thisarticledescribestheusejavalanguageasimplecalculator.Achievemathematicaladdition,subtraction,multiplication,division,involution,evolution,factorial,sine,cosineoperationsandotherrelatedoperations.Withthisprogramyoucaneasilycarryoutdailyoperationsofsmall-scale,smallsimple,isausefultool.Key words: programdesign; simple calculator; javalanguage目 录 1 前言.1 1.1 课程设计背景.11.2 需求环境.11.3 课程设计思路.12 课程设计概要.23 计算器详细设计 .3 3.1 计算器界面 . .33.1.1 CAL主类的显示分布.33.1.2计算器显示界面实现代码 . 33.2 监听各个按钮的类.53.2.1 编写监听类目的.53.2.2 监听类实现代码.53.3 按键响应类.73.3.1 响应类编写的目的.73.2.2 响应类实现代码.73.3 运算处理类.93.3.1 处理方式. .93.2.2 处理类实现代码.94 运行与调试.12 4.1 进入程序主界面.124.2 加减乘除功能实现.124.3 正余弦、正切功能实现.134.4 阶乘功能实现.134.5 乘方、开方功能实现.145 实验总结.15参考文献.16附录:源代码.17 5 1 前言1.1 课程设计背景日常生活中我们经常会遇到一些小型数据计算的问题,本课程设计就是以此为出发点,设计了这样一个简单的计算器,用以实现基本的数学运算。在java语言当中通过定义类,类中的各种方法实现对计算器相关功能的实现,并通过与用户自己手动输入并选择运算功能实现计算功能,是一个完全由用户控制的简易计算器。在完成课程设计的期间,参阅了相关的书籍并在网络上查找相关的资料,对实现这个设计提供了很大的帮助。由于设计水平和知识方面的匮乏,本次课程设计还存在缺点和不足,望多多包涵。1.2 需求环境本课程设计需要的设备为硬件要求和软件配置要求具体要求如下:硬件要求:一台计算机。软件配置:WINDOWS系统、JDK1.7、eclipse1.3课程设计思路做计算器最主要的就是实用性,要简单,便于操作。程序界面是一个窗口的形式展现并由用户自己完全手动控制。并以设计的要求为基础,在实现课程设计所给的功能以外,要对界面的美观进行了优化,看起来更人性化,由此一个简洁美观的计算器就建立起来了。2课程设计概要在设计程序的时候,考虑到计算器的功能,首先编写一个CAL类,在该类中按照其功能分别写了一个方法实现,在程序开始后,可以看到一个控制面板形式的计算器界面展现在用户面前,用户自己输入数据和选择要运算的功能进行计算。具体方法有:加减乘除运算、乘方开方运算、正余弦正切运算、删除阶乘运算等具体的关系结构如表2-1:CAL类加、减、乘、除方法正余弦、正切方法删除、阶乘方法乘方、开方方法表2-1 流程功能实现的流程图详解:用户按数字键输入数据加、减、乘、除方法正余弦、正切方法删除、阶乘方法乘方、开方方法用户启动计算器运算输出运算结果3 计算器详细设计3.1 计算器界面 3.1.1 CAL主类的显示布局为了方便用户的使用,创建了一个带按钮的控制面板。将所有的运算键、数字键等都放在该面板中方便用户选择与使用。所有的运算选择都完全由用户掌控。3.1.2 计算器显示界面实现代码 程序如下:public class cal extends Applet / 定义所有需要使用的全局变量String str = 0;/ 暂存显示屏数据String sign = null;/ 暂存符号数据double num = 0;/ 暂存内部运算数据boolean change = false;/ 暂存内部四则运算起用GridLayout g0, g1;/ 定义AWT布局部件JTextField number;Button num_p, num_0, num_1, num_2, num_3, num_4, num_5, num_6, num_7,num_8, num_9;Button act_add, act_mul, act_div, act_sub;Button mat_sin, mat_cos, mat_tan;Button do_ans, do_del;Button fac, sqr,inv,blank;Panel p0, p1;public void init()/ 主程序部分/ 定义各个控件的样式number = new JTextField(20);/ 显示屏number.setHorizontalAlignment(JTextField.RIGHT);number.setEnabled(false);number.setText(0);num_p = new Button(。);/ 小数点的按钮num_p.setForeground(Color.red);num_p.addActionListener(new getbt();num_0 = new Button(0);/ 数字0的按钮num_0.setForeground(Color.red);num_0.addActionListener(new getbt();num_1 = new Button(1);/ 数字1的按钮num_1.setForeground(Color.red);num_1.addActionListener(new getbt();/此处省略了部分代码num_9 = new Button(9);/ 数字9的按钮num_9.setForeground(Color.red);num_9.addActionListener(new getbt();act_add = new Button(+);/ 加法的按钮act_add.setForeground(Color.red);act_add.addActionListener(new getbt();/此处省略了部分代码blank = new Button(blank);/ blank的按钮blank.setForeground(Color.red);blank.addActionListener(new getbt();/ 布局整个计算器g0 = new GridLayout(1, 1, 10, 10);/ 布局输入输出大体位置g1 = new GridLayout(4, 6, 10, 10);p0 = new Panel(); / 显示屏的布局p0.setLayout(g0);p0.add(number);p0.setBounds(0, 0, 100, 50);/ 按纽的布局p1 = new Panel();p1.setLayout(g1);p1.add(num_1);/ 第一排按纽p1.add(num_2);p1.add(num_3);p1.add(act_add);p1.add(act_sub);p1.add(fac);/此处省略了部分代码p1.setBounds(5, 60, 400, 250);p1.setBackground(Color.cyan);/ 设置按钮背景颜色setLayout(null);add(p0);add(p1);this.setBackground(Color.cyan);/ 设置背景整体颜色3.2 监听各个按钮的类 3.2.1 编写监听类目的编写监听类的目的主要监听用户所按下的数字键和运算键,为了计算机后续的取得数据并进行运算做预备工作。给后续计算工作提供保障。 3.2.2 监听类实现代码class getbt implements ActionListener / 监听作出反应的类 public void actionPerformed(ActionEvent e)/ 对每个每个按钮做出响应if (e.getSource() = num_0)press0();else if (e.getSource() = num_1)press1();else if (e.getSource() = num_2)press2();else if (e.getSource() = num_3)press3();else if (e.getSource() = num_4)press4();else if (e.getSource() = num_5)press5();else if (e.getSource() = num_6)press6();else if (e.getSource() = num_7)press7();else if (e.getSource() = num_8)press8();else if (e.getSource() = num_9)press9();else if (e.getSource() = num_p)pressp();else if (e.getSource() = act_add)pressadd();else if (e.getSource() = act_mul)pressmul();else if (e.getSource() = act_div)pressdiv();else if (e.getSource() = act_sub)presssub();else if (e.getSource() = mat_sin)presssin();else if (e.getSource() = mat_tan)presstan();else if (e.getSource() = mat_cos)presscos();else if (e.getSource() = do_ans)pressans();else if (e.getSource() = do_del)pressdel();else if (e.getSource() = fac)pressfac();else if (e.getSource() = sqr)presssqr();else if (e.getSource() = inv)pressinv();3.3 按键响应类 3.3.1响应类编写的目的通过监听类的监听,采集用户所按下的按键信息进行分析,并将分析结果保存为不同的方法和标记,通过不同的标记在运算过程中调用处理类中的不同方法实现计算功能。 3.3.2 响应类实现代码public void press0()/ 对按0作出反应if (str.length() 16 & str != 0 & change = false) str += 0; else if (str = 0 | sign != null) str = 0;number.setText(str);/此处省略部分代码public void pressp()/ 对按。作出反应boolean p = false;for (int i = 0; i str.length(); i+)if (str.charAt(i) = .)p = true;if (str.length() 16)str = str.substring(0, 16);num = 0;number.setText(str);3.4 运算处理类 3.4.1 处理方式通过获得上诉用户所按下数字键和运算功能键的信息,用响应类的处理结果信息进行判断标示符的形式对号入座进行调用不同运算处理并把运算结果显示在输入数据框中。3.4.2 处理类实现代码public void pressans()/ 对按=作出反应if (sign = null)number.setText(str);else if (sign = fac) int sum=1;for(int i=1;i=num;i+)sum= sum*i;str = Double.toString(sum);number.setText(str);else if (sign = sqr) double sum;sum=Math.sqrt(Double.parseDouble(str); str = Double.toString(sum);number.setText(str);else if (sign = inv) double sum=num;for(int i=1;iDouble.parseDouble(str);i+)sum=sum*num;str = Double.toString(sum);number.setText(str);else if (sign = add) num += Double.parseDouble(str);str = Double.toString(num);number.setText(str); else if (sign = sub) num -= Double.parseDouble(str);str = Double.toString(num);number.setText(str); else if (sign = mul) num *= Double.parseDouble(str);str = Double.toString(num);number.setText(str); else if (sign = div) if (num != 0) num /= Double.parseDouble(str);str = Double.toString(num);number.setText(str); else str = 0;number.setText(str);sign = null;4 运行与调试 4.1 进入程序主界面4.2 加减乘除功能实现例如;333333乘于5555554.3 正余弦、正切功能实现例如:30的tan4.4 阶乘功能实现例如:9的阶乘4.5 乘方、开方功能实现例如;36开方5 课程设计总结本次课程设计用到的是主要是java的AWT和swing这两个类。所应用的开发语言是Java,所采用的开发运行环境是:JDK + Eclipse,由于也没有软件项目的经验,所以做起来很吃力。要独自编写计算器的各个功能,并对其进行按键的排版、监听、处理、运算等各个步骤的亲力亲为,基本上完成了任务书上的各项内容。虽然在这次的课程设计中有很多的不足,但是我也收获了很多。设计的过程中不时地翻阅资料,有时花费一天时间就只能完成一个功能。程序代码完成之后,要进行运行调试,刚开始由于没有调试经验和方法效率很低,同时发现了许多错误,最后在同学的帮助下,终于顺利完成了,自己也从中学到了不少东西。更重要的是,在测试过程中的往往发现自己粗心大意犯了很多不必要的错误,通过这次课程式设计,我对eclipse的开发环境有了更多的认识和了解,让我对java语言的认识又加深了一步,不仅使我学会了如何查阅资料,同时也让我发现了网络这个好老师。总之收获了很多,以后我会更努力的学习。参考文献1 朱福喜.Java语言程序设计(第二版).科学出版社2 陈国君等.Java程序设计基础(第二版).清华大学出版社3 Deitel.Java大学基础教程(第六版).电子工业出版社 4 MaryCampione.Java语言导学(第四版).机械工业出版社5 Y.Daniel Liang.Java语言程序设计基础篇(第六版).机械工业出版社6 Kathy Sierra.Head First Java(第二版).东南大学出版社7(美)埃克尔 著,陈昊鹏 译 Java编程思想(第4版).机械工业出版社8 李兴华 Java从入门到精通 人民邮电出版社 2010-4-1版附录:源代码package gefangyu;import java.applet.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;/import java.math.*;public class cal extends Applet / 定义所有需要使用的全局变量String str = 0;/ 暂存显示屏数据String sign = null;/ 暂存符号数据double num = 0;/ 暂存内部运算数据boolean change = false;/ 暂存内部四则运算起用GridLayout g0, g1;/ 定义AWT布局部件JTextField number;Button num_p, num_0, num_1, num_2, num_3, num_4, num_5, num_6, num_7,num_8, num_9;Button act_add, act_mul, act_div, act_sub;Button mat_sin, mat_cos, mat_tan;Button do_ans, do_del;Button fac, sqr,inv,blank;Panel p0, p1;public void init()/ 主程序部分/ 定义各个控件的样式number = new JTextField(20);/ 显示屏number.setHorizontalAlignment(JTextField.RIGHT);number.setEnabled(false);number.setText(0);num_p = new Button(。);/ 小数点的按钮num_p.setForeground(Color.red);num_p.addActionListener(new getbt();num_0 = new Button(0);/ 数字0的按钮num_0.setForeground(Color.red);num_0.addActionListener(new getbt();num_1 = new Button(1);/ 数字1的按钮num_1.setForeground(Color.red);num_1.addActionListener(new getbt();num_2 = new Button(2);/ 数字2的按钮num_2.setForeground(Color.red);num_2.addActionListener(new getbt();num_3 = new Button(3);/ 数字3的按钮num_3.setForeground(Color.red);num_3.addActionListener(new getbt();num_4 = new Button(4);/ 数字4的按钮num_4.setForeground(Color.red);num_4.addActionListener(new getbt();num_5 = new Button(5);/ 数字5的按钮num_5.setForeground(Color.red);num_5.addActionListener(new getbt();num_6 = new Button(6);/ 数字6的按钮num_6.setForeground(Color.red);num_6.addActionListener(new getbt();num_7 = new Button(7);/ 数字7的按钮num_p.addActionListener(new getbt();num_0 = new Button(0);/ 数字0的按钮num_0.setForeground(Color.red);num_0.addActionListener(new getbt();num_1 = new Button(1);/ 数字1的按钮num_1.setForeground(Color.red);num_1.addActionListener(new getbt();num_2 = new Button(2);/ 数字2的按钮num_2.setForeground(Color.red);num_2.addActionListener(new getbt();num_3 = new Button(3);/ 数字3的按钮num_3.setForeground(Color.red);num_3.addActionListener(new getbt();num_4 = new Button(4);/ 数字4的按钮num_4.setForeground(Color.red);num_4.addActionListener(new getbt();num_5 = new Button(5);/ 数字5的按钮num_5.setForeground(Color.red);num_5.addActionListener(new getbt();num_6 = new Button(6);/ 数字6的按钮num_6.setForeground(Color.red);num_6.addActionListener(new getbt();num_7 = new Button(7);/ 数字7的按钮的按钮mat_sin.setForeground(Color.red);mat_sin.addActionListener(new getbt();mat_cos = new Button(cos);/ cos的按钮mat_cos.setForeground(Color.red);mat_cos.addActionListener(new getbt();mat_tan = new Button(tan);/ tan的按钮mat_tan.setForeground(Color.red);mat_tan.addActionListener(new getbt();fac = new Button(fac);/ fac的按钮fac.setForeground(Color.red);fac.addActionListener(new getbt();sqr = new Button(sqr);/ sqr的按钮sqr.setForeground(Color.red);sqr.addActionListener(new getbt();inv= new Button(xy);/ Involution的按钮inv.setForeground(Color.red);inv.addActionListener(new getbt();blank = new Button(blank);/ blank的按钮blank.setForeground(Color.red);blank.addActionListener(new getbt();/ 布局整个计算器g0 = new GridLayout(1, 1, 10, 10);/ 布局输入输出大体位置g1 = new GridLayout(4, 6, 10, 10);p0 = new Panel(); / 显示屏的布局p0.setLayout(g0);p0.add(number);p0.setBounds(0, 0, 100, 50);/ 按纽的布局p1 = new Panel();p1.setLayout(g1);p1.add(num_1);/ 第一排按纽p1.add(num_2);p1.add(num_3);p1.add(act_add);p1.add(act_sub);p1.add(fac);p1.add(num_4);/ 第二排按纽p1.add(num_5);p1.add(num_6);p1.add(act_mul);p1.add(act_div);p1.add(sqr);p1.add(num_7);/ 第三排按纽p1.add(num_8);p1.add(num_9);p1.add(mat_sin);p1.add(mat_cos);p1.add(inv);p1.add(num_0);/ 第四排按钮p1.add(num_p);p1.add(do_ans);p1.add(do_del);p1.add(mat_tan);p1.add(blank);/ 为了占位排版美观p1.setBounds(5, 60, 400, 250);p1.setBackground(Color.cyan);/ 设置按钮背景颜色setLayout(null);add(p0);add(p1);this.setBackground(Color.cyan);/ 设置背景整体颜色class getbt implements ActionListener / 监听作出反应的类public void actionPerformed(ActionEvent e)/ 对每个每个按钮做出响应if (e.getSource() = num_0)press0();else if (e.getSource() = num_1)press1();else if (e.getSource() = num_2)press2();else if (e.getSource() = num_3)press3();else if (e.getSource() = num_4)press4();else if (e.getSource() = num_5)press5();else if (e.getSource() = num_6)press6();else if (e.getSource() = num_7)press7();else if (e.getSource() = num_8)press8();else if (e.getSource() = num_9)press9();else if (e.getSource() = num_p)pressp();else if (e.getSource() = act_add)pressadd();else if (e.getSource() = act_mul)pressmul();else if (e.getSource() = act_div)pressdiv();else if (e.getSource() = act_sub)presssub();else if (e.getSource() = mat_sin)presssin();else if (e.getSource() = mat_tan)presstan();else if (e.getSource() = mat_cos)presscos();else if (e.getSource() = do_ans)pressans();else if (e.getSource() = do_del)pressdel();else if (e.getSource() = fac)pressfac();else if (e.getSource() = sqr)presssqr();else if (e.getSource() = inv)pressinv();public void press0()/ 对按0作出反应if (str.length() 16 & str != 0 & change = false) str += 0; else if (str = 0 | sign != null) str = 0;number.setText(str);public void press1()/ 对按1作出反应if (str.length() 16 & str != 0 & change = false) str += 1; else if (str = 0 | sign != null) str = 1;change = false;number.setText(str);public void press2()/ 对按2作出反应if (str.length() 16 & str != 0 & change = false) str += 2; else if (str = 0 | sign != null) str = 2;change = false;number.setText(str);public void press3()/ 对按3作出反应if (str.length() 16 & str != 0 & change = false) str += 3; else if (str = 0 | sign != null) str = 3;change = false;number.setText(str);public void press4()/ 对按4作出反应if (str.length() 16 & str != 0 & change = false) str += 4; else if (str = 0 | sign != null) str = 4;change = false;number.setText(str);public void press5()/ 对按5作出反应if (str.length()

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论