当前位置:文档之家› java小程序设计

java小程序设计

课程设计报告设计题目:用Java编的计算器院系班级设计者学号指导教师设计时间前言:通过上课学习了java程序设计的基本原理和模式,这次又以java语言为基本结合其图形应用界面来设计一个包括加、减、乘、除。

还有带括号以及带负号、小数运算的计算程序。

系统功能和使用说明:一.硬件环境:本系统适用于现应用各种类型的计算机,内存容量为128MB以上,不必配备的外部附加设备。

二.软件环境:本系统的设计采用的是JAVA语言,应在JDK 5.0或者其更高版软件环境下本使用。

三.系统流程图:1.下图是本程序的运行后的界面,其标注是此组件在程序中的名字2.系统流程图中各个模块的功能说明:本程序一共包括:构造方法A5i13w(),setup(), windowClosing(), actionPerformed(), appendchar(), countall(), counting(), Iscountnum()和一个windowcloser类。

(1). A5i13w()是构造方法,在程序创建其对象时就调用,初始化次程序的事件的监听机制。

(2). setup()是我定义的方法,是用来初始化程序的图形应用界面,由A5i13w()来调用。

(3). windowClosing()是用来响应用户的关闭功能。

(4). actionPerformed()是用来处理每个事件的响应的方法(5). appendchar()方法的作用是在用户产生事件后向Textshow追加产生此事件的字符串(6).countall()的意义是在用户按“=”号后,从最里层括号向最外层逐层把括号里的内容传递给counting()方法,并负责对括号本身是否符合数学语法要求进行检验。

(7).counting()方法的作用是首先对无括号的数学表达式有关的数学计算符号进行调整,并检验所有的数学符号,如发现错误的数学符号立即在Labelnow中显示出来。

(8).Iscountnum()方法的作用是把“+ - × ÷ % . ( )”算术符号转化相应的数字,便于程序的流程控制。

3.此计算器的基本的计算过程说明:当用户发出计算整个数学表达式的命令后,本计算器首先是对用户向Textshow(即文本域)输入的数学表达式经行括号索检。

如果有一层括号,则对括号的内容进行优先计算。

如果有多层括号,则按先计算里层括号后计算外层括号的原则进行计算。

如果没有发现括号,则只进行一次无括号计算即可。

现在是关于要计算的数学表达式,本计算器也能检验此即将计算的数学表达式是否符合数学语法规则。

如发现符合数学规则的表达式,则在Labelnow中提示用户,并且Labelnow的背景颜色将变成红色。

在计算的开始,计算的过程,计算的出错,计算的成功,我都设计好不同的背景颜色与之对应。

用户要注意Labelnow的提示信息。

在提示信息的对应要求,我认为次程序是不符合要求的。

有几条提示信息只是告诉用户你输入的表达式有问题,并不能很明确的告诉用户他的出错的地方。

这是我作为程序的开发者对错误的分类标准把握的不好造成的。

是的,这是我的作品的不足。

但是,我要说一句话,本程序的重点是计算出合法的数学表达式,而对非法的表达式过于重视是否有一点本末倒置呢?四.程序执行的流程图五.程序的调试出错情况:我在程序的调试过程中最容易发生的错误就是:就是我画红线的句子即是“Exception in thread "AWT-EventQueue-0" ng.NumberFormatException”。

用户输入的数学表达式在我设计的程序转化为数学计算形式是不可行的。

假如是正确的数学表达式,则我的程序在处理数字转化方面是有问题的,为了解决这个问题我花大时间去重写我的程序的核心代码。

终于,我找到了一种捷径——把无括号的数学表达式进行校正合理的转化为在数字和数字之间只有一个运算符。

虽然我就摆平这个问题,但是我为了解程序执行的具体的过程,在DOS中都有关于每一步的提示信息。

六.结论:1.这次课程设计心得是的,JAVA的功能是十分强大的。

借助其强大的平台。

我的程序不仅有不错(至少我这样认为)的图形界面,而且其程序本身的功能也得到很好的发挥。

次程序支持一般的四则运算(包括括号)。

更有状态栏,以便程序在执行过程中向用户反馈一些必要的信息。

2.总结通过这次课程设计,我真的在实践中学到的不仅是课本知识的巩固和提高,而且还有在实践中使我着手解决不少程序设计的细节问题。

它们包括程序算法的设计和图形界面的构建的两大方面。

而我个人认为我越早接触,越多接触,越快解决对我本人缩短次过程有重要的意义。

