20165214 Experiment 2 Java Object-Oriented Programming

The cover of the lab report

Course: Java Programming Class: Class 1652 Name: Zhu Wenyuan Student ID: 20165214

Instructor: Lou Jiapeng Experiment Date: April 16, 2018

Experiment time: 13:45 - 15:25 Experiment number: 2

Experiment Name: Object-Oriented Programming

The purpose and requirements of the experiment:
1. Complete the experiment and write the experiment report. Pay attention to the experiment report focusing on the operation results, the problems encountered and the analysis.
2. Count your own PSP (Personal Software Process) time in the experimental report;
3. Master the ability to use IDEA to complete basic programming and program debugging.

2. Experimental content:

  1. Getting started with unit testing and TDD
  2. Understand and master the three elements of object-oriented: encapsulation, inheritance, polymorphism
  3. Preliminary mastery of UML modeling
  4. Become familiar with SOLID principles
  5. Learn about design patterns

3. Experimental steps

(1) Unit testing

  • 1. Create a "Util" project, create a "Util" class under the project, and enter the code:

  • 2. Create the test directory, click the class name, and pop out the light bulb. Then click Create Test to generate the MyUtilTest class in the test directory:

  • 3. Enter the following code in the MyUtilTest class to test normal conditions, boundary conditions, and error conditions:

  • 4. The running result is wrong, as shown in the figure:

  • 5. According to the analysis of the results in the screenshot, the reason is that the score cannot be lower than 0 and the score can be equal to 100. The modification procedure is:

  • 6. Run MyUtilTest again and get a "green bar"

(2) TDD

  • 1. Create a "TDDDemo" project, create a test directory "test" under the project, and then generate a test case class StringBufferTest under the "test" directory:

  • 2. To test StringBuffer's charAt(int a), indexOf(String s), capacity(), length(), write code:

  • 3. Run StringBufferTest, a green bar appears, and the test is successful:

(3) Expansion of test cases

  • 1. My student number is 14,14%6==2, so what I need to do is to make the system support the Boolean class, and add test code to the MyDoc class to show that the addition is correct.
  • 2. Analysis: According to the abstract factory pattern, I need to create two new classes Bollean (inheriting the Date class) and BolleanFactory (inheriting the Factroy class) in the program. Program and test, as shown in the figure:

(4) Practice

  • 1. Need to analyze: Use TDD to design and implement complex class Complex.
  • 2. Create a "Complex" project, create a test directory "test" under the project, and then generate a test case class ComplexTest in the "test" directory, enter the test code, and run a green bar:

  • 3. PSP time

step time consuming percentage
demand analysis 5min 7.1%
design 5min 7.1%
Code 30min 42.9%
test 20min 28.6%
analysis Summary 10min 14.3%
  • 4. Code cloud link
    which includes pseudo code, product code, test code
  • 5. The benefits of
    unit testing Unit testing I think is another great tool for programmers in addition to APIs in programming. The convenience is that you can unit test each class, checking for errors and correcting them until the class meets our needs. In this way, it can be avoided that when writing a program before, a compilation error will "trigger the whole body", and it will take us a lot of time to make mistakes.
    And using unit tests will also make our program more organized.

(5) Production of UML diagram of this experimental program

Select the most complex application of this experiment - the third part to make a UML diagram:

Difficulties and Solutions

  • Question 1: When creating unit tests, except TestCase is red, junit is also red.
  • Problem 1 solution: first turn off the software and reopen it, but there is no effect; then move the mouse to junit, and find a red light bulb, thinking of clicking a little bit to try it out, click the light bulb, and then select a Junit4 inside, the problem is solved.

  • Question 2: When testing the StringBuffer class, how is the capacity value obtained by the method capacity calculated?
  • Problem 2 Solution: The StringBuffer class has three construction methods, ①StringBuffer(), the capacity obtained at this time is the default value of 16, and it will automatically increase when it exceeds the range. ②StringBuffer(int size), the capacity obtained at this time is the size of the parameter size. ③StringBuffer(String s), the same thing obtained at this time is the length of s plus 16.
    Since the teacher gave very detailed steps in this experiment, there is no major problem.

experience

After this experiment has more content, the corresponding harvest is also quite large. First of all, I have mastered the usage of Junit proficiently. This is the biggest gain of the week, because through unit testing, I can speed up my programming and reduce the probability of my program error. Moreover, when writing unit tests, the program is inadvertently moved towards "high cohesion and low coupling". The specific reason is that I can't say anything. I think it may be because when writing unit tests, I focus more on perfecting a class. function. In addition, this week I got a very convenient tool: WhiteStarUML, and I also learned to draw UML diagrams in WhiteStarUML. This is a very convenient tool, and the drawn diagrams are simple and beautiful.

Guess you like

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