Java程序设计实验报告-计算器.doc_第1页
Java程序设计实验报告-计算器.doc_第2页
Java程序设计实验报告-计算器.doc_第3页
Java程序设计实验报告-计算器.doc_第4页
Java程序设计实验报告-计算器.doc_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

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);/将s

温馨提示

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

评论

0/150

提交评论