1、JAVA程序课程设计计算器编辑程序课程设计-JAVA实验报告 开课院系:经济管理学院 实 验:JAVA课程设计 班 级:信管111 1、实验内容与步骤 将下列程序,改造成算器,要求 1、 增加乘、除、减法运算 2、 改正现行的近似计算,即现在用7.6+7.8=15.39999999这个问题 3、(选作)进行乘方、开方、取模运算 4、(选作)进行统计功能开发,功能自定义。比如多个数的平均值求法等 5、(选作)实现二进制运算 二、系统分析 利用JAVA程序设计开发计算器,在老师的指导和程序代码的改编下,最终实现计算器的加、减、乘、除、开方、取模、乘方等功能,除此之外,还将解决该计算器的近似计算问题
2、。课程设计的计算器虽然功能不是很全面,但是也实现了一些简单的计算功能。三、系统总体设计或详细设计(简单写出即可)用JAVA编译计算器,系统面板包括了一组button控件,控件采用5X4的布局方式,再将界面大小控制在600x480大小,其中包括09数字、还有+、*、/、乘方、取模、开方等功能。(如图)一、计算加法12+13二、计算减法3626三、计算乘法5*12四、计算除法36/12五、计算乘方8的2次方六、计算开方49的开方七、计算取模,10除4八、解决近似计算,7.6+7.8四、程序代码import java.awt.*;import java.awt.event.*;import java
3、x.swing.*;import java.text.DecimalFormat;import java.lang.Math;public class Calculator extends JFrame implements ActionListenerDecimalFormat df=(DecimalFormat)DecimalFormat.getInstance(); private JPanel jPanel1,jPanel2; private JTextField resultField; private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1
4、,b2,b3,b4,b5,b6,b7,f1,f2,f3; private boolean end,add,sub,mul,div,pow,sqr,mod; private String str; private double num1,num2; public Calculator() super(Calculate); setSize(600,480); Container con=getContentPane(); con.setLayout(new BorderLayout(); jPanel1=new JPanel(); jPanel1.setLayout(new GridLayout
5、(1,1); jPanel2=new JPanel(); jPanel2.setLayout(new GridLayout(4,5); resultField=new JTextField(0); jPanel1.add(resultField); con.add(jPanel1,BorderLayout.NORTH); s1=new JButton( 1 ); s1.addActionListener(this); s2=new JButton( 2 ); s2.addActionListener(this); s3=new JButton( 3 ); s3.addActionListene
6、r(this); s4=new JButton( 4 ); s4.addActionListener(this); s5=new JButton( 5 ); s5.addActionListener(this); s6=new JButton( 6 ); s6.addActionListener(this); s7=new JButton( 7 ); s7.addActionListener(this); s8=new JButton( 8 ); s8.addActionListener(this); s9=new JButton( 9 ); s9.addActionListener(this
7、); s0=new JButton( 0 ); s0.addActionListener(this); b1=new JButton( + ); b1.addActionListener(this); b2=new JButton( - ); b2.addActionListener(this); b3=new JButton( * ); b3.addActionListener(this); b4=new JButton( / ); b4.addActionListener(this); b5=new JButton( ); b5.addActionListener(this); b6=ne
8、w JButton( sqr ); b6.addActionListener(this); b7=new JButton( % ); b7.addActionListener(this); f1=new JButton( . ); f1.addActionListener(this); f2=new JButton( = ); f2.addActionListener(this); f3=new JButton( OFF ); f3.addActionListener(this); jPanel2.add(s1); jPanel2.add(s2); jPanel2.add(s3); jPane
9、l2.add(b1); jPanel2.add(b5); jPanel2.add(s4); jPanel2.add(s5); jPanel2.add(s6); jPanel2.add(b2); jPanel2.add(b6); jPanel2.add(s7); jPanel2.add(s8); jPanel2.add(s9); jPanel2.add(b3); jPanel2.add(b7); jPanel2.add(f1); jPanel2.add(s0); jPanel2.add(f2); jPanel2.add(b4); jPanel2.add(f3); con.add(jPanel2,
10、BorderLayout.CENTER); public void num(int i) String s = null; s=String.valueOf(i); if(end) resultField.setText(0); end=false; if(resultField.getText().equals(0) resultField.setText(s); else str = resultField.getText() + s; resultField.setText(str); public void actionPerformed(ActionEvent e) if(e.get
11、Source()=s1) num(1); else if(e.getSource()=s2) num(2); else if(e.getSource()=s3) num(3); else if(e.getSource()=s4) num(4); else if(e.getSource()=s5) num(5); else if(e.getSource()=s6) num(6); else if(e.getSource()=s7) num(7); else if(e.getSource()=s8) num(8); else if(e.getSource()=s9) num(9); else if
12、(e.getSource()=s0) num(0); else if(e.getSource()=b1) sign(1); else if(e.getSource()=b2) sign(2); else if(e.getSource()=b3) sign(3); else if(e.getSource()=b4) sign(4); else if(e.getSource()=b5) sign(5); else if(e.getSource()=b6) sign(6); else if(e.getSource()=b7) sign(7); else if(e.getSource()=f1) st
13、r=resultField.getText(); if(str.indexOf(.)=1) str+=.; resultField.setText(str); else if(e.getSource()=f2) num2=Double.parseDouble(resultField.getText(); if(add) num1=num1 + num2; else if(sub) num1=num1 - num2; else if(mul) num1=num1 * num2; else if(div) num1=num1 / num2; else if(pow) num1=Math.pow(n
14、um1,num2); else if(sqr) num1=Math.sqrt(num1); else if(mod) num1=num1 % num2; df.setMaximumFractionDigits(2); String x=df.format(num1); resultField.setText(x); end=true; if(e.getSource()=f3) System.exit(0); public void sign(int s) if(s=1) add=true; sub=false; mul=false; div=false; pow=false; sqr=fals
15、e; mod=false; else if(s=2) add=false; sub=true; mul=false; div=false; pow=false; sqr=false; mod=false; else if(s=3) add=false; sub=false; mul=true; div=false; pow=false; sqr=false; mod=false; else if(s=4) add=false; sub=false; mul=false; div=true; pow=false; sqr=false; mod=false; else if(s=5) add=fa
16、lse; sub=false; mul=false; div=false; pow=true; sqr=false; mod=false; else if(s=6) add=false; sub=false; mul=false; div=false; pow=false; sqr=true; mod=false; else if(s=7) add=false; sub=false; mul=false; div=false; pow=false; sqr=false; mod=true; num1=Double.parseDouble(resultField.getText(); end=t
17、rue; public static void main(String args) Calculator th1=new Calculator(); th1.show(); 五、收获体会 通过这次课程设计实践,我深深地体会到java程序设计的内涵,我不但进一步理解到老师上课所传授的知识,而且在自我程序设计上也有了一个完美的突破。通过老师的指导和自己查阅资料,最终把计算器给编译出来了。这次设计的计算器,刚开始觉得很难,但是后来觉得很有趣,通过自己慢慢的摸索和请教老师,我一步一个脚印,将老师所给的代码,进一步解读,慢慢的自己也就会了,其实大体上都是差不多的,只要按照老师所给的加法和减法程序,那么乘法和除法以及乘方、取模等计算功能都可以将其模仿出来。经过了一天的自习研究,该计算器最终被我做出来了,最终实现计算器的加、减、乘、除、开方、取模、乘方等功能,除此之外,还将解决该计算器的近似计算问题。课程设计的计算器虽然功能不是很全面,但是也实现了一些简单的计算功能。 所以这次课程设计虽然比较困难,但是只要态度认真,最终就可以将计算器攻克,将理论与实践相结合,那么就会收获到你想要的结果。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1