标签:
1.第一阶段目标 - 把计算的功能封装成类。
2.设计测试用例:用白盒与黑盒测试设计技术,为计算核心设计测试用例。
3.在实验环境中(如MyEclipse集成开发环境+Junit测试框架)运行测试用例,分析测试结果,找出程序问题,给出改进前后的代码对照。
结对子:
201306114454 陈汝婷 http://home.cnblogs.com/u/chenruting/
201306114448 练丽云 http://i.cnblogs.com/EditPosts.aspx?opt=1
import java.text.DecimalFormat; import java.util.Scanner; public class Core { static final int NUM_MAX_OPERATOR = 1; // 最多4 个运算符 static final int NUM_SCALE = 2; // 数值范围是 -1000 到 1000 static final int NUM_PRECISION = 3; // 精度是小数点后两位 public static int i=0; public long z; public double calc(String s) { double result = 0.0d; // 这个方法中我可以计算两个数的+ - * / return result; } public long add(long x,long y) { return x+y; } import static org.junit.Assert.*; import org.junit.Test; public class CoreTest { @SuppressWarnings("deprecation") @Test public void test() { Core core = new Core(); long d = core.calc("+",12589456L,25364563L); assertEquals(37954019, d); } @Test public void jianfatest() { Core core = new Core(); long d = core.calc("-",2,1); assertEquals(1, d); } @Test public void chengfatest() { Core core = new Core(); long d = core.calc("*",2,1); assertEquals(2, d); } @Test public void cftest() { Core core = new Core(); long d = core.calc("/",6,3); assertEquals(2, d); } } public long calc(String s, long x, long y) { Scanner scanner = new Scanner(System.in); if (s.equals("+")) { z=(x+y); System.out.println(z); } else if (s.equals("-")) { z=x-y; System.out.println(z); } else if (s.equals("*")) { z=x*y; System.out.println(z); } else if (s.equals("/")) { if (y != 0) { z=x/y; System.out.println(x / y); } else { i++; if(i<3) { System.out.println("分母不能为零!"); System.out.print("请重新输入第二个数:"); y = scanner.nextLong(); calc(s,x,y); } else{ System.out.println(i); System.out.println(exit(i=3)); } } } else { System.out.println("输入不符合要求!"); }return z; } public String geta(){ return String.valueOf(z); } private char[] exit(int j) { // TODO 自动生成的方法存根 return null; } } import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import java.awt.GridLayout; import java.awt.GridBagLayout; import javax.swing.JToolBar; import javax.swing.JLabel; import java.awt.Color; import javax.swing.JButton; import java.awt.Font; public class Test extends JFrame implements ActionListener{ public JButton bt1; JLabel lb1; public Test() { getContentPane().setBackground(Color.PINK); setTitle("\u56DB\u5219\u8FD0\u7B97"); getContentPane().setLayout(null); setSize(200,200); lb1 = new JLabel("\u6B22\u8FCE\u8FDB\u5165\u56DB\u5219\u8FD0\u7B97\u7CFB\u7EDF"); lb1.setForeground(Color.BLUE); lb1.setBounds(10, 10, 159, 15); getContentPane().add(lb1); bt1 = new JButton("\u5F00\u59CB"); bt1.setBounds(41, 63, 98, 49); getContentPane().add(bt1); bt1.addActionListener(this); // bt1.addActionListener(new ActionListener() { // public void actionPerformed(ActionEvent arg0) { // //Test1.main(null); // // } // }); setVisible(true); //将窗口设置为可视 setResizable(false); setAlwaysOnTop(true);//设置此窗口永远为最上方 是window的方法 setLocationRelativeTo(null); //让窗口在屏幕的正中间显示 } public void actionPerformed(ActionEvent arg0) { if(arg0.getSource()==bt1){ new Test1(); } } public static void main(String[] args) { Test test=new Test(); } } import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; public class Test1 extends JFrame implements ActionListener{ private JTextField T1; private JTextField T2; private JTextField T3; private JTextField T4; private JLabel l0 ; private JLabel l1; private JLabel l2; private JLabel label_3; private JButton button; public long x; public long y ; public String x1; public String y1 ; public String s; public Test1() { setTitle("SI \u5219\u8FD0\u7B97"); getContentPane().setLayout(null); setSize(400,400); JButton btnNewButton = new JButton("\u7EE7\u7EED"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); btnNewButton.setBounds(130, 239, 93, 23); getContentPane().add(btnNewButton); l0 = new JLabel("\u8BF7\u8F93\u5165\u7B2C\u4E00\u4E2A\u6570\uFF1A"); l0.setBounds(38, 64, 131, 15); getContentPane().add(l0); l1 = new JLabel("\u8BF7\u8F93\u5165\u7B2C\u4E8C\u4E2A\u6570\uFF1A"); l1.setBounds(38, 89, 131, 15); getContentPane().add(l1); l2 = new JLabel("\u8BF7\u8F93\u5165\u8FD0\u7B97\u7B26\uFF1A"); l2.setBounds(38, 114, 131, 15); getContentPane().add(l2); T1 = new JTextField(); T1.setBounds(206, 61, 66, 21); getContentPane().add(T1); T1.setColumns(10); T1.addActionListener(this); T2 = new JTextField(); T2.setColumns(10); T2.setBounds(206, 86, 66, 21); getContentPane().add(T2); T2.addActionListener(this); T3 = new JTextField(); T3.setColumns(10); T3.setBounds(206, 111, 66, 21); getContentPane().add(T3); T3.addActionListener(this); button = new JButton("\u7ED3\u675F"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); button.setBounds(264, 239, 93, 23); getContentPane().add(button); label_3 = new JLabel("\u8FD0\u7B97\u7ED3\u679C\uFF1A"); label_3.setBounds(38, 139, 131, 15); getContentPane().add(label_3); T4 = new JTextField(); T4.setColumns(10); T4.setBounds(206, 142, 66, 21); getContentPane().add(T4); T4.addActionListener(this); this.setVisible(true); } public static void main(String[] args) { new Test1(); Test.setDefaultLookAndFeelDecorated(false); } public void actionPerformed(ActionEvent evt) { if(evt.getSource()==T1){ T2.requestFocusInWindow(); } else if(evt.getSource()==T2){ T3.requestFocusInWindow(); } else if(evt.getSource()==T3){ T3.requestFocusInWindow();} else{ x1=T1.getText(); y1=T2.getText(); s=T3.getText(); x=Integer.valueOf(x1); y=Integer.valueOf(y1); Core core=new Core(); T4.setText(core.geta()); } } }
运行结果:
减法测试:
乘法:
除法:
当除数为0的时候会有2次重新输入输入的机会
开始界面
标签:
原文地址:http://www.cnblogs.com/Lianliyun/p/4468391.html