七.原程序import java.awt.*;import java.awt.event.*;public class A5i13w extends Frame implements ActionListener{private Label Labelshow=new Label("显示区:");private Label Labelsay=new Label("状态:",Label.CENTER);private Label Labelnow=new Label("欢迎使用我,艾全通为你带来的科学计算器",Label.LEFT); private Label Labeltime=new Label("暂且为空");//为以后添加其他的辅助功能private TextField Textshow=new TextField(40);private Panel pan1=new Panel();private Panel pan2=new Panel();private Panel pan3=new Panel();private Button button1=new Button("1");private Button button2=new Button("2");private Button button3=new Button("3");private Button button4=new Button("4");private Button button5=new Button("5");private Button button6=new Button("6");private Button button7=new Button("7");private Button button8=new Button("8");private Button button9=new Button("9");private Button button10=new Button("0");private Button button11=new Button("+");private Button button12=new Button("-");private Button button13=new Button("× ");private Button button14=new Button("÷");private Button button15=new Button("%");private Button button16=new Button("(");private Button button17=new Button(")");private Button button18=new Button("清零");private Button button19=new Button(".");private Button button20=new Button("=");private Font wordFont1 =new Font("TimesRoman",Font.BOLD,13);private Font wordFont2 =new Font("TimesRoman",Font.BOLD,30);public A5i13w() //构造方法来初始化本程序的变量{super("我的科学计算器");setup();button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);button4.addActionListener(this);button5.addActionListener(this);button6.addActionListener(this);button7.addActionListener(this);button8.addActionListener(this);button9.addActionListener(this);button10.addActionListener(this);button11.addActionListener(this);button12.addActionListener(this);button13.addActionListener(this);button14.addActionListener(this);button15.addActionListener(this);button16.addActionListener(this);button17.addActionListener(this);button18.addActionListener(this);button19.addActionListener(this);button20.addActionListener(this);addWindowListener(new windowcloser());setResizable(false); //设置此图形界面是不可以改变大小的pack();setVisible(true);}public void setup() //搭建次程序的图形应用界面{pan1.setLayout(new BorderLayout());Labelshow.setFont(wordFont1);pan1.add("West",Labelshow);Textshow.setFont(wordFont1);pan1.add("Center",Textshow);pan2.setLayout(new GridLayout(4,5,5,6));button1.setFont(wordFont2);pan2.add(button1);button2.setFont(wordFont2);pan2.add(button2);button3.setFont(wordFont2);pan2.add(button3);button4.setFont(wordFont2);pan2.add(button4);button5.setFont(wordFont2);pan2.add(button5);button6.setFont(wordFont2);pan2.add(button6);button7.setFont(wordFont2);pan2.add(button7);button8.setFont(wordFont2);pan2.add(button8);button9.setFont(wordFont2);pan2.add(button9);button10.setFont(wordFont2);pan2.add(button10);button11.setFont(wordFont2);pan2.add(button11);button12.setFont(wordFont2);pan2.add(button12);button13.setFont(wordFont2);pan2.add(button13);button14.setFont(wordFont2);pan2.add(button14);button15.setFont(wordFont2);pan2.add(button15);button16.setFont(wordFont2);pan2.add(button16);button17.setFont(wordFont2);pan2.add(button17);button18.setFont(wordFont2);pan2.add(button18);button19.setFont(wordFont2);pan2.add(button19);button20.setFont(wordFont2);pan2.add(button20);pan3.setLayout(new FlowLayout(FlowLayout.CENTER,2,2)); Labelsay.setFont(wordFont1);Labelsay.setBackground(Color.blue );pan3.add(Labelsay);Labelnow.setFont(wordFont1);Labelnow.setBackground(Color.gray);pan3.add(Labelnow);Labeltime.setBackground(Color.gray);Labeltime.setFont(wordFont1);pan3.add(Labeltime);setLayout(new BorderLayout( ) );add("North",pan1);add("Center",pan2);add("South",pan3);}public class windowcloser extends WindowAdapter //通过构建类部类来使本程序可通过图形关闭{public void windowClosing(WindowEvent w){ System.exit(0); }}public void actionPerformed(ActionEvent e) //事件响应的处理{if(e.getSource()==button18){ Textshow.setEditable(true);Textshow.setText("");Labelnow.setBackground(Color.green);Labelnow.setText("请按标准的数学形式输入数字!"); }else{if(e.getSource()==button20){String QU=new String(Textshow.getText( ) );System.out.println();System.out.println(); //在DOS中显示计算过程前,先空两行。

相关主题