Java Experiment 21, experimental content
1. Initial mastery of unit testing and TDD
2. Understanding and mastering the object-oriented three elements: encapsulation, inheritance, polymorphism
3. Initial mastery of UML modeling
4. Familiarity with S.O.L.I.D principles
5. Understanding Design Patterns
Second, the experimental steps
(a) Unit testing
(1) Three kinds of code
Pseudo-code: An explanation of the function or function of a program
Product Code: Code written in a specific programming language that implements certain functions
Test code: Used to detect whether a product code has a vulnerability, or whether the program meets a specific requirement
(2) TDD (test driven devlopment, testing-driven development)
Test-driven Development (TDD): Write first 测试代码 , then write, and then write 产品代码 the code that is correct
The steps are as follows:
Clear the current functionality to be completed and record it as a test list
Quick completion of writing test cases for this feature
Test code compilation does not pass
Write the Product Code
Refactor the code and ensure that the test passes
Cycle through the development of all functions
The specific percentile five-point system example is as follows:
Test Code Myutiltest
Package ag20159201;
Import Junit.framework.TestCase;
Import Org.junit.Test;
public class Myutiltest extends TestCase {
@Test
public void Testnormal () {
Assertequals ("Fail", Myutil.percentage2fivegrade (55));
Assertequals ("Pass", Myutil.percentage2fivegrade (65));
Assertequals ("Medium", Myutil.percentage2fivegrade (75));
Assertequals ("Good", Myutil.percentage2fivegrade (85));
Assertequals ("excellent", Myutil.percentage2fivegrade (95));
}
public void TestException () {
Assertequals ("Error", Myutil.percentage2fivegrade (101));
Assertequals ("Error", Myutil.percentage2fivegrade (-51));
}
public void Testboundary () {
Assertequals ("Fail", Myutil.percentage2fivegrade (0));
Assertequals ("Pass", Myutil.percentage2fivegrade (60));
Assertequals ("Medium", Myutil.percentage2fivegrade (70));
Assertequals ("Good", Myutil.percentage2fivegrade (80));
Assertequals ("excellent", Myutil.percentage2fivegrade (90));
Assertequals ("excellent", Myutil.percentage2fivegrade (100));
}
}
Product Code Myutil
Package ag20159201;
public class Myutil {
public static String percentage2fivegrade (int grad) {
if (grad<0)
return "error";
else if (grad<60)
Return "Failed";
else if (grad<70)
return "Pass";
else if (grad<80)
Return "Medium";
else if (grad<90)
return "good";
else if (grad<=100)
return "excellent";
else return "error";
}
}
Using driver-developed test results
(ii) Inheritance of package polymorphism
The three elements of object-oriented (object-oriented) include: encapsulation, inheritance, polymorphism. The result of a process abstraction is a function, and the result of a data abstraction is an abstract data type. Encapsulation actually uses methods to hide the data of the class, controlling the user's modifications to the class and the extent to which the data is accessed, resulting in the benefits of modularity (modularity) and information hiding (information hiding) Interface (interface) is an accurate description of the package. Package types include:
1. Package object Initial Flow: Use the constructor function. Constructors are methods with the same name as class names, without declaring return types;
2. Encapsulation object operation Flow: Adding methods to the class to implement specific functions; method name lowercase;
3. Encapsulate object Internal data: Define the private variable, which is the user must use the programmer to customize the operation flow. Control the user's modification of the class and the extent to which the data is accessed,
Package ag20159201;
Public abstract class Animal {
private String color;
Public String GetColor () {
return color;
}
public void SetColor (String color) {
This.color = color;
}
Public abstract String shout ();
}
Inheritance and polymorphism: the use of keyword Extends,private members in Java is also inherited, except that subclasses cannot be accessed directly and must be accessed through the methods provided by the parent class. Polymorphism is a single interface that operates on multiple types of objects.
Package ag20159201;
public class Dog extends animal{
Public String shout () {
return "Bark";
}
Public String toString () {
Return "The Dog ' s color is" + this.getcolor () + ", and it shouts" + this.shout () + "!";
}
}
Package ag20159201;
public class Cat extends animal{
Public String shout () {
Return "Meow Meow";
}
Public String toString () {
Return "The Cat ' s color is" + this.getcolor () + ", and it shouts" + this.shout () + "!";
}
}
The test code and results are as follows:
(iii) design off the implementation of the plural class complex using TDD method
Pseudo code:
First, a complex class complex is designed to define three construction methods:
① The default is real and imaginary parts are 0 when there is no parameter;
② a parameter defaults to a real number, that is, the imaginary part is 0,
③ two parameters for both real and imaginary parts
Then define two member methods to calculate the sum of two complex numbers and the difference. Defines a print () method that outputs a complex number and does not output imaginary parts when the imaginary part is 0.
Finally, define a Complextest class that uses the complex class to create two complex objects in the main method of the class, calculating the sum and the difference and outputting the two complex numbers, respectively.
Product Code:
Package ag20159201; Public classcomplex{DoubleRepart,impart; Complex () { This. repart=0; This. impart=0; } Complex (DoubleRepart) { This. repart=Repart; This. impart=0; } Complex (DoubleRepart,Doubleimpart) { This. repart=Repart; This. impart=impart;} StaticComplex Add (Complex p1,complex p2) {Complex P=NewComplex (p1.repart+p2.repart,p1.impart+P2.impart);returnp;} Complex Min (Complex p1,complex p2) {Complex P=NewComplex (p1.repart-p2.repart,p1.impart-P2.impart);returnp;} voidPrint () {System. out. println ("the value of the plural is:");if( This. impart!=0) System. out. println ("("+ This. repart+","+ This. impart+")"); ElseSystem. out. println ( This. Repart); } }
Test code and results:
|
Steps |
Take |
Percentage |
|
Demand analysis |
0.5h |
10% |
|
Design |
1h |
20% |
|
Code implementation |
1.5h |
30% |
|
Test |
1h |
20% |
|
Analysis Summary |
1h |
20%
|
Iii. Summary
Problems encountered and solutions:
1. Hint Animal code is wrong, dog and cat need to be defined in the file, so animal and dog, cat are compiled in different class files. The program still error, because three files in different packets, you can put them in the same package or in front of the code through the Import path declaration
2, the implementation of the complex complex function when you want to use the Complextest function is also used in the Assertequals () for computer automatic testing, but the program error, had to pass the function of the output of manual inspection. Hope to find a better way to learn in the future
3, the Java does not understand, programming error many, change code very laborious. To learn better by combining program and book Knowledge
Harvest:
Recognizing the features of the Java language object-oriented programming, such as encapsulation inheritance polymorphism, interface and polymorphism, these and C languages have similar places, but also have Java's own characteristics, it is worth in-depth study. But I just touch but Java, a lot of places do not know deeply, the road to go is still very long.
Java Lab report (experiment one)