20145326 Java Programming Experiment two Java object-oriented Programming experiment report experiment content
- Initial mastery of unit testing and TDD
- Understanding and mastering object-oriented three elements: encapsulation, inheritance, polymorphism
- Initial mastery of UML modeling
- Familiarity with S.O.L.I.D principles
- Understanding Design Patterns
Experimental steps
One, Unit test
1. Three kinds of code
• Pseudo Code
• Product Code
• Test Code
Example:
Requirements: We want to solve a percentile grade in a Myutil class turn into "excellent, good, medium, pass, fail" five grade system performance function.
Pseudo code:
百分制转五分制: 如果成绩小于60,转成“不及格” 如果成绩在60与70之间,转成“及格” 如果成绩在70与80之间,转成“中等” 如果成绩在80与90之间,转成“良好” 如果成绩在90与100之间,转成“优秀” 其他,转成“错误”
Product Code:
Test Case:
Find errors and modify them ~
Perform TDD testing ~
Set up the Myutil class and test:
Second, TDD (test driven devlopment, testing-driven development)
Write the test code first, then write the product Code development method 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
• 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
Third, object-oriented three elements
(1) Abstract
"Refine, Youbiaojili, and different in seeking the same". 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. 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.
The first element of the OO three elements is encapsulation, which is the encapsulation of data and related behaviors to be wrapped together to implement information hidden. Classes are encapsulated in Java.
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.
Iv. use of UML
PublicAbstractclassAnimal {PrivateString color; publicString GetColor () {return color;} public void SetColor (String color) { this.color = color;} PublicAbstractString shout ();} PublicclassDogextendsanimal{ PublicString Shout () {return"Bark"; PublicString toString () {return"The Dog ' s color is" +This.getcolor () +", and it shouts" +this.shout () +"!"; }} PublicclassCatextendsanimal{public string shout () { return "Meow";} public string toString () { return this.getcolor () + ", and it shouts" + this.shout () + "!";}}
V. Design pattern and S.O.L.I.D principle
(1) S.O.L.I.D principle
· 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
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 in this area is design patterns.
(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)
Vi. Practice Assignments
Requirements: Use TDD to design the complex to implement the plural class.
1. Pseudo-code
请用户输入第一个复数的实部与虚部:请用户输入第二个复数的实部与虚部:这两个复数的和为:这两个复数的差为:这两个复数的乘为:这两个复数的除为:
2. Product Code
PublicClass myutil{public staticcomplex{//Two complex numbers addedDouble real2 = A.getreal ();Double image2 = A.getimage ();Double newreal =Real + real2;Double NewImage = image + Image2;complex result = new complex (newreal,newimage); return result; Two plural subtraction double Real2 = A.getreal (); double image2 = A.getimage (); double newreal = REAL-REAL2; double newimage = image-image2; complex result = new complex (newreal,newimage); return result; Two plural multiply double Real2 = A.getreal (); double image2 = A.getimage (); double newreal = REAL*REAL2-IMAGE*IMAGE2; double newimage = image*real2 + REAL*IMAGE2; Complex result = newComplex (newreal,newimage);return result;//Two division of complex numbers Double real2 = A.getreal (); Double image2 = A.getimage (); Double newreal = (real*real2 + image*image2)/(Real2*real2 + image2*image2); Double newimage = (Image*real2- real*image2)/(Real2*real2 + image2*image2); Complex result = new Complex (newreal,newimage); return result; }}
3. Test code
PublicClass MainClass {//For testing plural classespublic static void Main (string[] args) {System.out.println ( "Please enter the real and imaginary parts of the first complex number:"); complex data1 = new complex (); System. out.println ( "Ask the user to enter the real and imaginary parts of the second complex number:"); complex data2 = new complex ();//The following are subtraction complex result_add = Data1.add (data2); complex result_sub = Data1.sub (data2); complex Result_mul = Data1.mul (data2); complex result_div = Data1.div (data2); Result_add. print (); result_sub. print (); Result_mul. print () result_div.print ();}}
Program source Code
Import Java.util.Scanner;PublicClassComplex {//plural classDoubleReal Real DepartmentDouble image; Imaginary partComplex () {///without parameters Scanner input = new Scanner (System.in);DoubleReal = input.nextdouble ();Double image = Input.nextdouble ();Complex (Real,image); }private voidComplex (DoubleRealDouble image) {//For constructor calls without parameters//TODO auto-generated method stub this.Real =Real This.image = image; }Complex (DoubleRealDouble image) {//constructor method with parameter this.Real =Real This.image = image; }PublicDouble Getreal () {ReturnReal }public void Setreal (DoubleReal) {this.Real =Real }PublicDouble GetImage () {return image; }public void SetImage (Double image) {this.image = image;}Complex Add (Complex a) {//plural addDouble real2 = A.getreal ();Double image2 = A.getimage ();Double newreal =Real + real2;Double NewImage = image + Image2;Complex result = newComplex (Newreal,newimage);return result; }Complex Sub (Complex a) {//plural subtractionDouble real2 = A.getreal ();Double image2 = A.getimage ();Double newreal =Real-real2;Double newimage = image-image2;Complex result = newComplex (Newreal,newimage);return result; }Complex Mul (Complex a) {//plural multiplicationDouble real2 = A.getreal ();Double image2 = A.getimage ();Double newreal =Real*real2-image*image2;Double newimage = Image*real2 +Real*image2;Complex result = newComplex (Newreal,newimage);return result; }Complex Div (Complex a) {//plural divideDouble real2 = A.getreal ();Double image2 = A.getimage ();Double newreal = (Real*real2 + image*image2)/(Real2*real2 + image2*image2);Double newimage = (Image*real2-Real*image2)/(Real2*real2 + image2*image2);Complex result = newComplex (Newreal,newimage);return result; }public voidPrint () {//outputif (Image >0) {System.Out.println (Real +"+" + image +"I"); }Elseif (Image <0) {System.Out.println (Real +"" + image +"I"); }else{System.Out.println (Real); } }}PublicClass MainClass {//For testing plural classespublic static void Main (string[] args) {//TODO auto-generated method stub System.out.println ("Ask the user to enter the real and imaginary parts of the first complex number:"); Complex data1 = new Complex (); System. out.println ("Ask the user to enter the real and imaginary parts of the second complex number:"); Complex data2 = new Complex ();//the following respectively subtraction Complex result_add = Data1.add (data2); Complex result_sub = data1.sub (data2); Complex Result_mul = Data1.mul (data2); Complex result_div = Data1.div (data2); Result_add. print (); result_sub. print (); Result_mul. print (); Result_div.print ();}}
UML Model:
VII. Benefits of Unit Testing
For the new contact unit test, I think that writing unit tests can be found in the code in time to find problems, reduce the energy and cost of late maintenance, debugging code is a good way! You can change the side of the writing, the project is continuously optimized! It helps to achieve the module's "intra-block high cohesion, low coupling between blocks".
Eight, experience
This experiment compared to the first experiment, increased the use of software, not only to edit, compile, run, debug code, but also modeling, unit testing, so this experiment encountered the biggest problem is the use of software, installation, operation. Every step has a problem, first of all, the installation of staruml, after the download did not know how to install, and finally after the installation and found not to run, and later learned that the original version is not correct, and ultimately successfully downloaded. The experiment is more difficult than the last time, in the implementation of the code to spend a longer period, fortunately, the teacher demonstrated the process is used by eclipse, I have been using it, so follow the teacher experiment two blog content to do a little bit, the teacher blog is written in detail, the steps I basically follow the practice once. In the modeling aspect, I download is staruml, and teacher blog inside of not the same, so oneself Baidu steps, should be right. Do this practice I found myself in writing code is still very deficient, can only follow the book to find similar, encountered specific problems can not be solved independently, so to strengthen this practice!!!
20145326 Java Programming Experiment two Java object-oriented programming experiment report