Java Course Lab report Experiment two Java object-oriented programming

Source: Internet
Author: User

Beijing Institute of Electronic Technology (BESTI)

Real Inspection report

Course: Java Programming Class: 1353 Name: Han Yuqi No.: 20135317

Score: Instructor: Lou Jia Peng Experimental Date: 2015.5.6

Experiment level: Preview degree: Experiment time: 15:50--20:50

Instrument Group: Compulsory/Elective: Elective experiment number: 2

Experiment Name: experiment two Java Object-oriented programming

Experimental purposes and requirements:

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

Experimental instrument:

Name

Model

Number

Pc

Lenovo

1

Laboratory building Environment

/

1

Eclipse

/

1

First, the contents and steps of the experiment

(a) Unit testing

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

(2) Product Code

(3) test code

Because it is the code of their own, did not follow the hint step by step, so, not a lot of problems.

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:

Run:

Suppose the beginning did not take 100 into account (error condition):

(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:

In UML, the property of a class can display its name, type, initialization value, and attributes can also display private,public,protected. The methods of the class can display their method names, parameters, return types, and the Private,public,protected property of the method. Where: + represents public #表示 protected-represents private

Using UML lets us not focus on the details.

(3) The test class at this time:

Note: UML class diagrams show static relationships between classes, animaltest classes rely on the dog class and cat classes, and UML relies on lines with arrows.

Corresponding Code:

(3) Both the dog and cat classes have the color attribute and the corresponding set and get methods, which clearly violate the aforementioned dry principle, and we can solve this problem by inheriting the color attribute and the corresponding method into the parent class animal, as shown in the following UML diagram:

(Note that the representation of inheritance in UML class diagrams is to refer to the parent class with a triangular line)

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:

Code to run:

(iii) Preliminary design pattern

1. S.O.L.I.D principle

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)

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. Each pattern can be reused because of its repeatability, has its own name, can be imparted, and can be transplanted 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

(1) 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.

Behind the design pattern is the principle of abstraction and solid.

(2) 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)

(3) Understand the design patterns that may exist over design issues and how to avoid them.

(iv) Practice

1, the use of TDD design off the implementation of the plural class complex.

(1) Pseudo-code

(2) Product Code

Complex print, add, subtract

public class Complex {

private int sh,xu;

Complex () {

this.sh=0;

this.xu=0;

}

Complex (int sh) {

This.sh=sh;

this.xu=0;

}

Complex (int sh,int Xu) {

This.sh=sh;

This.xu=xu;

}

public void Addfu (Complex p1,complex p2) {

System.out.println ("The sum of these two complex numbers is:");

this.sh=p1.sh+p2.sh;

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.sh=p1.sh-p2.sh;

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.sh+ "+" +this.xu+ "I");

}else if (this.xu<0) {

System.out.println (this.sh+ "" +this.xu+ "I");

}else{

System.out.println (this.sh);

}

}

}

(3) test code

(4) Problems when writing code

(at the beginning of the function is not extracted, write a lot, and then extract the print function is a lot less.) )

The pseudo-code at the beginning of the design is this:

Then the code of the design is tested out like this:

The imaginary part is printed with a negative number of "+-" two symbols. So it would be nice to separate the two cases.

2, the report of the experiment to count their own PSP (Personal software Process) time

Steps

Take

Percentage

Demand analysis

25min

18%

Design

35min

25%

Code implementation

30min

21%

Test

30min

21%

Analysis Summary

20min

15%

3. Summarize the benefits of unit testing

(1) Make it easy to modify test code without worrying about the test code that will affect the design.

(2) from the caller's point of view of their own writing code, and from the writing point of view or not the same, in the writer's perspective, will be thinking how to facilitate the rapid completion of the entire code, but from the caller's point of view, will consider the use of other people's convenience, more emphasis on testing and invocation.

(3) 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.

Java Course Lab report Experiment two Java object-oriented programming

Related Article

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.