实验报告|
|
实验名称Java程序设计综合实验
课程名称Java程序设计
|
|
专业班级:学生姓名:
学号:成绩:
指导教师:实验日期:
一、实验目的
综合应用所学的知识分析问题、解决问题,提高实际编程能力及程序调试能力。二、实验内容及要求
三、实验仪器与设备
Eclipse
四、问题分析与系统设计
要做一个简单的计算器,实现的功能:
1、基本运算;
2、支持浮点数表示(运算);
3、连续操作;
4、得出结果后不用清0进行运算;
5、backspace:退格键,删除一个数字;
6、C键:清除当前计算,开始新的运算;
7、CE键:屏幕清零键,清除当前数字,可以重新输入按CE前输入的数字。
程序=结构+算法
首先设计的是程序的结构。程序中主要是一calculator 公共类(implements ActionListener),包括构造函数(用于计算器的外观设置,布局)。在设计一个界面时先设计Frame容器,在north添加一个TextField,center添加一个Panel容器。再
在Panel容器中添加两个panel容器,第一个panel中添加“CE”(西端)、“backspace”(中间)、“C”(东端)三个按钮。第二个panel容器中GridLaout把容器分成四行五列,添加1,2,3,4,5,6,7,8,9,0,+,-,*,/,%,+\-,1/x,sqrt,=, 这些按钮。定义各个按钮逐个添加给ActionListener监听器进行注册。界面构造号后接着定义算法。定义加减乘除,倒数,开方等这些基本运算包含于actionPerformed方法(用于事件的响应),一个public static void main(String arg[])程序运行入口。在main 中创建一个calculator类对象,在创建对象的同时,初始化各组件,创建图形用户界面。
五、实验结果
界面如图:
能实现的功能:
1、支持“+”、“-”、“*”、“/”运算,以及开平方根(按“sqart”),将结果转换为百分数(按“%”),取倒运算(按“1/x”)。
2、支持浮点数表示(运算)
例如:0.85+0.25=1.10
3、连续操作
例如:1+2+3=6
4、得出结果后不用清0进行运算
例如:4+5×2=14 +5=19
5、backspace:退格键,删除一个数字
6、C键:清除当前计算,开始新的运算。
7、CE键:屏幕清零键,清除当前数字,可以重新输入按CE前输入的数字。
六、总结与体会
实验达到了预期的目标。通过此次的编程,我学会了用Java语言编写简单的软件,增强了我对学习Java的兴趣。编程过程中,总体设计主要是窗口与组件的应用,具体执行则是利用Java语言设计算法,而难点也在算法的设计上。虽然程序大体上符合要求,但由于第一次编写这么复杂的程序,程序仍然存在一些漏洞,譬如由于未进行异常处理,当连续输入运算符是程序会出错,还有若一直按小数点的话会得到一连串的小数点。由于最近忙着考试没有时间修改,考试结束后一定认真修改这些问题。我觉得在编程时应该注意排版对齐,这样不论是自己检查程序还是别人阅读程序都会更加方便。如果能添加备注的话以后阅读程序会更容易些。在以后的编程中我会吸取经验,养成认真、严谨的编程习惯。
附件:源程序清单
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class calculator implements ActionListener
{
prvate boolean judge = true;
private boolean flag = false;
private String fh = "";
private String num1 = "";
private String num2 = "";
JFrame f=null;
JTextField tf;
JButton b;
public void jiemian()
{
f=new JFrame("计算器");
Container c=f.getContentPane();
tf=new JTextField();
tf.setHorizontalAlignment(JTextField.RIGHT);
c.add(tf,"North");
JPanel pn3=new JPanel(new BorderLayout());
c.add(pn3,"Center");
JPanel pn2=new JPanel();
pn2.setLayout(new BorderLayout());
JPanel pn1=new JPanel();
pn1.setLayout(new GridLayout(4,5));
pn3.add(pn2,"North");
pn3.add(pn1);
b=new JButton("CE");
b.setForeground(Color.BLUE);
b.addActionListener(this);
pn2.add(b,"East");
b=new JButton("C");
b.setForeground(Color.BLUE);
pn2.add(b,"West");
b=new JButton("backspace");
b.setForeground(Color.BLUE);
b.addActionListener(this);
pn2.add(b,"Center");
b=new JButton("1");
b.addActionListener(this); pn1.add(b);
b=new JButton("2");
b.addActionListener(this); pn1.add(b);
b=new JButton("3");
b.addActionListener(this); pn1.add(b);
b=new JButton("+");
b.setForeground(Color.BLUE); b.addActionListener(this); pn1.add(b);
b=new JButton("sqrt");
b.setForeground(Color.BLUE); b.addActionListener(this); pn1.add(b);
b=new JButton("4");
b.addActionListener(this); pn1.add(b);
b=new JButton("5");
b.addActionListener(this); pn1.add(b);
b=new JButton("6");
b.addActionListener(this); pn1.add(b);
b=new JButton("-");
b.setForeground(Color.BLUE);
b.addActionListener(this); pn1.add(b);
b=new JButton("%");
b.setForeground(Color.BLUE);
b.addActionListener(this); pn1.add(b);
b=new JButton("7");
b.addActionListener(this); pn1.add(b);
b=new JButton("8");
b.addActionListener(this); pn1.add(b);
b=new JButton("9");
b.addActionListener(this); pn1.add(b);
b=new JButton("*");
b.setForeground(Color.BLUE);
b.addActionListener(this); pn1.add(b);
b=new JButton("1/x");
b.setForeground(Color.BLUE);
b.addActionListener(this); pn1.add(b);
b=new JButton("0");
b.addActionListener(this); pn1.add(b);
b=new JButton(".");
b.addActionListener(this); pn1.add(b);
b=new JButton("+/-");
b.setForeground(Color.RED);
b.addActionListener(this);
pn1.add(b);
b=new JButton("/");
b.setForeground(Color.BLUE);
b.addActionListener(this);
pn1.add(b);
b=new JButton("=");
b.setForeground(Color.BLUE);
b.addActionListener(this);
pn1.add(b);
f.setSize(325,325);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String temp = e.getActionCommand();
if(!("0123456789.".indexOf(temp)==-1))
{
if(flag)
{
String s=tf.getText();
tf.setText(s+temp);
}
else
{
tf.setText(temp);
flag = true;
}
}
else if(!("+-*/".indexOf(temp)==-1))
{
if(judge)
{
num1 = tf.getText();
flag = false;
judge = false;
tf.setText("");
}
else
{
flag = false;
num2 = tf.getText();
if(!num1.equals("") && !num2.equals(""))
{
double i1 = Double.parseDouble(num1);
double i2 = Double.parseDouble(num2);
if("+".equals(fh))
{
tf.setText(Double.toString(i1+i2));
flag = false;
}
else if("-".equals(fh))
{
tf.setText(Double.toString(i1-i2));
flag = false;
}
else if("*".equals(fh))
{
tf.setText(Double.toString(i1*i2));
flag = false;
}
else if("/".equals(fh))
{
tf.setText(Double.toString(i1/i2));
flag = false;}
}
num1 = tf.getText();num2 = ("");
}
fh = temp;
}
else if("=".equals(temp))
{
num2 = tf.getText();
if(!num1.equals("") && !num2.equals(""))
{
double i1 = Double.parseDouble(num1);
double i2 = Double.parseDouble(num2);
if("+".equals(fh))
{
tf.setText(Double.toString(i1+i2));
flag = false;
}
else if("*".equals(fh))
{
tf.setText(Double.toString(i1*i2));
flag = false;
}
else if("-".equals(fh))
{
tf.setText(Double.toString(i1-i2));
flag = false;
}
else if("/".equals(fh))
{
tf.setText(Double.toString(i1/i2));
flag = false;
}
}
num2 = tf.getText();num1=("");
}
else if("+/-".equals(temp))
{
String s = tf.getText();
if(!s.startsWith("-"))
tf.setText("-"+s);
else
tf.setText(s.substring(1));
flag=false;
}
else if("sqrt".equals(temp))
{
String s =tf.getText();
if (!s.startsWith("-"))
tf.setText(Double.toString(Math.sqrt(Double. parseDouble(tf.getText()))));
else tf.setText("Error");
flag=false;
}
else if("1/x".equals(temp))
{
String s=tf.getText();
double d=Double.parseDouble(s);
double d1=1/d;
tf.setText(Double.toString(d1));
flag=false;
}
else if("%".equals(temp))
{
String s=tf.getText();
double d=Double.parseDouble(s);
double d1=d*100;
tf.setText(Double.toString(d1)+"%");
flag=false;
}
else if("CE".equals(temp))
{
tf.setText("");
num2 = "";
flag = false;
}
else if("C".equals(temp))
{
tf.setText("");
num1 = "";
num2 = "";
flag = false;
judge = true;
}
else if("backspace".equals(temp))
{
String s = tf.getText();
if(s.length()>0){s = s.substring(0,s.length()-1);
tf.setText(s);
flag = true;
}
else
{
tf.setText("");
flag = false;
}
}
}
public static void main(String[] args)
{
new calculator().jiemian();
}
}