《Java课程设计----简易计算器.pdf》由会员分享,可在线阅读,更多相关《Java课程设计----简易计算器.pdf(17页珍藏版)》请在三一文库上搜索。
1、面向对象课程设计(面向对象课程设计(JavaJava) 题目:简易计算器 专业:电子商务 班级:1110025 学生姓名: 指导老师: 2013 年 1 月 6 日 目录目录 一、 设计内容. 1 二、设计要求. 1 三、总体设计. 1 四、具体设计. 1 五、程序设计. 6 设计体会与小结. 15 简易计算器设计简易计算器设计 一、设计内容 设计一个图形界面(GUI)的应用程序,完成简单的运算。通过计 算器的编辑来检测和提升自己的 java 能力,并最终完成课程设计报告。 二、设计要求 1、应用自己所学课程知识完成对计算器的基本任务。 2、查阅相关资料,学习和掌握项目中涉及的新知识,提高自学
2、能 力。 3、 通过应用 java 程序编写计算器来提升对简单的图形界面的了解 和掌握。 三、总体设计 该计算器基本运算没有问题,清零、正负号、求倒数、退格功能都 能很好的实现,总体能完成一个计算器的基本功能,但仍有许多地方需要 改进,比如小数点的实现所存在的一些问题,虽然在基本的运算过程当中 不会造成太大影响,但这依然不能认为是一个很好的计算器,同时,在另 一方面,该计算器还没能很好的实现连续计算的功能,必须每次按下等号 按钮计算出结果后才能用产生的结果接着进行下一次的计算,改进的方法 是在运算符上同时注册 Result 类,让运算符同时拥有计算结果的功能。 四、具体设计四、具体设计 1、程
3、序流程图: 1 2、主要代码展示及说明: 总体代码的设计: 程序主窗体继承 JFrame 类,使用 24 个 JBotton 按钮实现数字和基本功能, 并且分别注册监听,采用6 行 4 列网格布局,完成计算器界面的基本设置, 最后对按钮进行计算分析,计算并显示结果。 这段代码主要是对程序的主框架进行相应的设置 super(简易计算器); setSize(300,260); this.setLayout(new BorderLayout(); jPanel1=new JPanel(); jPanel1.setLayout(new GridLayout(1,1);/一行一列的布局 jPanel2=
4、new JPanel(); jPanel2.setLayout(new GridLayout(6,4); /六行四列的布局 resultField=new JTextField(0); jPanel1.add(resultField); this.add(jPanel1,BorderLayout.NORTH); this.add(jPanel2,BorderLayout.CENTER); 设置数字按钮 1 到 9 并添加监控以及符号等的添加监控 s1=new JButton( 1 );s1.addActionListener(this); s2=new JButton( 2 );s2.addA
5、ctionListener(this); s3=new JButton( 3 );s3.addActionListener(this); s4=new JButton( 4 );s4.addActionListener(this); 2 s5=new JButton( 5 );s5.addActionListener(this); s6=new JButton( 6 );s6.addActionListener(this); s7=new JButton( 7 );s7.addActionListener(this); s8=new JButton( 8 );s8.addActionListe
6、ner(this); s9=new JButton( 9 );s9.addActionListener(this); s0=new JButton( 0 );s0.addActionListener(this); b1=new JButton( + );b1.addActionListener(this); b2=new JButton( - );b2.addActionListener(this); b3=new JButton( * );b3.addActionListener(this); b4=new JButton( / );b4.addActionListener(this); b
7、5=new JButton( );b5.addActionListener(this); b6=new JButton( 开方 );b6.addActionListener(this); b7=new JButton( % );b7.addActionListener(this); b8=new JButton( x! );b8.addActionListener(this); b9=new JButton( 1/x );b9.addActionListener(this); f1=new JButton( . );f1.addActionListener(this); f2=new JBut
8、ton( = );f2.addActionListener(this); f3=new JButton( C );f3.addActionListener(this); f4=new JButton(负 );f4.addActionListener(this); f5=new JButton( 退格 );f5.addActionListener(this); jPanel2.add(f3); jPanel2.add(b4); jPanel2.add(b3); jPanel2.add(f5); jPanel2.add(s7); jPanel2.add(s8); jPanel2.add(s9);
9、jPanel2.add(b1); jPanel2.add(s4); jPanel2.add(s5); jPanel2.add(s6); jPanel2.add(b2); jPanel2.add(s1); jPanel2.add(s2); jPanel2.add(s3); jPanel2.add(b5); jPanel2.add(s0); jPanel2.add(f1); jPanel2.add(f4); 3 jPanel2.add(b6); jPanel2.add(b7); jPanel2.add(b8); jPanel2.add(b9); jPanel2.add(f2); (3)数据输入用方
10、法 num(int i)设计如下: public void num(int i) String s = null; s=String.valueOf(i);/返回整数 i 的字符串表示形式 if(end)/如果数字输入结束,则将文本框置零,重 新输入 resultField.setText(0); end=false; if(resultField.getText().equals(0) /如果文本框的内容为零,则覆盖文本框的内容 resultField.setText(s); else/如果文本框的内容不为零, 则在内容后面添加数 字 str = resultField.getText()
11、+ s; resultField.setText(str); 以及用 JFrame 的方法 actionPerformed(ActionEvent e)来获取数据 (4)符号运算及清零、退格的设计如下: if(add)/ 加法 num1=num1 +num2; else if(sub)/减法 num1=num1 - num2; else if(mul)/乘法 num1=num1 * num2; else if(div)/除法 if (num2=0)throw new ArithmeticException(除数不能为 零); else 4 num1=num1 / num2; else if(c
12、hf)/乘方 if(num2=0) num1=1; else num1=Math.pow(num1, num2); else if(evo)/开方 if(num20) throw new ArithmeticException(被开方数 不能小于零); else num1=Math.sqrt(num2); else if(rec)/倒数 if(num20) throw new ArithmeticException(分母不能小于 零); else num1=1/num2; else if(per)/百分 num1=num1/100.0; else if(pur)/阶乘 if(num20) th
13、row new ArithmeticException(负数不能求 阶乘); else for(int i=1;inum2;i+) num1=i*num1; resultField.setText(String.valueOf(num1); end=true; else if(e.getSource()=f3)/实现清零 resultField.setText(0); else if(e.getSource()=f4)/实现负号 if(resultField.getText()!=0) resultField.setText(-+resultField.getText(); else if(e
14、.getSource()=f5)/实现退格 5 int i; i=(resultField.getText().length(); if(i=1) resultField.setText(0); else str=resultField.getText(); resultField.setText(str.substring(0,i-1); 五、程序设计五、程序设计 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class test1 extends JFrame implements Acti
15、onListener / JFrame jframe1; JPanel jPanel1,jPanel2; JTextField resultField; JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,b5,b6,b7,b8,b9,f1,f 2,f3,f4,f5;privateboolean end,add,sub,mul,div,evo,chf,per,rec,pur; private String str; private double num1,num2; public test1() super(简易计算器); setSize(300
16、,260); this.setLayout(new BorderLayout(); jPanel1=new JPanel(); jPanel1.setLayout(new GridLayout(1,1);/一行一列的布局 jPanel2=new JPanel(); jPanel2.setLayout(new GridLayout(6,4); /四行五列的布局 resultField=new JTextField(0); jPanel1.add(resultField); this.add(jPanel1,BorderLayout.NORTH); s1=new JButton( 1 );s1.a
17、ddActionListener(this); 6 s2=new JButton( 2 );s2.addActionListener(this); s3=new JButton( 3 );s3.addActionListener(this); s4=new JButton( 4 );s4.addActionListener(this); s5=new JButton( 5 );s5.addActionListener(this); s6=new JButton( 6 );s6.addActionListener(this); s7=new JButton( 7 );s7.addActionLi
18、stener(this); s8=new JButton( 8 );s8.addActionListener(this); s9=new JButton( 9 );s9.addActionListener(this); s0=new JButton( 0 );s0.addActionListener(this); b1=new JButton( + );b1.addActionListener(this); b2=new JButton( - );b2.addActionListener(this); b3=new JButton( * );b3.addActionListener(this)
19、; b4=new JButton( / );b4.addActionListener(this); b5=new JButton( );b5.addActionListener(this); b6=new JButton( 开方 );b6.addActionListener(this); b7=new JButton( % );b7.addActionListener(this); b8=new JButton( x! );b8.addActionListener(this); b9=new JButton( 1/x );b9.addActionListener(this); f1=new J
20、Button( . );f1.addActionListener(this); f2=new JButton( = );f2.addActionListener(this); f3=new JButton( C );f3.addActionListener(this); f4=new JButton(负 );f4.addActionListener(this); f5=new JButton( 退格 );f5.addActionListener(this); jPanel2.add(f3); jPanel2.add(b4); jPanel2.add(b3); jPanel2.add(f5);
21、jPanel2.add(s7); jPanel2.add(s8); jPanel2.add(s9); jPanel2.add(b1); jPanel2.add(s4); jPanel2.add(s5); jPanel2.add(s6); jPanel2.add(b2); jPanel2.add(s1); jPanel2.add(s2); jPanel2.add(s3); jPanel2.add(b5); 7 jPanel2.add(s0); jPanel2.add(f1); jPanel2.add(f4); jPanel2.add(b6); jPanel2.add(b7); jPanel2.a
22、dd(b8); jPanel2.add(b9); jPanel2.add(f2); this.add(jPanel2,BorderLayout.CENTER); public void num(int i) String s = null; s=String.valueOf(i);/返回整数 i 的字符串表示形式 if(end)/如果数字输入结束,则将文本框置零,重新输入 resultField.setText(0); end=false; if(resultField.getText().equals(0) /如果文本框的内容为零,则覆盖文本框的内容 resultField.setText(
23、s); else/如果文本框的内容不为零,则在内容后面添加数字 str = resultField.getText() + s; resultField.setText(str); publicvoidactionPerformed(ActionEvente)throws ArithmeticException /数字事件 if(e.getSource()=s1) num(1); else if(e.getSource()=s2) num(2); else if(e.getSource()=s3) num(3); else if(e.getSource()=s4) num(4); else i
24、f(e.getSource()=s5) 8 num(5); else if(e.getSource()=s6) num(6); else if(e.getSource()=s7) num(7); else if(e.getSource()=s8) num(8); else if(e.getSource()=s9) num(9); else if(e.getSource()=s0) num(0); /符号事件 else if(e.getSource()=b1) sign(1); else if(e.getSource()=b2) sign(2); else if(e.getSource()=b3
25、) sign(3); else if(e.getSource()=b4) sign(4); else if(e.getSource()=b5) sign(5); else if(e.getSource()=b6) sign(6); else if(e.getSource()=b7) sign(7); else if(e.getSource()=b8) sign(8); else if(e.getSource()=b9) sign(9); else if(e.getSource()=f1)/实现. str=resultField.getText(); if(str.indexOf(.)=1) s
26、tr+=.; resultField.setText(str); else if(e.getSource()=f2)/实现= num2=Double.parseDouble(resultField.getText(); 9 if(add)/ 加法 num1=num1 +num2; else if(sub)/减法 num1=num1 - num2; else if(mul)/乘法 num1=num1 * num2; else if(div)/除法 if (num2=0)throw new ArithmeticException(除数不 能为零); else num1=num1 / num2; e
27、lse if(chf)/乘方 if(num2=0) num1=1; else num1=Math.pow(num1, num2); else if(evo)/开方 if(num20) throw new ArithmeticException(被开方数不 能小于零); else num1=Math.sqrt(num2); else if(rec)/倒数 if(num20) throw new ArithmeticException(分母不能小 于零); else num1=1/num2; else if(per)/百分 num1=num1/100.0; else if(pur)/阶乘 if(n
28、um20) throw new ArithmeticException(负数不能求 阶乘); else for(int i=1;inum2;i+) num1=i*num1; 10 resultField.setText(String.valueOf(num1); end=true; else if(e.getSource()=f3)/实现清零 resultField.setText(0); else if(e.getSource()=f4)/实现负号 if(resultField.getText()!=0) resultField.setText(-+resultField.getText()
29、; else if(e.getSource()=f5)/实现退格 int i; i=(resultField.getText().length(); if(i=1) resultField.setText(0); else str=resultField.getText(); resultField.setText(str.substring(0,i-1); public void sign(int s) if(s=1) add=true; sub=false; mul=false; div=false; evo=false; chf=false; per=false; pur=false;
30、rec=false; else if(s=2) add=false; sub=true; mul=false; 11 div=false; evo=false; chf=false; per=false; pur=false; rec=false; else if(s=3) add=false; sub=false; mul=true; div=false; evo=false; chf=false; per=false; pur=false; rec=false; else if(s=4) add=false; sub=false; mul=false; div=true; evo=fals
31、e; chf=false; per=false; pur=false; rec=false; else if(s=5) add=false; sub=false; mul=false; div=false; evo=false; chf=true; per=false; pur=false; 12 rec=false; else if(s=6) add=false; sub=false; mul=false; div=false; evo=true; chf=false; per=false; pur=false; rec=false; else if(s=7) add=false; sub=
32、false; mul=false; div=false; evo=false; chf=false; per=true; pur=false; rec=false; else if(s=8) add=false; sub=false; mul=false; div=false; evo=false; chf=false; per=false; pur=true; rec=false; else if(s=9) add=false; sub=false; mul=false; 13 div=false; evo=false; chf=false; per=false; pur=false; re
33、c=true; num1=Double.parseDouble(resultField.getText(); end=true; public class mainclass public static void main(String args) try test1 th1=new test1(); th1.show(); catch (Exception e) System.out.println(e); 14 设计体会与小结设计体会与小结 经过一周的实习,本学期的java 课程设计到此也算告一段落了,经过 这次的学习,我学到了很多东西,在此基础上更加巩固了自己对java 基本 知识及软件
34、开发的了解。 在做本项目是时候,会遇到很多小问题,比如说,在整个运算过程中 要如何确保输入的计算数据哪个是第一个计算数据的,哪个是第二个计算 数据的,同时也要区分运算符,因为该计算器程序应用的都是利用字符串 来完成计算的,而且不能重复输出运算符,更不能将运算符错误的存储在 了第一个计算数据的数值中,也得考虑到万一不小心重复点击了运算符按 钮会不会造成第一个计算数据的重新赋值等等问题,最后想到利用布尔类 型来很好的控制运算符的应用! 此次课程设计让我更了解熟悉了Java中的图形用户界面和它的编程方 式。在完成课题的过程中也不断充实了自己,学习到了很多以前没有学习 到的知识,收获很大。最大的收获就是对大学学习的总结和培养了解决困 难的信心和能力,使我对所学知识能够融会贯通,又不断丰富了新知识。 Java 计算器设计使得我们对所学的专业课有了更为深刻的认识,使得知识 得到了巩固和提高。 在接下来的时间里,我觉得我要更加努力的往深一层次的方面看齐, 了解更多有关 java 的知识,熟练掌握 java 中常用类的方法。 15