2017-2018-2 20165221 Experiment 2 "Java Object-Oriented Programming" experimental report

JAVA experiment two report

Course : Java Programming
Name : Tan Xiao
Student ID : 20165221 Experiment time : 2018.4.13--2018.4.15

Experiment 2--1

Experimental content

Realize the function of converting 100-point system results into "excellent, good, medium, pass, fail" five-level system

Submit a request

Submit the screenshots that pass the last three test cases. The screenshots must be painted and watermarked, and enter your student ID.

Experiment specific operation
  • First generate pseudo code by thinking, and then choose to describe it in Java language, that is, product code is generated

    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 "错误";
    }
    }
  • Write test code Test the code
    from three angles : normal value, error value, boundary value
    ( )
    When testing the error of the boundary value, a bug was found, that is, 100 was not included when judging excellent.

    Then, I modified the product code and made the following

    changes

However, if the above picture does appear test failed, if you want to solve it, you need to create a new test. For details, see the following

. Rewrite the test code

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

Running, running at this time, will show the gap between your assertion and the actual.

  • This part of the summary is mainly about boundary values ​​and whether the problem is comprehensive or not. In the above example, there are multiple angles to test, and my ability is limited. I just selected several thinking angles provided by the teacher and tested and verified it. The focus is still Master the overall idea, understand the pseudo code to the product code and then to the test code, and then return to the process of modifying the product code after the problems found by the test code.

Experiment 2--2

Experimental content

Study and learn StringBuffer in the way of TDD

Experimental operation

StringBuffer( ): Allocate a buffer of 16 characters

length(): returns the length of the string

harAt(int i) : Returns the char value at the specified index in this sequence. The first char value is at index 0, the second at index 1, and so on

indexOf(String s): Returns the position of the first letter of the input substring in the parent string

capacity(): Returns the storage capacity allocated by string

  • Then I set up several strings of different lengths for testing.
    I set the strings according to my student ID 5221xuexijava. When increasing the length of the code, I chose to repeat the above strings. The test code is as follows

experimental code

import org.junit.Test;
import junit.framework.TestCase;
public class StringBufferDemoTest extends TestCase {
    StringBuffer a = new StringBuffer("5221xuexijava");
    StringBuffer b = new StringBuffer("5221xuexijava5221xuexijava");
    StringBuffer c = new StringBuffer("5221xuexijava5221xuexijava5221xuexijava");

    @Test
    public void testcharAt() throws Exception {
        assertEquals('u', a.charAt(5));
        assertEquals('2', c.charAt(1));
        assertEquals('j', c.charAt(9));
    }

    @Test
    public void testcapacity() throws Exception {
        assertEquals(29, a.capacity());
        assertEquals(55, c.capacity());
        assertEquals(55, c.capacity());

    }

    @Test
    public void testlength() throws Exception {
        assertEquals(13, a.length());
        assertEquals(39, c.length());
        assertEquals(39, c.length());
    }
}
  • The results of its operation are as follows:

Experiment 2-3

Experimental content

Expand the design pattern example, experience the application of the OCP principle and the DIP principle, and initially understand the design pattern
to use its own student number %6 to perform the remainder operation :

  • 0: Let the system support the Byte 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

  • Let the system support the Short class, and add the test code to 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
  • Let the system support the Boolean class, and add the test code to 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
  • 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 the screenshot of the running result, and add the student number watermark
  • Let the system support the Float class, and add the test code to 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
  • 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

Experimental operation

I choose the first one: the support byteclass, and the experimental code is as follows:

import java.util.Objects;
abstract class Data {
    abstract public void DisplayValue();
}
class Integer extends Data {
    int value;
    Integer() {
        value=100;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Long extends Data {
    long value;
    Long() {
        value=(long)20165221;
    }
    public void DisplayValue(){
        System.out.println (value);
    }
}
class Byte extends Data {//Byte继承Data类
    byte value;
    Byte() {
        value=(byte)20165221;
    }
    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 LongFactory extends Factory {
    public Data CreateDataObject(){
        return new Long();
    }
}
class ByteFactory extends Factory {//ByteFactory继承工厂类
    public Data CreateDataObject(){
        return new Byte();
    }
}
//Client classes
class Document {
    Data data;
    Document(Factory factory){
        data = factory.CreateDataObject();
    }
    public void DisplayData(){
        data.DisplayValue();

    }
}
public class StringBufferDemoTest {
    static Document d;
    static Document c;
    public static void main(String[] args) {
        d = new Document(new ByteFactory());
        d.DisplayData();
        c = new Document(new LongFactory());
        c.DisplayData();
    }
}

The test code is as follows:


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(2.0,1.6), complex.ComplexAdd(new Complex(5.2,2.1)));
    }
    //测试加法
    @Test
    public void testSub()
        assertEquals(new Complex(-2.0,-1.6), complex.ComplexSub(new Complex(5.2,3.0)));
    }
    //测试减法
    @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)));
    }
    //测试判断相等
}

