Experiment two Java object-oriented programming

Source: Internet
Author: User

Beijing Institute of Electronic Technology (BESTI)

Lab Report

Course: Java class: 1352 name: Jing Mengxin No.: 20135218

Score: Instructor: Lou Jia Peng Experimental Date: 2015.5.8

Experiment level: Preview degree: Experiment time: 15:30~18:00

Instrument Group: 18 Compulsory/elective: Elective experiment number: 02

Experiment Name: 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

Computer

Dell

1

Statistics for PSP (Personal software Process) time

Steps

Time-consuming (min)

Percentage

Demand analysis

15

7.5%

Design

40

20%

Code implementation

45

22.5%

Test

40

20%

Analysis Summary

60

30%

Experiment Content One:

(a) Unit testing

(1) Three kinds of code

What to do before programming, how to do to think clearly before you write the program and write well. To use the program to solve the problem, you will write three kinds of code:

    • Pseudo code
    • Product Code
    • Test code

Now, let's use an example to illustrate how to write these 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.

Pseudo code:

If the score is less than 60, fail

If the score is in 60~70, pass

If the score is 70~80, medium

If the score is 80~90, good

If the score is 90~100, good

Product Code:

Use Java to translate pseudo-code

Test code:

Used to test the product code.

Tried 50 of this number

But 50 is obviously not enough, the bottom of a number of test groups of data.

Test again-10 and 115 of this error data, found that the run-10 result is incorrect, modified, tested.

Test the boundary data:

Found 100 wrong, make the code change, test pass.

(2) 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
    • 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 do not have an over-design situation, and the requirements are expressed through test cases, and our product code is only allowed to pass the test.

Still take the above example as an example:

We add the first test case, the test case method name arbitrarily, and enter the following code:

There is a syntax error in the code at this point, the reason is simple, the MyUtil class does not exist, the Percentage2fivegrade method in the class does not exist, we TDDDemo src create a new class in the directory, MyUnil and implement the Percentage2fivegrade method, as shown in:

Right-click to run Debugging

The test results showed a red bar, indicating that the test did not pass, enter the following code, to debug successfully.

Add a test exception condition and run the debug successfully.

(ii) Object-oriented three elements

(1) Abstract

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 (three elements)

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.

Based on encapsulation, inheritance can implement code reuse , it should be noted that inheritance is more important to achieve polymorphism.

  polymorphism refers to the phenomenon that different class objects will 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) Preliminary design pattern

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 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 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
      • 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 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

    (iv) Practice
  • Pseudo code:

    First, a complex class complex is designed to define three construction methods:

    ① The default is real and imaginary parts are 0 when there is no parameter;

    ② a parameter defaults to a real number, that is, the imaginary part is 0,

    ③ two parameters for both real and imaginary parts

    Then define two member methods to calculate the sum of two complex numbers and the difference. Defines a print () method that outputs a complex number and does not output imaginary parts when the imaginary part is 0. Finally, a song class is defined using the complex class, in which two complex objects are created in the main method of the class, respectively, and the sum and difference of the two complex numbers are computed and output.

    • Product Code:

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

Two: Summarize the benefits of unit testing:

In the unit test activity, the independent unit of the software will be tested in isolation from the rest of the program, looking for errors, writing high-quality code, and improving the level of programming.

It also allows the code to be assured of modification and refactoring, to enable programmers to design software modules from the perspective of the caller rather than the implementation, and to enable programmers to write software modules that are easy to test and invoke.

This facilitates decoupling, and the test itself can be used as a description of the code being measured, thereby replacing some of the document functionality.

Third, the problems encountered and solutions

Define the JUnit test case in the source file, the computer always prompts java4 must have Java5 ... Workaround: Baidu will be carried out, and repeated attempts.

Iv. Harvest of Experiments

This experiment let me understand that Java code is not as simple as we think, in order to code a one-time convenience accurate, we should write three kinds of code: pseudo-code, product code, test code, such a program is more easy to use.

Using good unit tests is good for future Java programming and should be familiar and mastered.

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.