20165213 Java second experiment

Experiment 2 Java Object-Oriented Programming

Experiment 1

Experiment purpose and requirements:

Refer to http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST to complete the unit test learning and
submit the screenshots of the last three JUnit test cases (normal conditions, error conditions, boundary conditions) passing, and the screenshots should be If there are drawings and watermarks, enter your student ID. The
test cases should include at least tests for normal conditions, error conditions, and boundary conditions.
Screenshots of experimental content and results:

Pseudo code (can be in Chinese, preferably in English~)
Convert from 100 to five-point system:
If the score is less than 60, it will be converted to "Fail"
If the score is between 60 and 70, it will be converted to "Pass"
If the score is between 70 and 80
If the grade is between 80 and 90, convert to "Fair" if
the grade is between 90 and 100, convert to "Excellent"
Other, convert to "Wrong"
product code (the pseudocode directly translated in 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 "错误";
   }
}

unit test case

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));
    }

Experiment 2

Experiment purpose and requirements:

Refer to actively typing code, use JUnit to learn Java

, study and learn StringBuffer in the way of TDD,

submit your unit test cases and screenshots of test passing, and add the student number watermark to the screenshots.

Test whether you can write JUnit test cases yourself

Experiment 3

Experiment purpose and requirements:

Refer to Experiment 2 Java Object-Oriented Programming
Expand the design pattern example, experience the application of the OCP principle and the DIP principle, and initially understand the design pattern
Use your own student number %6 to perform the remainder operation, and expand the code according to the result:

0: Let the system support the Byte class, and add the test code to the MyDoc class to indicate that the addition is correct, submit a screenshot of the test code and the running result, and add the student number watermark
1: Let the system support the Short class, and add the test code to the MyDoc class to indicate that Add it correctly, submit the test code and the screenshot of the running result, add the student number watermark
2: Let the system support the Boolean class, and add the test code in the MyDoc class to indicate that the addition is correct, submit the test code and the screenshot of the running result, and add the student number watermark
3 : Let the system support the Long class, and add the test code in the MyDoc class to indicate that the addition is correct, submit the test code and screenshots of the running result, and add the student number watermark
4: Let the system support the Float class, and add the test code in the MyDoc class to indicate the addition Correct, submit the test code and the screenshot of the running result, add the student number watermark
5: Let the system support the Double class, and add the test code in the MyDoc class to indicate that the addition is correct, submit the test code and the screenshot of the running result, and add the student number watermark
experiment content and screenshot of the result:

The modified code is as follows:

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();
    }
}

Experiment 4

Experiment purpose and requirements:

Submit: unit test code and screenshots of successful running and code links on the code cloud. The screenshots should be watermarked with the student number.
Task: Develop a complex class Complex in the way of TDD. The requirements are as follows:
// Define properties and generate getter, setter
double RealPart;
double ImagePart;
// Define the constructor
public Complex()
public Complex(double R,double I)

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

// Define public methods: addition, subtraction, multiplication and division
Complex ComplexAdd(Complex a)
Complex ComplexSub(Complex a)
Complex ComplexMulti(Complex a)
Complex ComplexDiv(Complex a)

Product Code:

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));
    }
}

Test code:

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)));
    }
    //测试判断相等
}

Experiment 5

Experiment thoughts:

Through this experiment, I have successfully mastered TDD, and I have used UML diagrams to enrich my knowledge base. I am very grateful to Mr. Lou for providing this meaningful experiment!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324767176&siteId=291194637