The results are as follows:

Experiment 2-4

Experimental content

  • Reference blog

  • Task: Develop a complex class Complex in a TDD manner, the requirements are as follows:

    experiment procedure
  • Or follow the idea of ​​​​the first pseudo-code


// 定义属性并生成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)

(1)属性:复数包含实部和虚部两个部分,
      double RealPart;复数的实部
      double ImagePart;复数的虚部
      getRealPart():返回复数的实部
      getImagePart();返回复数的虚部
      setRealPart():设置复数的实部
      setImagePart();设置复数的虚部
      输出形式:a+bi
      
(2)方法:
       ①定义构造函数
       public Complex()
       public Complex(double R,double I)
       ②定义公有方法:加减乘除
       Complex ComplexAdd(Complex a):实现复数加法
       Complex ComplexSub(Complex a):实现复数减法
       Complex ComplexMulti(Complex a):实现复数乘法
       Complex ComplexDiv(Complex a):实现复数除法
       Override Object
       public String toString():将计算结果转化为字符串形式并输出
       
  • Give the experimental code
public class Complex {
    // 定义属性并生成getter,setter
    private double RealPart;
    private double ImagePart;
    // 定义构造函数
    public Complex(){

    }
    public Complex(double R, double I){
        this.RealPart = R;
        this.ImagePart = I;
    }

    public double getRealPart() {
        return RealPart;
    }

    public void setRealPart(double realPart) {
        RealPart = realPart;
    }

    public double getImagePart() {
        return ImagePart;
    }

    public void setImagePart(double imagePart) {
        ImagePart = imagePart;
    }

    //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 string = "";
        if (ImagePart > 0)
            string =  RealPart + "+" + ImagePart + "i";
        if (ImagePart == 0)
            string =  RealPart + "";
        if (ImagePart < 0)
            string = RealPart + " " + ImagePart + "i";
        return string;
    }

    // 定义公有方法:加减乘除
    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,ImagePart*a.RealPart+RealPart*a.ImagePart);
    }
    Complex  ComplexDiv(Complex a) {
        Complex d = new Complex();
        d.RealPart = (this.RealPart * a.RealPart + this.ImagePart * a.ImagePart)/(a.RealPart*a.RealPart+a.ImagePart*a.ImagePart);
        d.ImagePart = (this.ImagePart * a.RealPart - this.RealPart * a.ImagePart)/(a.RealPart*a.RealPart+a.ImagePart*a.ImagePart);
        return d;
    }

}
  • Give the test code again

    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(2.0,1.6), complex.ComplexAdd(new Complex(5.2,3.0)));
    }
    //测试加法
    @Test
    public void testSub(){
        assertEquals(new Complex(-2.0,-1.6), complex.ComplexSub(new Complex(5.2,3.0)));
    }
    //测试减法
    @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)));
    }
    //测试判断相等
    }

    Use StarUML to model the code in Experiment 2

problem solving

  • When I was looking for the location of until and configuring it, I couldn't find it myself. Finally, I went back to read the blog, downloaded everything, searched, and
    finally found the adaptation successfully!
  • When setting the strings in experiment 2-2, the strings in the examples I saw were all English letters, and I replaced them with English letters plus my student ID , just to pay attention to the length of the characters.
  • When using markdown to edit online, I can compile it successfully on Youdao Cloud , but when I paste it on the blog garden, the following code format error occurs.

word description


That is, the text part in the middle is coded, and it is solved by adjusting the typesetting later.

Summarize

This test is still very serious, and it took a lot of time. The test code is compiled and run according to the example code, and then changed to a format containing your student number information as required! The harvest this time should be not small, because I repaired the computer once last week and reinstalled everything. I also started from the jdk configuration, and learned the idea and the whole experiment again.
But there are still many shortcomings:

  • For example, the procedure for judging the results of Experiment 2--2 actually has many aspects of testing, except for the boundary value, critical value, output error, etc., I didn't think of a new angle, but just conducted the test according to the direction given by the teacher.
  • I am not very familiar with the operation of the idea. After reinstalling the idea, when I operate it myself, I find many steps, such as creating a new test, and I have not remembered the method of creating a new test on the experimental code. Practice makes perfect.
    Chief Assistant, this test is much more cumbersome than the first time, I spent more time to complete this test than the first time, and the gains are still there!

Guess you like

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