Beijing Institute of Electronic Technology (BESTI)
Real Inspection report
Course: Java Programming Class: 1352 Name: Lu Songhon No.: 20135229
Score: Instructor: Lou Jia Peng Experimental Date: 2015.5.6
Experiment level: Preview degree: Experiment time: 15:50--21:50
Instrument Group: 29 Compulsory/Elective: Elective experiment number: 02
Experiment Name: Experiment two Java object-oriented programmingExperimental 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 PatternsThe experiment requires 1.complete the experiment, write the experiment Report, the experiment report is published in the blog Garden, note that the experiment report focuses on the
running results , the
problems encountered (tool find, installation, use, program editing, debugging, running, etc.),
solutions (empty methods such as "Check the network", "Ask classmates", "reading" and so on all get 0 points) and
analysis (from which can get what revelation, what harvest, lessons, etc.).
2. Please first in the laboratory building in the ~/Code
directory with their own school number to create a directory, code and UML diagram to put in this directory, no study number will require redo, and then follow the following steps to practice .
Experimental instrument:
Name |
Model |
Number |
Pc |
Dell |
1 |
Laboratory building Environment |
/ |
1 |
Eclipse |
/ |
1 |
First, the contents and steps of the experiment
(a) Unit testing
(1) Three kinds of code
1, three kinds of code
Requirements: We want to solve a percentile grade in a Myutil class turn into "excellent, good, medium, pass, fail" five grade system performance function.
(1) Pseudo-code
百分制转五分制: 如果成绩小于60,转成“不及格” 如果成绩在60与70之间,转成“及格” 如果成绩在70与80之间,转成“中等” 如果成绩在80与90之间,转成“良好” 如果成绩在90与100之间,转成“优秀” 其他,转成“错误”
(2) Product Code
(3) test code
2.TDD (test driven devlopment, testing-driven development)
(1) Concept: Write the test code first, and then write the product Code development method called "Test-driven Development" (TDD).
(2) The general steps of TDD are as follows:
• Clear the current functionality to be completed and record it as a test list
• Quickly finish writing test cases for this feature
• Test code compilation does not pass (no product code)
• Write the Product code
• Test Pass
• Refactor the code and ensure the test passes (refactoring the next lab session)
• Cycle through the development of all functions
(3) Advantage: Based on TDD, we do not have excessive design situation, the requirements are expressed through the test case, our product code as long as the test pass on it.
(4) Change the percentile five-point system into TDD
Code hierarchy:
Create a new test case class:
(5) The coding rhythm of TDD is:
• Add test code, JUnit appears red bar
• Modify the Product code
JUnit appears green bar, mission accomplished
(ii) Object-oriented three elements
1. Abstract
(1) The ability of "refine, Youbiaojili and seeking for the same in different"
(2) Process abstraction, data abstraction
2. Encapsulation, inheritance and polymorphism
(1) Object-oriented three elements: encapsulation, inheritance, polymorphism.
(2) Object-oriented thinking involves all aspects of software development, such as object-oriented analysis (OOA), Object-oriented design (OOD), and object-oriented programming (OOP). OOA decomposes The system according to the abstract key problem domain and focuses on what. The main line that runs through OOA, Ood, and OOP is abstraction.
Ood Modeling uses the graphical Modeling language UML (Unified Modeling Language), UML is a generic modeling language.
(3) package
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.
3. Example
(1) The Dog class hides the attribute color by using class and access control (private,public), opening the interface SetColor (), GetColor (), bark (), and ToString. The Dog class is a module that we can use with the following code to test the code and run the results as follows:
(2) We can use class diagrams in UML to describe class dog:
Note: UML class diagrams show static relationships between classes, class- AnimalTest
dependent Dog
classes and Classes Cat
, and UML relies on lines with arrows.
The corresponding test code and run results are as follows:
Based on encapsulation, inheritance can implement code reuse, it should be noted that inheritance is more important to achieve polymorphism. Polymorphism is the basis of the flexibility and extensibility of object-oriented programming.
Looking at the previous class diagram, we can further abstract the bark () and meow () in the Cat class into an abstract method shout (), which overrides this method in the dog class and Cat class, as shown in the following UML diagram:
Operation Result:
(iii) design pattern preliminary (1) S.O.L.I.D principle
Object-oriented three elements are "encapsulation, inheritance, polymorphism", and any object-oriented programming language will support these three elements syntactically. It is very difficult to use the three elements, especially polymorphism, with the help of abstract thinking, and the S.O.L.I.D
principle of class design is a good guide:
- SRP (Single Responsibility Principle, sole responsibility principle)
- OCP (open-closed Principle, open-closed principle)
- LSP (Liskov substitusion Principle,liskov substitution principle)
- ISP (Interface segregation Principle, interface separation principle)
- DIP (Dependency inversion Principle, dependency inversion principle)
(2) mode and design mode
A pattern is a customary solution (solution) for a particular problem (problem) under an external environment (context). The pattern must make the problem clear, explain why it is used to solve the problem, and under what circumstances it will not work, and each pattern can be reused because of its repeatability, having its own name, being able to impart it, and transplanting it into different scenarios. Patterns can be seen as expert solutions to a problem that can be reused .
There are many patterns in computer science:
- Grasp mode
- Analysis mode
- Software Architecture Patterns
- Design Patterns: Created, structured, behavioral
- Management mode: The manager Pool implementation mode
- Interface Design Interaction Mode
- ...
The most important thing here is the design pattern , the position of design pattern in object-oriented can be equal to the position of data structure in process programming.
(3) design pattern Real Example
There are four basic elements of the design pattern:
- Pattern Name: Description mode, easy to communicate, archive
- Problem: Describes where to apply the pattern
- Solution: Describes a design's constituent elements, not for special cases
- Consequence: The results and tradeoffs of applying this pattern (trade-offs)
Design patterns are used extensively in Java class libraries:
- Factory:java.util.Calendar
- Compsite:java.awt.Container
- Decorator:java i/0
- Iterator:java.util.Enumeration
- Strategy:java.awt.LayoutManager
(iv) Exercise 1 using TDD to design a complex that implements a complex number class.
(1) Pseudo-code
Plural class Complex
plural = number of real parts + imaginary Part I
Sum of complex numbers = (real + real) + (imaginary + imaginary part) I
Plural subtraction = (real part-real) + (imaginary part-imaginary part) I
Print plural:
Imaginary part greater than 0 "real" + "+" + "imaginary" + "I"
Imaginary part less than 0 "real" + "imaginary" + "I"
Imaginary part is 0 "real part"
(2) Product Code
public class Complex {
private int shi,xu;
Complex () {
this.shi=0;
this.xu=0;
}
Complex (int shi) {
This.shi=shi;
this.xu=0;
}
Complex (int shi,int Xu) {
This.shi=shi;
This.xu=xu;
}
public void Addfu (Complex p1,complex p2) {
System.out.println ("The sum of these two complex numbers is:");
This.shi=p1.shi+p2.shi;
This.xu=p1.xu+p2.xu;
Print ();
}
public void Minusfu (Complex p1,complex p2) {
System.out.println ("The difference between these two complex numbers is:");
This.shi=p1.shi-p2.shi;
This.xu=p1.xu-p2.xu;
Print ();
}
public void Outputfu () {
System.out.println ("The value of the plural is:");
Print ();
}
public void print () {
if (this.xu>0) {
System.out.println (this.shi+ "+" +this.xu+ "I");
}else if (this.xu<0) {
System.out.println (this.shi+ "" +this.xu+ "I");
}else{
System.out.println (This.shi);
}
}
}
(3) test code
3. Report the time of your PSP (Personal software Process)
Steps |
Take |
Percentage |
Demand analysis |
1h |
18% |
Design |
1h |
25% |
Code implementation |
1.5h |
21% |
Test |
1h |
21% |
Analysis Summary |
0.5h |
15% |
4. Summarize the benefits of unit testing
(1) You can safely modify the test code without worrying about the test code that will affect the design.
(2) The test itself is a description of the code being measured, instead of a part of the code function, forcing itself to design the code to be tested more independently to complete one or several functions.
Resources
1. The essence of UML
2. The law of Construction (electronic version), author Xin Zou Blog
3. "In the design mode"
4. "Analytic Extreme Programming"
5. The way of unit testing
- "Code Encyclopedia"
- The abstract three principles of code
Tools
- Junit
- Umbrello
- StarUML
Java Course Lab report Experiment two Java object-oriented programming