您的位置:首页 > 其它

作业5 四则运算 测试与封装 5.1

2015-04-30 11:19 267 查看
编译环境:Eclipse

开发人员:钟鹏升

开发时间:2015-04-30

更新的代码:

运用封装,新建Core类方法重载计算方法:

public void jbInit() throws Exception
{
this.setLayout(null);
L.setBounds(60, 150, 150, 40);
L.setFont(firstFont);
T.setBounds(190, 150, 80, 40);
B.setBounds(280, 145, 60, 50);
colorBtn.setBounds(380, 20, 60, 50);
L1.setBounds(40, 220, 400, 40);
L1.setFont(secondFont);
L2.setFont(thirdFont);
L3.setFont(thirdFont);
L4.setFont(thirdFont);
P1.setBounds(120, 300, 200, 200);
B.addActionListener(this);
colorBtn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
setBackground(new Color(
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128));
P1.setBackground(new Color(
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128));
}
});
Core core=new Core();
core.calc(op, a, b);
this.add(L);
this.add(T);
this.add(B);
this.add(L1);
this.add(P1);
this.add(colorBtn);
P1.add(L2);
P1.add(L3);
P1.add(L4);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==B)
{
t = T.getText();

/*      检查学号是否输入      */
if(!checkString(t)){
JOptionPane.showMessageDialog(null, "未输入数字!","提示",JOptionPane.ERROR_MESSAGE);
return;
}
else if((!checkInteger(t))){
T.setText("");
JOptionPane.showMessageDialog(null, "请输入数字!","提示",JOptionPane.ERROR_MESSAGE);
return;
}
else {
n++;
if(Double.parseDouble(t)==answer){
r++;
L1.setText("               答对了!");
}
else{
L1.setText("答错了!"+L.getText()+"的正确答案是:"+answer);
}
T.setText("");
if(n<=Integer.parseInt(Cal.number)){
Core core=new Core();
core.calc(op, a, b);
}
else
{
B.setEnabled(false);
L2.setText("本次测验共"+Integer.parseInt(Cal.number)+"题");
L3.setText("你答对了"+r+"题");
ra=(double)r/(n-1);
DecimalFormat fmt = new DecimalFormat("0.##%");
L4.setText("正确率为:"+fmt.format(ra));
}
}
}
}


Core类方法重载:

public class Core{
public void calc(int op,int a,int b){
a =new Random().nextInt(100);
b =new Random().nextInt(100);
op=new Random().nextInt(4);
switch(op)
{
case 0:
L.setText("题"+n+". "+a+"+"+b+"=");
answer=(double) (a+b);
break;
case 1:
L.setText("题"+n+". "+a+"-"+b+"=");
answer=(double) (a-b);
break;
case 2:
L.setText("题"+n+". "+a+"*"+b+"=");
answer=(double) (a*b);
break;
case 3:
while(b==0){
b =new Random().nextInt(100);
}
L.setText("题"+n+". "+a+"/"+b+"=");
BigDecimal   x   =   new   BigDecimal((double)a/b);
answer  =   x.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
break;
}
}
}


运用封装后:

封装作用:

   1、对象的数据封装特性彻底消除了传统结构方法中数据与操作分离所带来的种种问题,提高了程序的可复用性和可维护性,降低了程序员保持数据与操作内容的负担。

   2、对象的数据封装特性还可以把对象的私有数据和公共数据分离开,保护了私有数据,减少了可能的模块间干扰,达到降低程序复杂性、提高可控性的目的。

优点:

   1、重用。

   2、可以只关注于使用,而不关心具体的实现。

   3、面向对象的三大特征之一。

   4、具有安全性。

   5、防止代码冗余,也可以方便代码的调用,同时也可以防止不必要的错误。

   6、类内部的结构可以自由修改,可以对成员进行更精确的控制,隐藏信息,实现细节。

总结:

  由于原本的程序不是让用户输入数字来运算的,是自动生成的四则运算,目前更改的内容较少,仍需继续改进。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: