Java Second Experiment report 20135231

Source: Internet
Author: User
Tags function definition

Java Lab Report Two: Java Object-oriented programming

20135231 Hejia

Experimental requirements:

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. Familiarity with S.O.L.I.D principles 5. Understanding Design Patterns

Experimental content:

(i) Unit testing (ii) object-oriented three elements (iii) design pattern preliminary (iv) exercise

Experimental steps:

(a) unit testing (1) Three kinds of code

programming is intellectual activity, not typing, what to do before programming, how to do to think clearly to write the program, write well. With a lot of students at present, a program to open the editor to write code, I hope that students develop a habit, when you want to solve problems with the program, you will write three kinds of code: pseudo-code, product code, test code.

Pseudo-code is independent of the specific programming language, which 伪代码 solves the problem from the intent level and, ultimately, 伪代码 is the 产品代码 most natural and best comment.

The code is:

public class Myutil

{

public static String Percentage2fivegrade (int grade)

{

If the score is less than 60, turn into "fail"

If (Grade < 60)

Return "Failed";

If the score is between 60 and 70, turn into "pass"

else if (Grade < 70)

return "Pass";

If the score is between 70 and 80, turn into "medium"

else if (Grade < 80)

Return "Medium";

If the score is between 80 and 90, turn into "good"

else if (Grade < 90)

return "good";

If the score is between 90 and 100, turn into "good"

else if (Grade < 100)

return "excellent";

Other, turn into "error"

else return "error";

}

}

public class Myutiltest

{

public static void Main (string[] args)

{//Test normal condition

if (Myutil.percentage2fivegrade (55)! = "Failed")

System.out.println ("Test failed!");

else if (Myutil.percentage2fivegrade (65)! = "Pass")

System.out.println ("Test failed!");

else if (Myutil.percentage2fivegrade (75)! = "Medium")

System.out.println ("Test failed!");

else if (Myutil.percentage2fivegrade (85)! = "good")

System.out.println ("Test failed!");

else if (Myutil.percentage2fivegrade (95)! = "excellent")

System.out.println ("Test failed!");

else System.out.println ("Test passed!");

}

}

(2) TDD (Test driven devlopment, test-driven development)

Definition: Write first 测试代码 , then write 产品代码 the development method

TDD general 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 (no product code)
    • Write the Product Code
    • Test Pass
    • Refactor the code and ensure the test passes (refactoring the next lab session)
    • Cycle to complete the development of all functions.

(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, and abstract thinking ability is very important in program design," Refine, Youbiaojili and different " . To a large extent, the abstract ability of the programmer to determine the program design capabilities. 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 that constructs the system as a "real-world" object in a way that is very close to the problem domain terminology, focusing on how to doit, Implement functional specifications 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.  

(iii) Preliminary design pattern

(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 design principle 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)

OCP is one of the most important principles in Ood, and the OCP's content is:

    • Software entities (class, modules, function, etc) should open for extension,but closed for modification.
    • Software entities (classes, modules, functions, etc.) should be open to expansion and closed to modifications.

The behavior of the extended open for Extension software module must be extensible, when the application needs change or needs to meet the new application needs, we have to let the module work in different ways, the modification is closed (Closed for modification Requires that the source code of the module is not modifiable, and no one is allowed to modify the existing module. Based on the OCP, with the object-oriented polymorphism (polymorphic), more flexibility in handling change embrace changes, the OCP can be implemented by: (1) abstraction and inheritance, (2) interface-oriented programming.

The SRP content is:

    • There should never be + than one reason for a class to change
    • Never have more than one reason to modify a class

objects provide a high degree of encapsulation of a single responsibility, and object changes depend only on the change of a single responsibility, which is based on the definition of high cohesion in software design.

The LSP content is:

    • Subtypes must is substitutable for their base types
    • Functions that use pointers or references to base classes must is able to use objects of derived classes without knowing I T
    • The subclass must be able to be used by its base class
    • Using a pointer to a base class or a referenced function, you must be able to use it without knowing the specific derived class object type

The content of the ISP is:

    • Clients should not being forced to depend upon interfaces the They do
    • Customers should not rely on interfaces that they do not use

The content of the dip is:

    • The modules should not depend upon low level modules. Both should depend upon abstractions
    • Abstractions should not depend upon details. Details should depend upon abstractions
    • High-level modules should not be dependent on low-layer modules. Both should be dependent on the abstract
    • Abstraction should not depend on detail. Details should depend on abstraction

(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

Design pattern provides a subsystem or component for refining a software system, or a diagram between them, which describes the public rendition structure of a communication component, which can solve a design problem in a particular context. , as the number of objects in the system increases and the interactions between objects grow exponentially, design patterns can help us design the system in the best possible way. Behind the design pattern is the principle of abstraction and solid. 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) Practice

1. use TDD to design the complex to implement the plural class.

Pseudo code

    • No input---The real part of the complex number is 0 and the imaginary part is 0
    • Enter only the real part---the real part of the complex is the actual part entered, the imaginary part is 0
    • Enter only the imaginary part---the real part of the complex number is 0, the imaginary part is the imaginary part entered
    • The imaginary part of the real part---the real part of the complex number and the imaginary part correspond to the actual imaginary part of the corresponding input.
    • Addition---the real part and the real part of the complex, the imaginary part and the imaginary part, namely the output P1.repart+p2.repart,p1.impart+p2.impart
    • Subtraction---The real part of the complex number is subtracted from the real part, and the imaginary part is subtracted from the imaginary part, that is, the output P1.repart-p2.repart,p1.impart-p2.impart

Test code

Public class Complextest

Public static void Main (string[] args)

Complex c=new Complex ();
Complex c1=new Complex (4,8);
Complex c2=new Complex (2,5);
C1. Print ();
C2. Print ();
System.out.println ("The sum of the two complex numbers:");
System.out.println ((C.jia (c1, C2). repart+ "+" +c.jia (c1, C2). impart+ "I"). ToString ());
System.out.println ("The difference between the two complex numbers is:");
System.out.println (C.jian (c1, C2). repart+ "+" +c.jian (c1, C2). impart+ "I");

}

Product Code

Public class Complextest

Public static void Main (string[] args)

Complex c=new Complex ();
Complex c1=new Complex (4,8);
Complex c2=new Complex (2,5);
C1. Print ();
C2. Print ();
System.out.println ("The sum of the two complex numbers:");
System.out.println ((C.jia (c1, C2). repart+ "+" +c.jia (c1, C2). impart+ "I"). ToString ());
System.out.println ("The difference between the two complex numbers is:");
System.out.println (C.jian (c1, C2). repart+ "+" +c.jian (c1, C2). impart+ "I");

}

2. Report the time of your PSP (Personal software Process)

Steps

Take

Percentage

Demand analysis

40min

16%

Design

60min

24%

Code implementation

90min

36%

Test

30min

12%

Analysis Summary

30min

12%

Summarize the benefits of unit testing:

write the test code first, you can guide me to complete a class that can implement these functions, the code has a whole grasp. and can be responsible for the module function definition as clear as possible, the internal changes of the module will not affect the other modules, and the quality of the module can be stable, quantitative guarantee.

Java Second Experiment report 20135231

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.