20165213Java第二次实验

实验二Java面向对象程序设计

实验1

实验目的与要求:

参考http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习
提交最后三个JUnit测试用例(正常情况,错误情况,边界情况)都通过的截图,截图上要有画图加水印,输入自己的学号
测试用例至少要包含正常情况,错误情况,边界情况的测试
实验内容及结果截图:

伪代码(可以用中文,最好用英文~)
百分制转五分制:
如果成绩小于60,转成“不及格”
如果成绩在60与70之间,转成“及格”
如果成绩在70与80之间,转成“中等”
如果成绩在80与90之间,转成“良好”
如果成绩在90与100之间,转成“优秀”
其他,转成“错误”
产品代码(将伪代码直接用Java翻译过来)

public class MyUtil{
   public static String percentage2fivegrade(int grade){
       //如果成绩小于0,转成“错误”
       if ((grade < 0))
           return "错误";
       //如果成绩小于60,转成“不及格”
       else if (grade < 60)
           return "不及格";
       //如果成绩在60与70之间,转成“及格”
       else if (grade < 70)
           return "及格";
       //如果成绩在70与80之间,转成“中等”
       else if (grade < 80)
           return "中等";
       //如果成绩在80与90之间,转成“良好”
       else if (grade < 90)
           return "良好";
       //如果成绩在90与100之间,转成“优秀”
       else if (grade <= 100)
           return "优秀";
       //如果成绩大于100,转成“错误”
       else
           return "错误";
   }
}

单元测试用例

import org.junit.Test;
import junit.framework.TestCase;

public class MyUtilTest extends TestCase {
    @Test
    public void testNormal() {
        assertEquals("不及格", MyUtil.percentage2fivegrade(55));
        assertEquals("及格", MyUtil.percentage2fivegrade(65));
        assertEquals("中等", MyUtil.percentage2fivegrade(75));
        assertEquals("良好", MyUtil.percentage2fivegrade(85));
        assertEquals("优秀", MyUtil.percentage2fivegrade(95));
    }

    @Test
    public void testException() {
        assertEquals("错误", MyUtil.percentage2fivegrade(105));
        assertEquals("错误", MyUtil.percentage2fivegrade(-55));
    }

