5.2和5.3

开发环境:

1.结对伙伴:叶煜稳 博客:http://www.cnblogs.com/doubi2wy/

2.分工:我负责2.3阶段,我的小伙伴负责完善一下5.1的测试与封装

3.源代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random; 
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import javax.swing.JFrame;

public class est 
{ 
public static void main(String [] args) 
{ 
     new MyWindow();
}

} 
class MyWindow extends JFrame implements ActionListener{
    Random r=new Random();
    int result=0;
    float result1=0.2f,result2=0.2f;
                  int m; 
    int sum=0;
    int num=0;
    int num1=0;
    float num2=0.2f;
    int a=0,b=0;
    int q=0,w=0;
    float j=0.1f,k=0.1f;
                  JLabel L1=new JLabel("请输入题数(最多不超过5题):");
                  JLabel L2=new JLabel("结果:");
                  JLabel L3=new JLabel("");
                  JTextField T1,T2,T3, T4;
                  JButton B1,B2,B3;
                  MyWindow(){
                  setTitle("四则运算");
                  this.setSize(300,150);
                  T1=new JTextField(10);
                  T2=new JTextField(10);
                  T3=new JTextField(10);
                  T4=new JTextField(10);
                  B1=new JButton("确定");
                  B2=new JButton("确定");
                  B3=new JButton("下一题");
                  T1.addActionListener(this);
                  T3.addActionListener(this);
                  B1.addActionListener(this);
                  B3.addActionListener(this);
                  B2.addActionListener(this);
                  setLayout(new FlowLayout(FlowLayout.CENTER,20,10));
                  add(L1);
                  add(T1);
                  add(B1);
                  add(T2);
                  add(B3);
                  add(T3);
                  add(B2);
                  add(L2);
                  add(T4);
                  add(L3);
                  setBounds(300,300,250,250);
                  setResizable(true);
                  setVisible(true);

}
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==B1){
        String str=T1.getText();
        num=Integer.parseInt(str);
        if(num<=5){
        m=(int)(Math.random()*4+1);
        if(m==3){
            a=r.nextInt()%1000; 
            b=r.nextInt()%1000;
            T2.setText(a+"+"+"("+b+")=?");
            result=a+b;
        }
        else if(m==1){    
        q=r.nextInt()%1000; 
        w=r.nextInt()%1000;
        T2.setText(q+"*"+"("+w+")"+"=?");
        result1=q*w;
        }
        else if(m==2){
        j=r.nextInt()%1000; 
        k=r.nextInt()%1000;
        if(k==0) 
        k++;
        T2.setText(j+"/"+"("+k+")"+"=?");
        result2=(j/k);
        }
        num--;
        T1.setText(null);
        }
        else if(m==4){
            a=r.nextInt()%1000; 
            b=r.nextInt()%1000;
            T2.setText(a+"-"+"("+b+")=?");
            result=a-b;
        }
        else{
            T1.setText(null);
            T4.setText("重新输入!");
        }
            }
    else if(e.getSource()==B3){
        if(num>0){
        m=(int)(Math.random()*4+1);
        if(m==3){
            a=r.nextInt()%1000; 
            b=r.nextInt()%1000;
            T2.setText(a+"+"+"("+b+")=?");
            result=a+b;
        }
        else if(m==1){    
        q=r.nextInt()%1000; 
        w=r.nextInt()%1000;
        T2.setText(q+"*"+"("+w+")"+"=?");
        result1=q*w;
        }
        else if(m==2){
        j=r.nextInt()%1000; 
        k=r.nextInt()%1000;
        if(k==0) 
        k++;
        T2.setText(j+"/"+"("+k+")"+"=?");
        result2=(j/k);
        }
        else if(m==4){
            a=r.nextInt()%1000; 
            b=r.nextInt()%1000;
            T2.setText(a+"-"+"("+b+")=?");
            result=a-b;
        }
        }
        else{
            T2.setText("答题结束!!");
        }
        num--;
    }
    else if(e.getSource()==B2){
        if(m==3){
           num1=Integer.parseInt(T3.getText());
            if(num1==result){
                T4.setText("回答正确!"); 
                sum=sum+1; 
            }
            else 
            {
                T4.setText("回答错误!"+result);
            }
            T3.setText(null);
        }
      else if(m==1){
             num1=Integer.parseInt(T3.getText());
           if(num1==result1){ 
            T4.setText("回答正确!"); 
            sum=sum+1;
            }
           else
           {
               T4.setText("回答错误!"+result1);
           }
           T3.setText(null);
        }
      else if(m==2){
          num2=(float)Double.parseDouble(T3.getText());
          if(num2==result2){
              T4.setText("回答正确!"); 
              sum=sum+1;
          }
          else 
          {
              T4.setText("回答错误!"+result2);
          }
          T3.setText(null);
          }
      else if(m==4){
          num1=Integer.parseInt(T3.getText());
            if(num1==result){
                T4.setText("回答正确!"); 
                sum=sum+1; 
            }
            else 
            {
                T4.setText("回答错误!"+result);
            }
            T3.setText(null);
        }
        }
         L3.setText("答对"+sum+"道!!");
    }
}


