1:西安交通大学《Java技术与应用》大作业简单计算器的编写一、选题理由这个学期我选择《Java技术与应用》这门课(其实我的选修课已经修完了),完全是对java语言的兴趣和老师的个人魅力使然。这门课让我学到了许多知识,了解到了java语言的基本语法、结构和特点。由于个人觉得水平有限,并且没有队友,所以选择了较为容易的简易计算器作为我的大作业。二、系统目标实现对代数表达式的输入、计算、输出。三、系统分析与设计(1)计算类cal设计编写publicclasscal{publicstaticbooleancheck(Stringstr)//检验表达式是否正确publicstaticFloattransfer(Stringstr)//转化为栈的形式并计算publicstaticbooleancompare(chara,charb)//比较优先级,判断b的优先级是否大于等于a的优先级}2:publicstaticFloattransfer(Stringstr)方法为该程序的核心代码,涉及数据结构的知识。(2)人机界面设计通过swing可以容易的设计出计算器的人机界面。(3)部分细节①普通Button内部代码的编写由于要在文本框中输入一串字符串,而不是覆盖之前的输入。因此采用方法:先将文本框中原有的字符串取出,再加上要输入的字符,最后一同输出到文本框。具体代码为Stringstr1=jButton1.getText();Stringstr2=jTextField1.getText();jTextField1.setText(str2+str1);②”=“Button的代码编写3:”=“作为一个特殊的Button,其作用为:先将文本框中的字符串取出,取出后首先判断字符串即表达式是否正确,运用cal.check方法,若返回值为true,将字符串表达式送入cal.transfer,返回代数式的值,并在文本框jTextField输出。过程涉及各种数据类型的转换。具体代码为Stringstr1=jTextField1.getText();booleanm=cal.check(str1);if(m=true){Floatresult=cal.transfer(str1);Stringlast=String.valueOf(result);jTextField1.setText(last);}elsejTextField1.setText(default);(4)实验数据与测试用例4:5:四、使用说明输入数学表达式(可以用鼠标点按钮,也可直接在文本框键盘输入),输入后点击”=“按钮,得到计算结果。五、人员安排自己动手,丰衣足食。六、程序漏洞与不足。(1)程序健壮性不足。Cal.check方法代码存在问题,对于部分错误表达式没有报错。(2)按等号按键计算输出后,再按数字按钮不会清空文本框,会在计算结果的基础上继续输入。先自动清空文本框会更好一些。6:七、开发总结编写这个简易计算器是我编写的第一个成型的小软件,有很多不足。但是在编写的过程中,我对java语言有了更加深刻的理解,知道了设计制作软件的一般过程。老师给我的最深的印象就是认真负责,现场编程现场讲解,让我最真实的感受到了一个开发者的思维过程和遇到问题时的处理方法,这在我之前两年课程从来都没有遇到过。总之,感谢老师精彩的课程。八、附录:完整代码及jar文件importjava.awt.BorderLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JTextField;importjavax.swing.WindowConstants;importjavax.swing.SwingUtilities;/***ThiscodewaseditedorgeneratedusingCloudGarden'sJiglooSWT/SwingGUI*Builder,whichisfreefornon-commercialuse.IfJiglooisbeingused*commercially(ie,byacorporation,companyorbusinessforanypurpose*whatever)thenyoushouldpurchasealicenseforeachdeveloperusingJigloo.*Pleasevisit*acceptanceoftheselicensingterms.ACOMMERCIALLICENSEHASNOTBEEN*PURCHASEDFORTHISMACHINE,SOJIGLOOORTHISCODECANNOTBEUSEDLEGALLYFOR*ANYCORPORATEORCOMMERCIALPURPOSE.*/publicclassNewJFrameextendsjavax.swing.JFrame{privateJTextFieldjTextField1;7:privateJButtonjButton1;privateJButtonjButton15;privateJButtonjButton16;privateJButtonjButton21;privateJButtonjButton20;privateJButtonjButton19;privateJButtonjButton18;privateJButtonjButton17;privateJButtonjButton14;privateJButtonjButton13;privateJButtonjButton12;privateJButtonjButton11;privateJButtonjButton10;privateJButtonjButton9;privateJButtonjButton8;privateJButtonjButton7;privateJButtonjButton5;privateJButtonjButton4;privateJButtonjButton3;privateJButtonjButton2;/***Auto-generatedmainmethodtodisplaythisJFrame*/publicstaticvoidmain(String[]args){SwingUtilities.invokeLater(newRunnable(){publicvoidrun(){NewJFrameinst=newNewJFrame();inst.setLocationRelativeTo(null);inst.setVisible(true);}});}publicNewJFrame(){super();initGUI();}privatevoidinitGUI(){try{8:setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);getContentPane().setLayout(null);this.setTitle(Mycalculation);{jTextField1=newJTextField();getContentPane().add(jTextField1,North);jTextField1.setPreferredSize(newjava.awt.Dimension(460,72));jTextField1.setBounds(0,0,460,72);jTextField1.setFont(newjava.awt.Font(MicrosoftYaHeiUI,1,16));}{jButton1=newJButton();getContentPane().add(jButton1);jButton1.setText(1);jButton1.setBounds(25,87,82,48);jButton1.setFont(newjava.awt.Font(MicrosoftYaHeiUI,0,18));jButton1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){System.out.println(jButton1.actionPerformed,event=+evt);//TODOaddyourcodeforjButton1.actionPerformedStringstr1=jButton1.getText();Stringstr2=jTextField1.getText();jTextField1.setText(str2+str1);}});}{jButton2=newJButton();getContentPane().add(jButton2);jButton2.setText(2);jButton2.setBounds(120,87,82,48);jButton2.setFont(newjava.awt.Font(MicrosoftYaHeiUI,0,18));9:jButton2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){System.out.println(jButton2.actionPerformed,event=+evt);//TODOaddyourcodeforjButton2.actionPerformedStringstr1=jButton2.getText();Stringstr2=jTextField1.getText();jTextField1.setText(str2+str1);}});}{jButton3=newJButton();getContentPane().add(jButton3);jButton3.setText(3);jButton3.setBounds(214,87,82,48);jButton3.setFont(newjava.awt.Font(MicrosoftYaHeiUI,0,18));jButton3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){System.out.println(jButton3.actionPerformed,event=+evt);//TODOaddyourcodeforjButton3.actionPerformedStringstr1=jButton3.getText();Stringstr2=jTextField1.getText();jTextField1.setText(str2+str1);}});}{jButton4=newJButton();getContentPane().add(jButton4);jButton4.setText(4);jButton4.setBounds(25,142,82,48);jButton4.setFont(newjava.awt.Font(MicrosoftYaHeiUI,0,18));jButton4.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){10:System.out.println(jButton4.actionPerformed,event=+evt);//TODOaddyourcodeforjButt