Experiment Report of Java Experiment two experiment Contents 1. Initial mastery of unit testing and TDD2. Understand and master the object-oriented three elements: encapsulation, inheritance, polymorphism 3. Initial mastery of UML modeling 4. Familiar with the S.O.L.I.D principle 5. Understanding the design pattern of the experiment requirements 1. Students without Linux Foundation are advised to first learn the basic Linux Basics (new version), VIM Editor Course 2. Complete the experiment, write the experiment Report, the experiment report is published in the blog Garden in the blog, notice the experiment report focus is
Run Results, encountered the
problem(Tools find, install, use, program edit, Debug, run, etc.),
Solutions(empty methods such as "Check Network", "Ask classmates", "reading" and so on are 0 points) and
Analysis(What can be learned from it, what gains, lessons, etc.). The report can refer to the guidance of Fan Fei Dragon Teacher 3. Plagiarism is strictly forbidden, and the results of the experiment of the perpetrator are zero, and other punitive measures are added. 4.
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 Step (i) Unit test
When using the program to solve the problem, learn to write three kinds of code: pseudocode, Product code, test code
Example: We need to MyUtil
solve a percentile in a class. Turn into "excellent, good, medium, pass, fail" five Grade system performance function
Create a directory:
(1) Pseudo-code:
百分制转五分制: 如果成绩小于60,转成“不及格” 如果成绩在60与70之间,转成“及格” 如果成绩在70与80之间,转成“中等” 如果成绩在80与90之间,转成“良好” 如果成绩在90与100之间,转成“优秀” 其他,转成“错误”
(2) Product Code:
(3) Test code:
Write the test code first. This first-written 测试代码
, then 产品代码
-written development method is called "Test-driven Development" (TDD). The general steps for TDD 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 (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
Based on TDD, we don't have an over-design situation where the requirements are expressed through test cases, and we just 产品代码
let the test pass. There is a unit test tool JUnit in Java to aid TDD, and we use TDD to rewrite the example of the previous percentile five-point system,
Specific operations such as:
II) object-oriented three elements (1) abstract
Abstract the meaning of the word refers to people in the cognitive thinking activities in the object of the appearance of the factors of abandonment and the essence of the extraction of factors . Abstract is a kind of thinking tool that people often use when they know complex things and phenomena, abstract ability of abstract thinking is very important in program design,"refine, simplifying, youbiaojili and seeking the same in different ways" has a great extent to determine the programmer's programming ability. Abstraction is a matter of extracting the essential features of things and not considering their details for the time being. For complex system problems, we can solve problems by means of hierarchical abstraction. At the highest level of abstraction, the solution of the problem is described in a generalized way, using the language of the problem environment. In the lower layers of abstraction, they are described in a procedural way. When describing a problem solution, use a problem-oriented and implementation-oriented terminology. In program design, abstraction consists of two aspects, one is process abstraction and the other is data abstraction.
(2) encapsulation, inheritance and polymorphism
The three elements of object-oriented (object-oriented) include: encapsulation, inheritance, polymorphism. 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. Ood is an object-oriented implementation of the symbolic design system, which constructs the system into a "real-world" object in a way that is very close to the problem domain terminology, and focuses on howto implement the functional specification through the model. OOP is coded in a programming language (such as Java) on a design basis. 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, we experiment with Umbrello for modeling, and Windows recommends that you use STARUML.
The result of a process abstraction is a function, and the result of the data abstraction is an abstract data Type,adt, which can be used as an ADT with inherited and polymorphic mechanisms. Data Abstraction is the core and origin of OOP.
, encapsulation is the hiding of data and related behaviors to implement information. Classes are encapsulated in Java,
Encapsulation actually uses methods to hide the data of the class, controlling the user's modification of the class and the degree of access to the data, resulting in the benefits of modularity (modularity) and information hiding (information hiding) ; interface (interface) is an accurate description of the package. Dog
classes use classes and access control (Private,public) to hide Properties color
, open interfaces 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:
Using class diagram in UML to describe class inheritance is the foundation of software reusability, and it is the main way to improve the expansibility and maintainability of software system. As shown above, based on encapsulation, inheritance can be implemented
Code Reuse, it should be noted that the more important role of inheritance is to achieve
polymorphic。 Object-oriented objects allow different classes of objects to respond to the same message, which means that the same message can be used in a variety of different ways depending on the sending object, and we call this behavior polymorphic. In Java,
polymorphicRefers to a phenomenon in which different class objects execute different code when they invoke the same signed member method.
polymorphism is the basis of the flexibility and extensibility of object-oriented programming。 (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
- ...
- (3) design pattern Real Example
-
Java Experiment Two experiment report: Java Object-oriented programming