版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上小型计算器系统1. 课程设计目的JAVA程序设计是计算机相关专业的必修专业基础课程,其实践性、应用性很强。实践教学环节是必不可少的一个重要环节。本课程的程序设计专题实际是计算机相关专业学生学习完JAVA程序设计课程后,进行的一次全面的综合训练,JAVA程序设计的设计目的是加深对理论教学内容的理解和掌握,使学生较系统地掌握程序设计及其在网络开发中的广泛应用,基本方法及技巧,为学生综合运用所学知识,利用软件工程为基础进行软件开发、并在实践应用方面打下一定基础。2. 课程设计要求按课程设计指导书提供的课题,要求学生在自行完成各个操作环节,并能实现且达到举一反三的目的,完成一
2、个项目解决一类问题。要求学生能够全面、深入理解和熟练掌握所学内容,并能够用其分析、设计和解答类似问题;对此能够较好地理解和掌握,能够进行简单分析和判断;能编写出具有良好风格的程序;掌握JAVA程序设计的基本技能和面向对象的概念和方法;了解多线程、安全和网络等编程技术。同时培养学生进行分析问题、解决问题的能力;培养学生进行设计分析、设计方法、设计操作与测试、设计过程的观察、理解和归纳能力的提高。3. 课程设计内容3.1概要设计3.3.1自定义类说明本程序一共包括:构造方法A5i13w(),setup(), windowClosing(), actionPerformed(), appendcha
3、r(), countall(), counting(), Iscountnum()和一个windowcloser类。(1) A5i13w()是构造方法,在程序创建其对象时就调用,初始化次程序的事件的监听机制。(2) setup()是我定义的方法,是用来初始化程序的图形应用界面,由A5i13w()来调用。(3) windowClosing()是用来响应用户的关闭功能。(4) actionPerformed()是用来处理每个事件的响应的方法(5) appendchar()方法的作用是在用户产生事件后向Textshow追加产生此事件的字符串(6) countall()的意义是在用户按“=”号后,从最
4、里层括号向最外层逐层把括号里的内容传递给counting()方法,并负责对括号本身是否符合数学语法要求进行检验。(7) counting()方法的作用是首先对无括号的数学表达式有关的数学计算符号进行调整,并检验所有的数学符号,如发现错误的数学符号立即在Labelnow中显示出来。(8)Iscountnum()方法的作用是把“+ - × ÷ % . ( )”算术符号转化相应的数字,便于程序的流程控制。开始初始化数据double result=0/存储的结果boolean newnum=true/是否新数stringlastop=”=”:/存储操作符按下数字按钮按下操作符(+*
5、/=)按钮判断newnum把当前文本框的内容转为double后calc方法calc(double.parsedouble(txt.get))文本框显示新数:txt.setText(数字)newnum=false文本框接着显示txt.setText(txt.getText+数字)Lastop=”操作符”mewnum=true图2.1首先分析这个说要完成的功能,它的主要任务是完成十六进制、十进制、八进制和二进制之间的相互转化,需要包含负数,但不用考虑小数。 其次,确定接口参数。根据功能可知,它传递给外部的就是一个转后的数据,而外部除了需要传递给转化前的数据以外,是否还需要其他参数呢?我
6、们都知道,十进制的负数同样可以利用其他进制进行表示,例如二进制,转化方法是:求十进制数的绝对值对应的二进制数,最高位为1;然后对二进制逐位求反,最高位除外;接着最低一位加1,需要进位的话依次进位,最高位除外。如今的问题就摆在了我们面前,我们如何知道这个数的最高位是第几位呢,也许有人会说,那就用转化后的前面再加上一位,好,即使这样可以成立,那么-1转化后就是11,3转化后也为11,现在如果需要现在的把这个数二进制数再转化为十进制,那么我们如何知道原来的是正数还是负数呢,如果要转化为十六进制,八进制又当如何呢?经过以上的分析发现,还应该有另外一个参数二进制时的宽度,只有这样才能知道谁是最高位,因此
7、就称这个参数为位宽。 最后,设计类。根据第二步的分析,因为位宽对每一个方法都是一样,所以可能把它作为类的成员变量,而另外两个参数转化前的值和转化后的值就分别作为输入参数和返回值。而类对外的接口只有各个转化的方法,因此这几个方法设计为public权限,而其它所有的辅助方法都为private权限。位宽通过构造函数传递,外部不能直接访问和修改,设置也设置为private权限。为了减少计算量,所有的转化都以二进制为中介。例如十六进制到十进制,是十六进制二进制十进制。3.2详细设计(程序部分源代码及注释)import java.awt.*; import java.awt.event.*;
8、import javax.swing.*;public class MyCalculator implements ActionListener JFrame f; JMenu mEdit; JMenu mView; JMenu mHelp; JMenuItem mCopy; JMenuItem mPaste; JTextField tResult;JButton bNumber; JButton bOperator; JButton bOther; JButton bM;boolean isDouble=false;/是否为实数 int opFlag=-1; static double t1
9、=0,t2=0,t3=0,result=0; static int opflag1=-1,opflag2=-1,flag=0,resflag=1; int preOp,currentOp=0;/标准位 double op1=0,op2=0;/操作数 double n3; StringBuffer buf=new StringBuffer(20); StringBuffer copyBoard=new StringBuffer(20);/剪贴板 StringBuffer memory = new StringBuffer(20);/M系列 StringBuffer str = new Strin
10、gBuffer(); public MyCalculator() f = new JFrame("计算器ByYX"); Container contentPane = f.getContentPane(); /*菜单的创建开始*/ JMenuBar mBar = new JMenuBar(); mBar.setOpaque(true);mEdit = new JMenu("编辑(E)"); mEdit.setMnemonic(KeyEvent.VK_E); mCopy = new JMenuItem("复制(C)"); mEdit.a
11、dd(mCopy); mPaste = new JMenuItem("粘贴(P)"); mEdit.add(mPaste);mView = new JMenu("查看(V)"); mView.setMnemonic(KeyEvent.VK_V); mView.add(new JMenuItem("标准型"); mView.add(new JMenuItem("科学型"); mView.addSeparator(); mView.add(new JMenuItem("查看分组");mHelp =
12、new JMenu("帮助(H)"); mHelp.setMnemonic(KeyEvent.VK_H); mHelp.add(new JMenuItem("帮助主题"); mHelp.addSeparator(); mHelp.add(new JMenuItem("关于计算器");mBar.add(mEdit); mBar.add(mView); mBar.add(mHelp); f.setJMenuBar(mBar); contentPane.setLayout(new BorderLayout(); JPanel pTop =
13、new JPanel(); tResult = new JTextField("0.",26); tResult.setHorizontalAlignment(JTextField.RIGHT ); tResult.setEditable(false); pTop.add(tResult); contentPane.add(pTop,BorderLayout.NORTH); JPanel pBottom = new JPanel(); pBottom.setLayout(new BorderLayout(); JPanel pLeft = new JPanel(); pLe
14、ft.setLayout(new GridLayout(5,1,3,3); bM = new JButton(" ");bM.setEnabled(false); pLeft.add(bM); bOther = new JButton("MC"); bOther.addActionListener(this); bOther.setForeground(Color.RED); bOther.setMargin(new Insets(3,2,3,2); pLeft.add(bOther); bOther = new JButton("MR&quo
15、t;); bOther.addActionListener(this); bOther.setForeground(Color.RED); bOther.setMargin(new Insets(3,2,3,2); pLeft.add(bOther); bOther = new JButton("MS"); bOther.addActionListener(this); bOther.setForeground(Color.RED); bOther.setMargin(new Insets(3,2,3,2); pLeft.add(bOther); bOther = new
16、JButton("M+"); bOther.addActionListener(this); bOther.setForeground(Color.RED); bOther.setMargin(new Insets(3,2,3,2); pLeft.add(bOther); pBottom.add(pLeft,BorderLayout.WEST); JPanel pRight = new JPanel(); pRight.setLayout(new BorderLayout(); JPanel pUp = new JPanel(); pUp.setLayout(new Gri
17、dLayout(1,3,3,0); bOther = new JButton("Backspace"); bOther.addActionListener(this); bOther.setForeground(Color.RED); bOther.setMargin(new Insets(3,0,3,5); pUp.add(bOther); bOther = new JButton("CE"); bOther.addActionListener(this); bOther.setForeground(Color.RED); pUp.add(bOther
18、); bOther = new JButton("C"); bOther.addActionListener(this); bOther.setForeground(Color.RED); pUp.add(bOther); JPanel pDown = new JPanel(); pDown.setLayout(new GridLayout(4,5,3,2); bNumber = new JButton("7"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bN
19、umber.setMargin(new Insets(3,3,3,3); pDown.add(bNumber); bNumber = new JButton("8"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3,3,3,3); pDown.add(bNumber); bNumber = new JButton("9"); bNumber.setForeground(Color.blue); bNumb
20、er.addActionListener(this); bNumber.setMargin(new Insets(3,3,3,3); pDown.add(bNumber); bOperator = new JButton("/"); bOperator.setForeground(Color.RED); bOperator.addActionListener(this);bOperator.setMargin(new Insets(3,0,3,0); pDown.add(bOperator); bOperator = new JButton("sqrt"
21、); bOperator.addActionListener(this); bOperator.setForeground(Color.blue); bOperator.setMargin(new Insets(3,0,3,0); pDown.add(bOperator); bNumber = new JButton("4"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3,3,3,3); bNumber.setHorizo
22、ntalTextPosition(JButton.LEFT); pDown.add(bNumber); bNumber = new JButton("5"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3,3,3,3); pDown.add(bNumber); bNumber = new JButton("6"); bNumber.setForeground(Color.blue); bNumber.ad
23、dActionListener(this); bNumber.setMargin(new Insets(3,3,3,3); pDown.add(bNumber); bOperator = new JButton("*"); bOperator.setForeground(Color.RED); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3,3,3,3); pDown.add(bOperator); bOperator = new JButton("%"); bOpe
24、rator.setForeground(Color.blue); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3,3,3,3); pDown.add(bOperator); bNumber = new JButton("1"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3,3,3,3); pDown.add(bNumber); bNum
25、ber = new JButton("2"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3,3,3,3); pDown.add(bNumber); bNumber = new JButton("3"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3,3,
26、3,3); pDown.add(bNumber); bOperator = new JButton("-"); bOperator.setForeground(Color.RED); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3,3,3,3); pDown.add(bOperator); bOperator = new JButton("1/x"); bOperator.setForeground(Color.blue); bOperator.addActionLi
27、stener(this); pDown.add(bOperator); bNumber = new JButton("0"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3,3,3,3); pDown.add(bNumber); bOperator = new JButton("+/-"); bOperator.setForeground(Color.blue); bOperator.addActionL
28、istener(this); bOperator.setMargin(new Insets(3,3,3,3); pDown.add(bOperator); bOperator = new JButton("."); bOperator.setForeground(Color.blue); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3,3,3,3); pDown.add(bOperator); bOperator = new JButton("+"); bOperat
29、or.setForeground(Color.RED); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3,3,3,3); pDown.add(bOperator); bOperator = new JButton("="); bOperator.setForeground(Color.blue); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3,3,3,3); pDown.add(bOperator)
30、; pRight.add(pUp,BorderLayout.NORTH); pRight.add(pDown,BorderLayout.SOUTH); pBottom.add(pRight,BorderLayout.EAST); contentPane.add(pBottom,BorderLayout.SOUTH);f.setSize(new Dimension(320,256);f.setResizable(false); f.setVisible(true); f.addWindowListener(new WindowAdapter() public void windowClosing
31、(WindowEvent e) System.exit(0); ); public void actionPerformed(ActionEvent e) String s=e.getActionCommand(); if(s.equals("复制(C)") String temp=tResult.getText().trim(); copyBoard.replace(0,copyBoard.length(),temp); mPaste.setEnabled(true); else if(s.equals("粘贴(P)") tResult.setText
32、(copyBoard.toString(); else if(s.equals("CE") /如果是CE则清除文本框 tResult.setText("0."); else if(s.equals("Backspace") /如果是backspace则删除一个字符。 if(!tResult.getText().trim().equals("0.") /如果文本框中有内容 if(str.length()!=1&&str.length()!=0) tResult.setText(str.delete(s
33、tr.length()-1,str.length().toString(); else tResult.setText("0."); str.setLength(0); op2=Double.parseDouble(tResult.getText().trim(); else if(s.equals("C") /如果是C删除当前计算。 tResult.setText("0."); op1=op2=0; str.replace(0,str.length()," "); preOp=currentOp=0; else
34、if(s.equals("MC") /如果是MC则清除缓冲区 String temp="" memory.replace(0,memory.length(),temp); bM.setText(" "); else if(s.equals("MR") /如果按键为MR则恢复缓冲区的数到文本框 tResult.setText(memory.toString(); else if(s.equals("MS") /如果按键为MS则将文本框的数存入缓冲区 String s1=tResult.getTex
35、t().trim(); memory.replace(0,memory.length(),s1); bM.setText("M"); else if(s.equals("M+") /如果按键为MS则将文本框值与缓冲区的数相加但不显示结果。 String temp1=tResult.getText().trim(); double dtemp=Double.parseDouble(temp1); String temp2=memory.toString(); dtemp+=Double.parseDouble(temp2); temp1=String.va
36、lueOf(dtemp); memory.replace(0,memory.length(),temp1); else if(s.equals("1/x") /如果按键为1/x则将文本框中的数据置为它的倒数。 String temp=tResult.getText().trim(); double dtemp=Double.parseDouble(temp); tResult.setText(""+1/dtemp); else if(s.equals("sqrt") /如果按键为sqrt则将文本框中的内容求平方根 String tem
37、p=tResult.getText().trim(); double dtemp=Double.parseDouble(temp); tResult.setText(""+Math.sqrt(dtemp); else if(s.equals("+")/+ str.setLength(0); if(currentOp=0) preOp=currentOp=1;/op1=0; op2=0; tResult.setText(""+op1); else currentOp =preOp; preOp =1; switch(currentOp)
38、 case 1:op1=op1+op2;tResult.setText(""+op1);break; case 2:op1=op1-op2;tResult.setText(""+op1);break; case 3:op1=op1*op2;tResult.setText(""+op1);break; case 4:op1=op1/op2;tResult.setText(""+op1);break; else if(s.equals("-")/- str.setLength(0); if(curr
39、entOp=0) preOp=currentOp=2;/op1=op2;op2=0; tResult.setText(""+op1); else currentOp =preOp; preOp =2; switch(currentOp) case 1:op1=op1+op2;tResult.setText(""+op1);break; case 2:op1=op1-op2;tResult.setText(""+op1);break; case 3:op1=op1*op2;tResult.setText(""+op1
40、);break; case 4:op1=op1/op2;tResult.setText(""+op1);break; else if(s.equals("*")/* str.setLength(0); if(currentOp=0) preOp=currentOp=3;/op1=op2;op2=1; tResult.setText(""+op1);/op1=op2; else currentOp =preOp; preOp =3; switch(currentOp) case 1:op1=op1+op2;tResult.setText
41、(""+op1);break; case 2:op1=op1-op2;tResult.setText(""+op1);break; case 3:op1=op1*op2;tResult.setText(""+op1);break; case 4:op1=op1/op2;tResult.setText(""+op1);break; else if(s.equals("/")/ / str.setLength(0); if(currentOp=0) preOp=currentOp=4;/op2=1;
42、 tResult.setText(""+op1);/op1=op2; else currentOp =preOp; preOp =4; switch(currentOp) case 1:op1=op1+op2;tResult.setText(""+op1);break; case 2:op1=op1-op2;tResult.setText(""+op1);break; case 3:op1=op1*op2;tResult.setText(""+op1);break; case 4:op1=op1/op2;tResu
43、lt.setText(""+op1);break; else if(s.equals("=")/ = if(currentOp=0) str.setLength(0); tResult.setText(""+op2); else str.setLength(0); currentOp =preOp; switch(currentOp) case 1:op1=op1+op2;tResult.setText(""+op1);break; case 2:op1=op1-op2;tResult.setText("
44、"+op1);break; case 3:op1=op1*op2;tResult.setText(""+op1);break; case 4:op1=op1/op2;tResult.setText(""+op1);break; currentOp=0; op2=0; else if(s.equals(".") isDouble=true; if(tResult.getText().trim().indexOf('.')!=-1); else if(tResult.getText().trim().equals
45、("0") str.setLength(0); tResult.setText(str.append("0"+s).toString(); /else if(tResult.getText().trim().equals("")/如果初时显示为空则不做任何操作 else tResult.setText(str.append(s).toString(); else if(s.equals("0")/如果选择的是"0"这个数字键 if(tResult.getText().trim().equals(
46、"0.") else tResult.setText(str.append(s).toString(); op2=Double.parseDouble(tResult.getText().trim(); else tResult.setText(str.append(s).toString(); op2=Double.parseDouble(tResult.getText().trim(); if(currentOp=0) op1=op2; /end actionPerformedpublic static void main(String args) new MyCalculator(); 4.测试分析程序正常的响应按钮事件,输入的数值长度受变量length的限定,进行混合四则运算的时候,结果如果不是很大,可以正常显示,但如果结果很大,就可能采用科学记数方式,这样带来了进制转换时的麻烦,需要特殊的处理科学记数里的E ,当结果太大的时候,有可能超出可能表示的最大值,(结果溢出),这个时候就要对结果进行必要的异常处理,我在输出结果的方法内设置if 语句,如果结果大于某一个值就抛出一个类型的类,再使用try-catch,捕捉输出结果的方法抛出的异常对象。然后进行,重制归零的处理。 图2.2 图2.34.1程序运
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 种子包衣剂使用技术方案
- 安全生产法律法规培训指南
- 无人机飞防作业规范指引
- 施肥机械深施作业质量标准
- 足底反射疗法操作流程
- 经络疏通理疗服务手册
- 老人叩背排痰护理操作标准流程
- 内脏脂肪检测分析标准
- 烟草青枯病统防统治
- 超声波理疗设备使用操作规范
- 关于精益管理办法
- 湖北省部分高中2025届高三下学期四月统考(二模)政治试卷(含解析)
- 白细胞减少症病例讨论
- 年产200吨高纯金属铯铷项目报告书
- 2025具身智能行业发展研究报告
- 委托代缴社保协议书范例
- 智库能力测试题及答案
- 第五单元100以内的笔算加、减法达标卷(单元测试)(含答案)2024-2025学年一年级数学下册人教版
- 陕西单招数学试题及答案
- 《政府与集团项目型公关策略和销售技巧》
- CNAS-CC01:2015 管理体系认证机构要求
评论
0/150
提交评论