面向对象程序设计课程设计报告题 目: 专 业: 班 级: 学 号: 姓 名: 指导老师: 时 间: 一、课程设计题目及所涉及知识点设计题目是“计算器应用程序”所涉及的知识点主要是:1、java的抽象与封装:private、public、protected之类的访问属性控制符;2、图形用户界面:java中的awt包(Panel、Frame类),swing包(JFrame、Jpanel类)以及各种组件的使用即 按钮JButton、布局管理器(BorderLayout、GridLayout等)、标签JLabel、JTextField等;3、java机制:鼠标事件的处理,窗口事件的处理;4、类的继承与接口的实现:类的继承(extends关键字),接口的实现(implements关键字)。
二、课程设计思路1、设计的计算器是基于AWT 、SWING组;其功能就是可以完成加法、减法、乘法、除法简单运算以及其他复杂运算 例如:求平方、立方、平方根、对数、正弦、余弦、正切,余数等2、此程序由2个面板组成,第一个面板放置输入信息和运算结果的显示文本域,第二个面板放置按钮组建,例如数字键1、2、3、4等,利用布局管理器的嵌套完成各组件的分布 3、分两个部分来来具体实现该计算器即 Jisuan.java实现具体的界面布局及运算功能、CalculatorFrame.java为主函数部分,实现具体的面板显示及大小设计,对计算机进行初始化三、课程设计中遇到的难点及解决办法难点:1、如何实现清零功能 2、数学函数功能的实现解决方法: 1、分析代码的运行过程,设计出几种解决方式,然后进行实践,多次试验后解决了该问题 2、在JDK中寻找实现此数学函数的代码四、总结程序设计中,发现了自己有很多不足的地方,更加深了我对事件监听各方方法的实现功能,布局管理的应用和GUI各组建的使用的认识更加认识到要努力自己解决问题,无论是通过书籍还是网络资料,自己动手实践,这样更能加深印象,加强记忆,也能认识到自己的不足,促使自己不得不去提高自在此次己的编程水平,每一次的实践都是对自己的能力的检测,都是一次宝贵的经验,也是强迫自己学习的一种有效方式。
五、附录—主要源程序代码及运行结果import java.awt.*;import javax.swing.JTextField;public class Calculator extends Frame { GridLayout g1,g2,g3; Panel p0,p1,p2,p3; JTextField f1;TextField f2; Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10, b11,b12,b13,b14,b15,b16,b17,b18, b19,b20,b21,b22,b23,b24,b25,b26; StringBuffer s; double x,y;int z;static double m; public Calculator(){ g1=new GridLayout(1,4,10,0); g2=new GridLayout(4,1,0,15); g3=new GridLayout(4,5,10,15); f1=new JTextField(27); f1.setHorizontalAlignment(JTextField.RIGHT); f1.setForeground(Color.BLACK); f1.setFont(new Font("宋体",Font.BOLD,19)); f1.setText("0"); f1.setEditable(false); f2=new TextField(10); f2.setForeground(Color.RED); f2.setEnabled(false); b0=new Button("Backspace"); b0.setForeground(Color.RED); b0.addActionListener(new Bt()); b1=new Button("CE"); b1.setForeground(Color.RED); b1.addActionListener(new Bt()); b2=new Button("C"); b2.setForeground(Color.RED); b2.addActionListener(new Bt()); b3=new Button("sin"); b3.setForeground(Color.RED); b3.addActionListener(new Bt()); b4=new Button("cos"); b4.setForeground(Color.RED); b4.addActionListener(new Bt()); b5=new Button("tan"); b5.setForeground(Color.RED); b5.addActionListener(new Bt()); b6=new Button("log"); b6.setForeground(Color.RED); b6.addActionListener(new Bt()); b7=new Button("7"); b7.addActionListener(new Bt()); b8=new Button("8"); b8.addActionListener(new Bt()); b9=new Button("9"); b9.addActionListener(new Bt()); b10=new Button("/"); b10.addActionListener(new Bt()); b11=new Button("sqrt"); b11.addActionListener(new Bt()); b12=new Button("4"); b12.addActionListener(new Bt()); b13=new Button("5"); b13.addActionListener(new Bt()); b14=new Button("6"); b14.addActionListener(new Bt()); b15=new Button("*"); b15.addActionListener(new Bt()); b16=new Button("%"); b16.addActionListener(new Bt()); b17=new Button("1"); b17.addActionListener(new Bt()); b18=new Button("2"); b18.addActionListener(new Bt()); b19=new Button("3"); b19.addActionListener(new Bt()); b20=new Button("-"); b20.addActionListener(new Bt()); b21=new Button("1/X"); b21.addActionListener(new Bt()); b22=new Button("0"); b22.addActionListener(new Bt()); b23=new Button("+/-"); b23.addActionListener(new Bt()); b24=new Button("."); b24.addActionListener(new Bt()); b25=new Button("+"); b25.addActionListener(new Bt()); b26=new Button("="); b26.addActionListener(new Bt()); p1=new Panel(); p2=new Panel();p3=new Panel();p0=new Panel(); s=new StringBuffer(); p0.add(f1); p0.setBounds(10,25,300,40); p1.setLayout(g1); p1.add(f2);p1.add(b0);p1.add(b1);p1.add(b2); p1.setBounds(10,65,300,25); p2.setLayout(g2); p2.add(b3);p2.add(b4);p2.add(b5);p2.add(b6); p2.setBounds(10,110,40,150); p3.setLayout(g3); p3.add(b7);p3.add(b8);p3.add(b9);p3.add(b10); p3.add(b11);p3.add(b12);p3.add(b13);p3.add(b14); p3.add(b15);p3.add(b16);p3.add(b17);p3.add(b18); p3.add(b19);p3.add(b20);p3.add(b21);p3.add(b22); p3.add(b23);p3.add(b24);p3.add(b25);p3.add(b26); p3.setBounds(60,110,250,150); setLayout(null); add(p0);add(p1);add(p2);add(p3); setResizable(false); addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvent e1){System.exit(0);}}); setBackground(Color.lightGray); setBounds(100,100,320,280); setVisible(true);} class Bt implements ActionListener{ public void actionPerformed(ActionEvent e) { try{ if(e.getSource()==b1){ f1.setText("0"); s.setLength(0);} else if(e.getSource()==b23){ x=Double.parseDouble(f1.getText().trim()); f1.setText(""+"-"+x); } else if(e.getSource()==b25){ x=Double.parseDouble(f1.getText().trim()); s.setLength(0);y=x;z=0;} else if(e.getSource()==b20){ x=Double.parseDouble(f1.getText().trim()); s.setLength(0);y=x;z=1;} else if(e.getSource()==b15){ x=Double.parseDouble(f1.getText().trim()); s.setLength(0);y=x;z=2;} else if(e.getSource()==b10){ x=Double.parseDouble(f1.getText().trim()); s.setLength(0);y=x;z=3; }else if(e.getSource()==b3){ x=Double.parseDouble(f1.getText().trim()); s.setLength(0); y=Math.toRadians(x); f2.setText(""+(Math.sin(y))); f1.setText("sin("+x+")"+"=");} else if(e.getSource()==b4){ x=Double.parseDouble(f1.getText().trim()); s.setLength(0); y=Math.toRadians(x); f2.setText(""+(Math.cos(y))); f1.setText("cos("+x+")"+"=");} else if(e.getSource()==b5){ x=Double.parseDouble(f1.getText().trim()); s.setLength(0); y=Math.toRadians(x); f2.setText(""+(Math.tan(y))); f1.setText("tan("+x+")"+"=");} else if(e.getSource()==b6){ x=Double.parseDouble(f1.getText().trim());y=x; f1.setText("数学格式异常"); if(y<=0) f1.setText("指数不能小于或等于零"); else f2.setText(""+Math.log(x)); f1.setText("log("+x+")"); s.setLength(0); y=0d;} else if(e.getSource()==b26){ s.setLength(0); switch(z){ case 0:{f2.setText(""+(y+x)); f1.setText(y+"+"+x+"="); }break; case 1:{f2.setText(""+(x-y)); f1.setText(y+"-"+x+"="); }break; case 2:{f2.setText(""+(y*x)); f1.setText(y+"×"+x+"="); }break; case 3:{f2.setText(""+(y/x)); f1.setText(y+"/"+x+"="); }break;}} else if(e.getSource()==b24){if(f1.getText().trim ().indexOf('.')!=-1){} else{ if(f1.getText().trim().equals("0")){ s.setLength(0); f1.setText((s.append("0"+e.getActionCommand ())).toString());} else if(f1.getText().trim().equals("")){} else{ f1.setText(s.append(e.getActionCommand()).toString()); }}y=0d;} else if(e.getSource()==b11){ x=Double.parseDouble(f1.getText().trim()); y=x; f1.setText("数学格式异常"); if(y<0)f1.setText("负数没平方根"); else f2.setText(""+Math.sqrt(x)); f1.setText("sqrt("+x+")"); s.setLength(0); y=0d;} else if(e.getSource()==b16){ x=Double.parseDouble(f1.getText().trim()); f2.setText(""+(0.01*x)); f1.setText(x+"/100"); s.setLength(0); y=0d;} else if(e.getSource()==b21){ x=Double.parseDouble(f1.getText().trim()); if(x==0){f1.setText("除数不能为0");} else{f2.setText(""+(1/x)); f1.setText("1"+"/"+x);} s.setLength(0); y=0d;} else if(e.getSource()==b0){ if(f1.getText().trim().equals("0")){ if(s.length()!=1){ f1.setText(s.delete (s.length()-1,s.length()).toString());} else{ f1.setText("0"); s.setLength(0);}} y=Double.parseDouble(f1.getText().trim());} else{ f1.setText(s.append(e.getActionCommand ()).toString()); x=Double.parseDouble(f1.getText().trim());}} catch(NumberFormatException e3){ f1.setText("错误");} catch(StringIndexOutOfBoundsException e4){ f1.setText("错误");}}} public static void main(String args[]){ new Calculator();}} 六、指导老师评语及成绩。