java experiment 2

Experimental content

1. Preliminary mastery of unit testing and TDD

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

3. Preliminary grasp of UML modeling

4. Familiarize yourself with SOLID principles

5. Understand Design Patterns

Experimental content

1. Refer to the Intellj IDEA Simple Tutorial
and submit the screenshots that pass the last three test cases. The screenshots must be drawn and watermarked, and enter your student ID.

2. Refer to actively typing code to learn Java using JUnit,
study and learn StringBuffer in the way of TDD, submit your unit test cases and screenshots of test passing, and add the student ID watermark to the screenshots.

3. 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 supports the Byte class, and adding test code to the MyDoc class shows that the addition is correct 1

1: Let the system support the Short class, and add test code to the MyDoc class to show that the addition is correct

2: Let the system support the Boolean class, and add test code to the MyDoc class to show that the addition is correct

3: Let the system support the Long class, and add test code to the MyDoc class to show that the addition is correct

4: Let the system support the Float class, and add test code to the MyDoc class to show that the addition is correct

5: Let the system support the Double class, and add test code to the MyDoc class to show that the addition is correct

4. Develop a complex number class Complex to support addition, subtraction, multiplication and division in the way of TDD.

5. Use StarUML to model the code in Experiment 2, send a screenshot of the class diagram, and add the student number watermark. At least two classes in a class diagram.

Experimental procedure

(1) Unit testing

(1) Three codes
• Product code
public class MyUtil{
public static String percentage2fivegrade(int grade){
//If the grade is less than 60, it will be converted to "Fail"
if (grade < 60)
return "Fail";
//If If the grade is between 60 and 70, switch to "Pass"
else if (grade < 70)
return "Pass";
//If the grade is between 70 and 80, switch to "Fair"
else if (grade < 80)
return " Moderate";
//if the grade is between 80 and 90, convert to "good"
else if (grade < 90)
return "good";
//if the grade is between 90 and 100, convert to "excellent"
else if ( grade < 100)
return "Excellent";
//Other, turn into "Error"
else
return "Error";
}
}
•Test code
In order to check the robustness of the code and avoid bugs, it is necessary to test the code. Use test code.
write test code

• Test
the code in general, the results are as follows

•Boundary test
The code that tests the boundary conditions of the input "0, 60, 70, 80, 90, 100", and the following results are obtained

There is a problem, fix this problem, get code 2, test again, and get the following results

(2) TDD (Test Driven Devlopment, Test Driven Development)

The general steps of TDD are as follows:
• Identify the current function to be completed and record it as a test list

• Quickly complete writing test cases for this feature

• Test code compilation fails (no production code yet)

Experimental procedure

1. Learn to use TDD under IDEA and install JunitGenerator V2.0

To use TDD in IDEA, you can refer to the teacher's Intellj IDEA simple tutorial, follow the teacher's steps step by step, only when there is a problem when installing JunitGenerator V2.0, the tutorial says to select Plugins in the settings dialog box, click Install JetBrains The plugin... button opens the plug-in installation interface, then enter junit in the search of the plug-in installation dialog box, click JunitGenerator V2.0, click the green Install1 button on the right to install, but when I click the Install JetBrains plugin... button JunitGenerator V2.0 cannot be found when opening the plugin installation interface, only the following plugin

After many explorations, I found that the following steps can be used to find the content that needs to be installed: Select Plugins in the settings dialog, and then click the Browse repositories... button (as shown below) to open the plugin installation interface, and then the tutorial is the same, in the plugin installation dialog Enter junit in the search box, click JunitGenerator V2.0, and click the green Install1 button on the right to install.

using TDD

The coding rhythm of TDD is:
• Add test code, JUnit appears red bar

• Modify product codes

• A green bar appears in JUnit, and the task is completed. Completed
tasks 1, 2 and 4 of this experiment through TDD. Here is a link to the code learned through TDD and the screenshot of the test passed.
The product code of the first experimental topic The test code and the result screenshot

The product code, test code and screenshot of the result of the second experiment topic

The product code, test code, and screenshot of the running result of the fourth experimental topic

Experiment 5 I didn't make it myself. I tried a lot for that picture but it didn't work.

Guess you like

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