    @Test
    public void testBoundary() {
        assertEquals("不及格", MyUtil.percentage2fivegrade(0));
        assertEquals("及格", MyUtil.percentage2fivegrade(60));
        assertEquals("中等", MyUtil.percentage2fivegrade(70));
        assertEquals("良好", MyUtil.percentage2fivegrade(80));
        assertEquals("优秀", MyUtil.percentage2fivegrade(90));
        assertEquals("错误", MyUtil.percentage2fivegrade(100));
    }

实验二

实验目的与要求:

参考 积极主动敲代码,使用JUnit学习Java

以 TDD的方式研究学习StringBuffer

提交你的单元测试用例和测试通过的截图,截图要加上学号水印

测试自己会不会写JUnit测试用例

实验三

实验目的与要求:

参考实验二 Java面向对象程序设计
对设计模式示例进行扩充,体会OCP原则和DIP原则的应用,初步理解设计模式
用自己的学号%6进行取余运算,根据结果进行代码扩充:

0:让系统支持Byte类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
1:让系统支持Short类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
2:让系统支持Boolean类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
3:让系统支持Long类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
4:让系统支持Float类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
5:让系统支持Double类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印
实验内容及结果截图:

改过的代码如下:

abstract class Data {
    abstract public void DisplayValue();
}
class Integer extends  Data {
    int value;
    Integer() {
        value=100;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Short extends  Data {
    int value;
    Short() {
        value=1;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
// Pattern Classes
abstract class Factory {
    abstract public Data CreateDataObject();
}
class IntFactory extends Factory {
    public Data CreateDataObject(){
        return new Integer();
    }
}
class ShortFactory extends Factory {
    public Data CreateDataObject(){
        return new Short();
    }
}
//Client classes
class Document {
    Data pd;
    Document(Factory pf){
        pd = pf.CreateDataObject();
    }
    public void DisplayData(){
        pd.DisplayValue();
    }
}
//Test class
public class MyDoc {
    static Document d;
    public static void main(String[] args) {
        d = new Document(new ShortFactory());
        d.DisplayData();
    }
}

实验四

实验目的与要求:

提交:单元测试代码和运行成功截图及码云上代码链接,截图要加上学号水印
任务:以TDD的方式开发一个复数类Complex,要求如下:
// 定义属性并生成getter,setter
double RealPart;
double ImagePart;
// 定义构造函数
public Complex()
public Complex(double R,double I)

//Override Object
public boolean equals(Object obj)
public String toString()

// 定义公有方法:加减乘除
Complex ComplexAdd(Complex a)
Complex ComplexSub(Complex a)
Complex ComplexMulti(Complex a)
Complex ComplexDiv(Complex a)

产品代码:

import java.lang.Integer;
import java.util.Objects;

public class Complex {
    //定义属性并生成getter,setter
    double RealPart;
    double ImagePart;
    public double getRealPart(){
        return RealPart;
    }
    public double getImagePart(){
        return ImagePart;
    }

    //定义构造函数
    public Complex(){
        RealPart = 0;
        ImagePart = 1;
    }
    public Complex(double R,double I){
        RealPart = R;
        ImagePart = I;
    }

    //Override Object
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }
        if(!(obj instanceof Complex)) {
            return false;
        }
        Complex complex = (Complex) obj;
        if(complex.RealPart != ((Complex) obj).RealPart) {
            return false;
        }
        if(complex.ImagePart != ((Complex) obj).ImagePart) {
            return false;
        }
        return true;
    }
    public String toString(){
        String s = new String();
        if (ImagePart > 0){
            s = getRealPart() + "+" + getImagePart() + "i";
        }
        if (ImagePart == 0){
            s = getRealPart() + "";
        }
        if(ImagePart < 0){
            s = getRealPart() + "" + getImagePart() + "i";
        }
        if(RealPart == 0){
            s = getImagePart() + "i";
        }
        return s;
    }
    //定义公有方法:加减乘除
    Complex ComplexAdd(Complex a){
        return new Complex(RealPart + a.RealPart,ImagePart + a.ImagePart);
    }
    Complex ComplexSub(Complex a){
        return new Complex(RealPart - a.RealPart,ImagePart - a.ImagePart);
    }
    Complex ComplexMulti(Complex a){
        return new Complex(RealPart*a.RealPart-ImagePart*a.ImagePart,RealPart*a.ImagePart + ImagePart*a.RealPart);
    }
    Complex ComplexDiv(Complex a) {
        return new Complex((RealPart * a.ImagePart + ImagePart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart), (ImagePart * a.ImagePart + RealPart * a.RealPart) / (a.RealPart * a.RealPart + a.RealPart * a.RealPart));
    }
}

测试代码:

import static org.junit.Assert.*;
import org.junit.Test;
import junit.framework.TestCase;
public class ComplexTest extends TestCase {
    Complex complex = new Complex(1,1);
    @Test
    public void testAdd(){
        assertEquals(new Complex(3.3,3.4), complex.ComplexAdd(new Complex(2.3,2.4)));
    }
    //测试加法
    @Test
    public void testSub(){
        assertEquals(new Complex(-5.3,-2.4), complex.ComplexSub(new Complex(6.3,3.4)));
    }
    //测试减法
    @Test
    public void testMulti(){
        assertEquals(new Complex(3.0,2.0), complex.ComplexMulti(new Complex(3.0,2.0)));
    }
    //测试乘法
    @Test
    public void testDiv(){
        assertEquals(new Complex(1.0,1.0), complex.ComplexDiv(new Complex(1.0,1.0)));
        assertEquals(new Complex(0.0,0.0), complex.ComplexDiv(new Complex(1.0,0.0)));
        //assertEquals(new Complex(0.0,0.0), complex.ComplexDiv(new Complex(3,4)));
        //边缘测试
    }
    @Test
    public void testequals(){
        assertEquals(true, complex.equals(new Complex(1.0,1.0)));
    }
    //测试判断相等
}

实验五

实验感想:

通过这次实验我成功掌握了TDD,已经UML图的做法,丰富了我的知识库,很感谢娄老师提供的这次有意义的实验!

posted on 2018-04-22 23:18  zqh根本吃不饱  阅读(185)  评论(1编辑  收藏  举报

导航