4.单元测试代码:

package Junit4;
import java.util.Random;


public class test {
    Random r=new Random();
    private int result=0,result3=0;
    private int result1=0,result2=0;
    int m;
    int a=0,b=0;
    int q=0,w=0;
    int j=0,k=0;
    public void Unit()throws Exception{
         m = (int)(Math.random()*4+1);
        a=r.nextInt()%1000; 
        b=r.nextInt()%1000;
        q=r.nextInt()%1000; 
        w=r.nextInt()%1000;
        j=r.nextInt()%1000; 
        k=r.nextInt()%1000;
        if(m==3){
            add(a,b);
        }
        else if(m==1){
            Cheng(q,w);
        }
        else if(m==2){
            Chu(j,k);
        }
        else if(m==4){
            jian(a,b);
        }
    }
    
    public int  add(int a,int b)throws Exception
    {
  
        if((b<-1000||b>1000)||(a<-1000||a>1000))
        {
            throw new Exception("a,b的值超出范围");
        }
        System.out.println(a+"+"+"("+b+")=?");
        result=a+b;
        return result;
    }
    public int  jian(int a,int b)throws Exception
    {
        if((b<-1000||b>1000)||(a<-1000||a>1000))
        {
            throw new Exception("a,b的值超出范围");
        }
        System.out.println(a+"-"+"("+b+")=?");
        result3=a-b;
        return result3;
    }
     public int Cheng(int q,int w)throws Exception
        {   
         if((q<-1000||q>1000)||(w<-1000||w>1000))
        {
            throw new Exception("a,b的值超出范围");
        }
            System.out.println(q+"*"+"("+w+")"+"=?");
            result1=q*w;
            return result1;
        }
     public int  Chu(int j,int k)throws Exception
        {
         if((j<-1000||j>1000)||(k<-1000||k>1000))
            {
                throw new Exception("a,b的值超出范围");
            }
            System.out.println(j+"/"+"("+k+")"+"=?");
            if(k==0){
                throw new Exception("除数不能为0");
            }
            result2=j/k;
            return result2;
        }
    
}
package Junit4test;

import static org.junit.Assert.*;
import junit.framework.Assert;

import org.junit.Test;
import Junit4.test;
public class TTest{

    @Test
    public void testAdd() {
        int z;
            try {
                z = new test().add(1,1);
                assertEquals(2,z);
            } catch (Exception e) {
                e.printStackTrace();
                 Assert.fail("没有抛出异常,测试失败");
            }
            System.out.println("加法运算!");
        
    }
    @Test
    public void testJian() {
        int z;
        try {
            z = new test().jian(2,1);
            assertEquals(1,z);
        } catch (Exception e) {
            e.printStackTrace();
             Assert.fail("没有抛出异常,测试失败");
        }
        System.out.println("减法运算!");
    }
    @Test
    public void testCheng() {
        double z;
        try {
            z = new test().Cheng(2,3);
            assertEquals(6,(int)z);
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail("没有抛出异常,测试失败");
        }
        System.out.println("乘法运算!");
    }

    @Test
    public void testChu(){
        float z=0;
        try{
        String r=null,r1=null;
         z=new test().Chu(3,1);
        r=String.valueOf(z);
        r1=String.valueOf(3.00);
        assertEquals(r1,r);
        }
        catch(Exception e){
            e.printStackTrace();
            Assert.fail("没有抛出异常,测试失败");
        }
        System.out.println("除法运算!");
    }

}

测试结果:

代码覆盖:

异常处理:

改变一下a,b的值

public void testAdd() {
        int z;
            try {
                z = new test().add(1001,1001);
                assertEquals(2,z);
            } catch (Exception e) {
                e.printStackTrace();
                 Assert.fail("没有抛出异常,测试失败");
            }
            System.out.println("加法运算!");
        
    }

   除数为0的时刻

public void testChu(){
        float z=0;
        try{
        String r=null,r1=null;
         z=new test().Chu(3,0);
        r=String.valueOf(z);
        r1=String.valueOf(3.00);
        assertEquals(r1,r);
        }
        catch(Exception e){
            e.printStackTrace();
            Assert.fail("没有抛出异常,测试失败");
        }
        System.out.println("除法运算!");
    }

5.负数运算:

总结:这次作业基本不知道怎么做,也是参考其他同学的博客,看别人怎么做就试着去

做,小伙伴一直鼓励和协助我完成这次作业,虽然不明确这次作业要做什么,但多少学到些东西!

原文地址:https://www.cnblogs.com/lixin20/p/4487575.html