Java程序设计实验报告-计算器.doc

上传人:good****022 文档编号:116546529 上传时间:2022-07-05 格式:DOC 页数:19 大小:256.50KB
返回 下载 相关 举报
Java程序设计实验报告-计算器.doc_第1页
第1页 / 共19页
Java程序设计实验报告-计算器.doc_第2页
第2页 / 共19页
Java程序设计实验报告-计算器.doc_第3页
第3页 / 共19页
点击查看更多>>
资源描述
Java程序设计实验报告题 目 计算器 姓 名 学 号 学 院 软件学院 专 业 软件工程 年 级 2009 级 指导教师 2010年 4 月 9 日一、实验目的1、 熟练掌握java面向对象编程。2、 选择合适的数据结构实现需求。3、 熟练使用各种控制结构。4、 掌握GUI组件、事件处理技术。二、实验任务1、 设计一个简易的计算器,可以进行四则运算:加、减、乘、除等(限于十进制下)程序要求:(1)应具有相应的界面,可参考下图进行设计: 图示1 图示2(2)操作符号定为:“+”,“-”,“*”,“/”,“(”,“)”等。(按国际惯例设计)(3)用户通过点击面板中的各种组件输入的表达式,应在上方文本框中显示。(4)当用户点击“=”时,文本框中之前的内容应清空,直接显示表达式运算结果。例如:输入表达式“3+2*(8-5)”之后按“=”,显示结果“9”。(5)具有菜单栏,内有相应的编辑“复制”、“粘贴”等菜单,提供相应的功能。(6)以上部分为必须完成的内容。2、选作功能,可参考windows系统附带计算器: (1)具有相应的科学计算功能。例如“图示2”中的各种科学计算功能。三、开发工具与平台1.开发工具:Eclipse默认是一个和Jbuilder类似的Java开发工具,但它不仅仅只 是Java开发工具,只要装上相应的插件,eclipse也可作为其它语言的开发工具。如C/C+插件(CDT)。2.开发平台:JDK1.5四、设计思路1.界面设计 (1)制作一个简单的面板JFrame,在其上面添加构造界面所需的各种组件其构造方法为:calculate()super();this.setBounds(10, 10, 300, 250);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setTitle(计算器);init();/创建菜单及各种组件(如按钮,文本框等)getContentPane().setLayout(new BorderLayout(3,3);getContentPane().add(panelCenter,BorderLayout.CENTER);getContentPane().add(panelNorth,BorderLayout.NORTH);(2)实现构造菜单及文本框等组件的函数init(),如下实现:Public void Init()/菜单项的构造形成; editor.add(copy);editor.addSeparator();editor.add(paste); editor.setMnemonic(E);look.setMnemonic(V);help.setMnemonic(H); copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK); paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK); help.add(about); bar.add(editor);bar.add(look);bar.add(help); setJMenuBar(bar);/将菜单添加到JFrame; /* 接下来是文本或结果显示区及Backspace、功能键(CE、C)和的添加,采用 GridBagLayout布局方式,将它们放在北部panelNorth面板。 其次,是数字键及其他功能键的的使用,我这里采用的是网格布局,setLayout(new GridLayout(4,6,5,5);将它们放在主面板中心区域。 这里给出网格包布布局的两个重要方法,由于代码段较长,故将其他的代码放在后面附录;private void makeShow(GridBagLayout gridBag,GridBagConstraints constraints, Component component) gridBag.setConstraints(component, constraints);/ 网格包布布局; panelNorth2.add(component);private void addButton(GridBagLayout gridBag,/网格包布布局重要方法;GridBagConstraints constraints, Component component) gridBag.setConstraints(component, constraints); panelNorth1.add(component);总体界面如下:2.逻辑设计 为了能够得到我们想要的操作,比如让那些数字参与运算,做什么运算等等,我们就需要为每个按钮添加响应事件,通过响应事件来完成我们预期的结果。以下我们来分析下重要的事件是如何响应的:.String str = e.getActionCommand();/ TODO Auto-generated method stubif(str.equals(Backspace)/点击 BackspacehanderBackspace();else if(str.equals(CE)/点击了CE的功能是清除当前显示的数值bool = false;show.setText(0);else if(str.equals(C)/点击了C的功能是清零copyText=;Div = 0.0;CalculateResult =;bool = false;show.setText(0);else if(str.equals(MC)/MC清楚存储器内容handerMC();else if(str.equals(MR)/MR将存于存储器的值显示在文本框中handerMR();else if(str.equals(MS)/MS将显示框得文字存于存储器handerMS();else if(str.equals(M+)/M+将显示框的数与存储器中的数相加并进行存储handerM();else if(0123456789.indexOf(str)=0)/按下的是数字键或者小数点正负号handerCenter(str);else if(str.equals(%)/将当前显示框里的数字用百分比表示( x/100)String current = show.getText();double a = 0;if(!current.isEmpty() a = Double.parseDouble(current); Double result =(Double) a/100; String s4 = result.toString(); if(!isInt(s4) show.setText(s4);else show.setText(s4.substring(0,s4.indexOf(.); bool = true; else handerOperation(str);/响应的是运算符;处理运算符操作3. 程序测试 程序测试过程中无较大BUG,基本上轻松搞定。五、实验总结 这是我第一次正式的java程序设计实验,感觉能够把学到的东西用到实际中是件非常有趣的事,更重要的是让我们真正明白了学贵致用的道理。 由于时间有限,本程序未能做到科学计算器,同时在这里也感谢老师的指导和同学的帮助。6、 附录public class calculate extends JFrame implements ActionListener /* * param args */private JMenuBar bar = new JMenuBar();private JMenu editor =new JMenu(编辑(E);private JMenuItem copy =new JMenuItem(复制);private JMenuItem paste =new JMenuItem(粘贴);private JMenu look = new JMenu(查看(V);private JMenu help = new JMenu(帮助(H);private JMenuItem about = new JMenuItem(关于);private String copyText ;private JPanel panelWest= new JPanel();private JPanel panelCenter = new JPanel();/放Number键private JPanel panelNorth = new JPanel();/放功能键1与文本框private JPanel panelNorth1 = new JPanel();private JPanel panelNorth2 = new JPanel();/放显示框private JPanel panelEast = new JPanel();/放运算键;private String strNorth = new StringBackspace,CE,C;/功能键1private String strCenter = new String7,8,9,4,5,6,1,2,3,0,+/-,.;private String strEast = new String/,sqrt,*,%,-,1/x,+,=;private StringstrWest = new StringMC,MR,MS,M+;private JButton buttonCenter = new JButtonstrCenter.length;private JButton buttonEast= new JButtonstrEast.length;private JButton buttonNorth = new JButtonstrNorth.length;private JButton buttonWest = new JButtonstrWest.length;private String ch=new String();/保存当前运算符private Double Div;/保存中间运算数private String save = new String();/保存存储器中的数值private String CalculateResult= new String() ;/保存运算结果private boolean bool = false;/当前结果是否为运算结果private JTextField show = new JTextField(0,24);/输出显示private JTextField label;private GridBagLayout gridBag = new GridBagLayout();/网格包布布局;private GridBagConstraints constraints = new GridBagConstraints();calculate()super();this.setBounds(10, 10, 300, 250);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setTitle(计算器);init();getContentPane().setLayout(new BorderLayout(3,3);getContentPane().add(panelCenter,BorderLayout.CENTER);getContentPane().add(panelNorth,BorderLayout.NORTH);public void init() editor.add(copy);editor.addSeparator();editor.add(paste); editor.setMnemonic(E);look.setMnemonic(V);help.setMnemonic(H); copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK); paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK); help.add(about); bar.add(editor);bar.add(look);bar.add(help); setJMenuBar(bar); copy.addActionListener(this); paste.addActionListener(this); about.addActionListener(this); panelNorth1.setLayout(gridBag);/ panelNorth2.setLayout(gridBag); show.setHorizontalAlignment(JTextField.RIGHT);/右对齐; show.setEditable(false); panelNorth.setLayout(new BorderLayout(); panelNorth.add(show,BorderLayout.NORTH); show.setBorder(new BevelBorder(BevelBorder.LOWERED);/让显示框凹陷下去 show.setBackground(Color.WHITE); /网格包布布局; constraints.gridx = 0; constraints.gridy = 0; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; makeShow(gridBag,constraints,show); /* /设置箱式布局间隔 (另一种布局方式) * / box.add(label); for(int i = 0;istrNorth.length;+i) buttonNorthi = new JButton(strNorthi); box.add(Box.createHorizontalStrut(10); box.add(buttonNorthi); buttonNorthi.addActionListener(this); buttonNorthi.setForeground(Color.RED); */ label = new JTextField(2); label.setEditable(false); label.setBorder(new BevelBorder(BevelBorder.LOWERED);/让文本框凹陷下去; constraints.gridx = 0; constraints.gridy = 0; constraints.fill = GridBagConstraints.NONE; constraints.insets = new Insets(10,5,10,10); addButton(gridBag,constraints,label); for(int j = 0;jstrNorth.length;+j) buttonNorthj = new JButton(strNorthj); buttonNorthj.addActionListener(this); buttonNorthj.setForeground(Color.RED); constraints.gridx =j+1 ; constraints.gridy = 0; addButton(gridBag,constraints,buttonNorthj); panelNorth.add(panelNorth1,BorderLayout.CENTER); panelNorth.add(panelNorth2,BorderLayout.NORTH); panelCenter.setLayout(new GridLayout(4,6,5,5); for(int i =0;istrWest.length;+i) buttonWesti = new JButton(strWesti); buttonWesti.setBorder(new BevelBorder(BevelBorder.RAISED); buttonWesti.setForeground(Color.RED); buttonWesti.addActionListener(this); for(int i =0;istrCenter.length;+i) buttonCenteri = new JButton(strCenteri); buttonCenteri.setBorder(new BevelBorder(BevelBorder.RAISED); buttonCenteri.setForeground(Color.BLUE); buttonCenteri.addActionListener(this); for(int i =0;istrEast.length;+i) buttonEasti = new JButton(strEasti); buttonEasti.setBorder(new BevelBorder(BevelBorder.RAISED); buttonEasti.addActionListener(this); if(i%2=0|(i=7) buttonEasti.setForeground(Color.RED); else buttonEasti.setForeground(Color.BLUE); int w =0,e = 0, c = 0; for(int i = 0;i24;) if(i%6=0) buttonWestw.setBorder(new LineBorder(Color.GRAY); panelCenter.add(buttonWestw); +i; +w; else for(int j = 0;j3;+j) buttonCenterc.setBorder(new LineBorder(Color.GRAY); panelCenter.add(buttonCenterc); +i; +c; for(int j = 0;j=0)/按下的是数字键或者小数点正负号handerCenter(str);else if(str.equals(%)/将当前显示框里的数字用百分比表示( x/100)String current = show.getText();double a = 0;if(!current.isEmpty() a = Double.parseDouble(current); Double result =(Double) a/100; String s4 = result.toString(); if(!isInt(s4) show.setText(s4);else show.setText(s4.substring(0,s4.indexOf(.); bool = true; else handerOperation(str);private void handerMC() save = ;/ TODO Auto-generated method stublabel.setText();private void handerMR() if(!save.isEmpty() if(!isInt(save) show.setText(save); else show.setText(save.substring(0,save.indexOf(.); private void handerMS() save = show.getText();/ TODO Auto-generated method stubprivate void handerM() String str = show.getText();/ TODO Auto-generated method stubdouble s=0,v=0;if(!str.isEmpty()&!save.isEmpty() s= Double.parseDouble(str); v = Double.parseDouble(save);save = String.valueOf(s+v);label.setText(M);private void handerCenter(String str) String result = show.getText();if(str.equals(.)if(!(result.indexOf(.)=0)/判断是否已存在小数点if(bool)show.setText();result=;result+=str;show.setText(result);bool = false;String num = result;if(*-+/.indexOf(ch)=0) handerResult(ch,num); else if(result.equals(0)result=str;show.setText(result);bool = false;String num = result;if(*-+/.indexOf(ch)=0)handerResult(ch,num); else if(bool)show.setText();/文本框不为0.既有数字或者0和小数点result = ;result+=str;show.setText(result);bool = false;String num = result;/保存if(*-+/.indexOf(ch)=0) handerResult(ch,num); private void handerOperation(String str) String result = show.getText();if(str.equals(+/-)/处理正负号 if(result.indexOf(-)=0) StringBuffer e = new StringBuffer(result); e.deleteCharAt(0); show.setText(e.toString(); String num = e.toString(); if(*-+/.indexOf(ch)=0) handerResult(ch,num); else StringBuffer e = new StringBuffer(result); e.insert(0, -); show.setText(e.toString(); String num = e.toString(); if(*-+/.indexOf(ch)=0) handerResult(ch,num); else if(str.equals(sqrt)/平方根功能if(!(result.indexOf(-)=0)double operate=0;if(!result.isEmpty() operate =Double.parseDouble(result); double reult = Math.sqrt(operate); Double a = reult; String s =a.toString(); if(!isInt(s) show.setText(s); else show.setText(s.substring(0,s.indexOf(.); bool = true; if(*-+/.indexOf(ch)=0) handerResult(ch,s); else show.setText(函数输入无效);bool= true;else if(str.equals(/)/除法运算;String show2 = show.getText();show.setText();ch=/;double operate = 0;if(!show2.isEmpty()operate = Double.parseDouble(show2); Div = (Double)operate;/保存被除数;else if(str.equals(*)String show2 = show.getText();show.setText();ch=*;/保存当前运算符double operate = 0;if(!show2.isEmpty() operate = Double.parseDouble(show2);Div = (Double)operate;else if(str.equals(-)String show2 = show.getText();show.setText();ch=-;double operate =0;if(!show2.isEmpty() operate= Double.parseDouble(show2);Div = (Double)operate;else if(str.equals(+)String show2 = show.getText();show.setText();ch=+;double operate = 0;if(!show2.isEmpty() operate = Double.parseDouble(show2); Div = (Double)operate;/保存加数else if(str.equals(1/x)/求倒String show2 = show.getText();if(!show2.equals(0)double operate = 0;if(!show2.isEmpty() operate = Double.parseDouble(show2);double resultReverse = (1/operate);Double resultReverseDouble = resultReverse;show.setText(resultReverseDouble.toString();bool = true;else show.setText(除数不能为零。);bool = true;else if(str.equals(=)show.setText(CalculateResult);bool = true;private boolean isInt(String s) if(s.indexOf(.)0)for(int i =(s.length()-1);is.indexOf(.);-i) if(!(s.charAt(i)=0) return false;return true;private void handerResult(String ch,String num) /处理运算结果并保存; char swt = 0 ;if(!ch.equals() swt = ch.charAt(0);/将String转换成charswitch(swt)case -: double resultSub = (Div-Double.parseDouble(num);Double resultSubDouble = resultSub;String s1 = resultSubDouble.toString(); if(!isInt(s1) CalculateResult=s1; else CalculateResult = s1.substring(0,s1.indexOf(.); /保存运算结果;break;case +:double resultAdd = (Div+Double.parseDouble(num);Double resultAddDouble = resultAdd;String s2 = resultAddDouble.toString(); if(!isInt(s2) CalculateResult=s2; else CalculateResult = s2.substring(0,s2.indexOf(.); break;case *: double resultMul = (Div*(Double.parseDouble(num); Double resultMulDouble = resultMul; String s3 = resultMulDouble.toString(); if(!isInt(s3) CalculateResult=s3; else CalculateResult = s3.substring(0,s3.indexOf(.); break;case /: if(Double.parseDouble(num)!=0) double resultDiv = (Div/(Double.parseDouble(num); Double resultDivDouble = resultDiv; String s4 = resultDivDouble.toString(); if(!isInt(s4) CalculateResult=s4; else CalculateResult = s4.substring(0,s4.indexOf(.); else CalculateResult = 除数不能为零。; break;private void handerBackspace() /退格键String show1 = show.getText();if(!bool)if(!(show1.equals(0)try String result = show1.substring(0, show1.length()-1); show.setText(result); if(result.isEmpty() show.setText(0); return; catch(ArrayIndexOutOfBoundsException e)e.printStackTrace();
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 临时分类 > 人力资源


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!