20165333 Experiment 2 Java Object-Oriented Programming

  • Name: Chen Guochao
  • Student ID: 20165333
  • Class: 1653
  • Experimental course: JAVA programming
  • Experiment Name: Java Object-Oriented Programming
  • Experiment time: 2018.4.14
  • Instructor: Lou Jiapeng

    Experiment content and steps

    (one)
    "Test Driven Development" (TDD). The general steps of TDD are as follows:
  • Identify the current functions to be completed and record them as a test list

  • Quickly complete writing test cases for this feature
  • The test code compilation fails (there is no product code)
  • Write product code
  • Test passed
  • Refactor the code and make sure the tests pass (refactoring the next lab exercise)
  • The development of all functions is completed in a loop.
    Screenshot of the result

(two)

Three elements: encapsulation, inheritance, polymorphism

  • Encapsulation is the packaging of data with related behaviors so that information is hidden.
  • Inheritance means that the definition of a class can be based on another existing class, that is, the subclass is based on the parent class, so that the code of the parent class can be reused.
  • Polymorphism refers to the phenomenon that different class objects will execute different code when calling a member method of the same signature.

  • Product Code

public class StringBufferDemo{
    StringBuffer buffer = new StringBuffer();
    public StringBufferDemo(StringBuffer buffer){
        this.buffer = buffer;
    }
    public Character charAt(int i){
        return buffer.charAt(i);
    }
    public int capacity(){
        return buffer.capacity();
    }
    public int length(){
        return buffer.length();
    }
    public int indexOf(String buf) {
        return buffer.indexOf(buf);
    }

}
  • test code
import junit.framework.TestCase;
import org.junit.Test;

import static org.junit.Assert.*;

public class StringBufferDemoTest extends TestCase {

    StringBuffer string1 = new StringBuffer("Beautiful");
    StringBuffer string2 = new StringBuffer("Beautiful Girls");
    StringBuffer string3 = new StringBuffer("Beautiful Girls and Boys");
    @Test
    public void testCharAt(){
        assertEquals('a',string1.charAt(2));
        assertEquals(' ',string2.charAt(9));
        assertEquals('a',string3.charAt(16));
    }
    @Test
    public void testCapacity(){
        assertEquals(25,string1.capacity());
        assertEquals(31,string2.capacity());
        assertEquals(40,string3.capacity());
    }
    @Test
    public void testindexOf() {

        assertEquals(1, string3.indexOf("ea"));
    }
    @Test
    public void testlength() {

        assertEquals(9, string1.length());
    }
}

Results screenshot

(three)
  • SRP (Single Responsibility Principle, single responsibility principle)
  • OCP (Open-Closed Principle, Open-Closed Principle)
  • LSP (Liskov Substitution Principle, Liskov Substitution Principle)
  • ISP (Interface Segregation Principle)
  • DIP (Dependency Inversion Principle, Dependency Inversion Principle)
  • The code given by the teacher is to realize the return of the int class. The title requires the return of the long class. If the code is directly deleted or modified, it will violate the OCP principle. It is open to addition and closed to deletion. So let's use design patterns. - Use interface-oriented programming, using abstraction and inheritance. The following are the experimental tasks:
  • Use your own student number (20165333)%6 to perform the remainder operation, and expand the code according to the result:
    0: Let the system support the Byte class, and add a test code to the MyDoc class to show that the addition is correct.
  • code

    abstract class Data{
    public abstract void DisplayValue();
    }
    class Integer extends Data {
    int value;
    Integer(){
        value=100; }
    public void DisplayValue(){
        System.out.println(value);
    }
    }
    class PaseByte extends Data {
    Byte value;
    PaseByte(){
        value=-33;
    }
    public void DisplayValue(){
        System.out.println(value);
    }
    }
    class Document {
    Data pd;
    Document() {
        pd=new PaseByte() ;
    }
    public void DisplayData(){
        pd.DisplayValue();
    }
    }
    public class MyDoc {
    static Document d;
    public static void main(String[] args) {
        d = new Document();
        d.DisplayData();
    }
    }

Results screenshot

(Four)
  • Use TDD to design and implement complex class Complex
    // Define properties and generate getters, setters
    double RealPart;
    double ImagePart;
    // Define 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

    public class Complex {
    // 定义属性并生成getter,setter
    double RealPart;
    double ImagePart;
    // 定义构造函数
    public Complex(){
        RealPart = 0;
        ImagePart = 1;
    }
    public Complex(double R,double I){
        ImagePart = I;
        RealPart = R;
    }
    
    //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);
    }
    Complex ComplexDiv(Complex a) {
        if(a.RealPart==0||a.ImagePart==0) {
            System.out.println("被减数不能为0");
            return new Complex();
        }
    
        double d = Math.sqrt(a.RealPart*a.RealPart)+Math.sqrt(a.ImagePart*a.ImagePart);
        return new Complex((RealPart*a.RealPart+ImagePart*a.ImagePart)/d,Math.round((RealPart*a.ImagePart-ImagePart*a.RealPart)/d));
    }
    }
  • 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)));
    } //测试判断相等
    }

    Results screenshot

(Fives)
  • Use WhiteStarUML to model the code in Experiment 2, send a screenshot of the class diagram, and add the student number watermark. Refer to http://www.cnblogs.com/rocedu/p/6736847.html
    There are only two classes in the class diagram.
  • screenshot
Problems encountered during the experiment

There was a little problem in the process of installing juit. When I looked for the juit.jar file according to the tutorial, I didn't find it. Finally, I found that the search path was wrong. I will pay attention to it later.

Guess you like

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