20165309 Experiment 2 Java Object-Oriented Programming

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

1. Experimental content

1. Preliminary mastery of unit testing and TDD

2. Understand and master the three elements of object orientation: encapsulation, inheritance, and polymorphism

3. Preliminary grasp of UML modeling

4. Familiar with SOLID principles

5. Understand Design Patterns

2. Experimental steps

(1) Unit testing

1. Three kinds of codes: pseudo code, test code, product code

Requirement: In a MyUtil class, solve the function of converting a percentile score into a five-level score of "excellent, good, medium, pass, and fail".

  • First use natural language to write pseudocode to solve the problem from the intent level:

    百分制转五分制:
    如果成绩小于60,转成“不及格”
    如果成绩在60与70之间,转成“及格”
    如果成绩在70与80之间,转成“中等”
    如果成绩在80与90之间,转成“良好”
    如果成绩在90与100之间,转成“优秀”
    其他,转成“错误”
  • The pseudocode is then "translated" into production code in the Java language MyUtil.java.
  • Write test code MyUtilTest.javato verify production code:
    • JUnit test case:
      • normal circumstances
      • Error conditions (negative numbers, numbers over 100)
      • edge cases (0, 60, 70, 80, 90, 100)
    • Right-click on the project and select New->DirectoryCreate Directory test, right-click testthe directory, and select from the pop-up menu Mark Directory as->Test Sources Rootto set SOURCEPATHenvironment variables.
    • The screenshot of the test passing is as follows:
  • Complete product codes.
  • Test code and final production code are in the code link .

2. Test-driven development TDD (test code -> product code)

  • StringBufferDemoCreate a class in the new directory ,
    click the lightbulb icon that appears next to the class name, in the pop-up menu Create Test.
  • Test StringBufferDemoclass methods charAt(), capacity(), indexOf(), length():
    • char charAt(int index)Returns the char value at the specified index in this sequence.
    • int capacity()Returns the current capacity.
    • int indexOf(String str)Returns the index within this string of the first occurrence of the specified substring.
    • int length()Returns the length (number of characters).
  • The test results are as follows:
  • code link

(2) Expand the design pattern example to support the Long class

  • If a support Longclass is required, the Documentclass has to modify the constructor, which violates the OCP principle. Encapsulation, inheritance, and polymorphism cannot solve the problem, so design patterns are needed:
    • class Long extends Data
    • class LongFactory extends Factory
  • MyDocAdd test code to the class:

    public class MyDoc {
    static Document d;
    public static void main(String[] args) {
        d = new Document(new LongFactory());
        d.DisplayData();
        }
    }
  • The results are as follows:
  • The code is in the code cloud link .

(3) Develop a complex class Complex in the way of TDD

  • Fake code:

    Complex类要输出实部、虚部,并按照a+bi的形式输出复数。
    Complex类中有两个变量,实部A和虚部B。
    
    方法:
    getA(int a);返回实部
    getB(int b);返回虚部
    ComplexAdd(Complex c);实现复数相加
    ComplexMinus(Complex c);实现复数相减
    ComplexMulti(Complex c);实现复数相乘
    ComplexDiv(Complex c);实现复数相除
    toString(int a,int b);将复数输出成a+bi的格式。
  • Test Results:
  • The product code and test code are linked in the code cloud .

(4) Three elements of object-oriented

  • abstract
  • Encapsulation, Inheritance and Polymorphism

    Use UML to model the code in Experiment 2
    • 代码:
      ```
      public abstract class Animal {
      private String color;
      public String getColor() {
      return color;
      }
      public void setColor(String color) {
      this.color = color;
      }
      public abstract String shout();
      }
      public class Cat extends Animal {
    public String shout() {
    return "喵喵";
    }
    public String toString() {
    return "The Cat's color is " + this.getColor() +", and it shouts "+ this.shout() + "!";
    }
    }
    public class Dog extends Animal
    {
    public String shout() {
    return "汪汪";
    }
    public String toString() {
    return "The Dog's color is " + this.getColor() +", and it shouts "+ this.shout() + "!";
    }
    }
    ```
  • UML diagram (made by web page ProcessOn)
  • References

Third, the problems encountered in the experiment and their solutions

  • Question 1: The default path was selected when installing IDEA at that time, and it was not used often, so the Junit jar package was not found.
  • Solution 1: Right-click IntelliJ IDEA Ultimate on the desktop, click into properties, copy the address and paste it to "+" to find it easily~
  • Problem 2: I don't know enough about StringBuffer.
  • Solution 2: You can refer to learning www.runoob.com/java/java-stringbuffer.html
  • Solution 3: The range of the long type is -2147483648~2147483647
  • Problem 4: I don't know how to concatenate strings when designing and implementing the complex number class.
  • concat()Solution 4: There are still problems after trying the method, and suddenly realize that you can directly +....

Fourth, the experimental harvest

This experiment taught me how to write test code, and it also allowed me to standardize the habit of writing code. While following the teacher's blog tutorial and the requirements on Lan Moyun, I also realized that I am not familiar enough with some basic knowledge... I have to read books carefully.

step time consuming percentage
demand analysis 15min 12.5
design 20min 16.7
Code 40min 33.3
test 25min 20.8
analysis Summary 20min 16.7

Guess you like

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