Two experimental reports on Java experiment

Source: Internet
Author: User

First, the contents of the experiment

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

Second, the experimental process (this test is done on their own computer, no use of the experimental building)

(a) Unit testing

When using the program to solve the problem, you will write three kinds of code: pseudocode, Product code, test code

This first write test code, and 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

(ii) Object-oriented three elements

1. Abstraction

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 a data abstraction is an abstract data type.

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.

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

(2) mode and design mode

A pattern is the usual solution to a particular problem (problem) under an external environment (context).

The position of design patterns in object-oriented mode can be comparable to the status of data structures in process-oriented programming.

(iv) Practice

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

Pseudo code:

First, a complex class complex is designed, and the real and imaginary parts of the complex number are defined as attributes, defining 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:

Package EXP2;

Class Complex {
Double A, B;

Complex () {
THIS.A = 0;
this.b = 0;
}

Complex (double a) {
THIS.A = A;
this.b = 0;
}

Complex (double A, double b) {
THIS.A = A;
this.b = b;
}

Complex Add (complex P1, complex p2) {
Complex p = new complex (p1.a + p2.a, p1.b + p2.b);

return p;
}

Complex minus (complex P1, complex p2) {
Complex p = new complex (p1.a-p2.a, p1.b-p2.b);
return p;
}

void print () {
System.out.println ("The value of the plural is:");
if (this.b! = 0)
System.out.println (THIS.A + "+" + this.b + "I");
Else
System.out.println (THIS.A);
}
}

Test code:

Package EXP2;

public class Song {
public static void Main (string[] args) {
Complex C = new complex ();
Complex C1 = new Complex (3, 9);
Complex C2 = new complex (7, 1);
C1.print ();

C2.print ();

System.out.println ("The sum of these two complex numbers is:");
System.out.println ((C.add (c1, C2). A + "+" + c.add (c1, C2). B + "I")
. toString ());
System.out.println ("The difference between these two complex numbers is:");
System.out
. println ((C.minus (c1, C2). A + "+" + c.minus (c1, C2). B + "I")
. toString ());
}
}

Operation Result:


2.PSP (Personal software Process) time

Steps

Time-consuming percentage

Demand analysis

10%

Design

20%

Code implementation

40%

Test

15%

Analysis Summary

15%

4. 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.

The benefits of unit testing are much more than what I'm finding now, after searching the web for information. It allows code to be safely modified and refactored, allowing programmers to design software modules from the perspective of the caller rather than the implementation.

Enables programmers to write software modules that are easy to test and invoke, thereby facilitating decoupling, testing itself as a usage description of the code under test, thereby replacing some of the document functionality.

Third, the problems encountered and solutions

1.eclipse hints Animal code is wrong, and dog and cat need to be defined in the file. Workaround: Put animal and dog, cat in a different class file to compile

2. Design plural class complex pseudo-code, do not know how to classify. Solution: On-line Query the construction method of plural class, know that can define three kinds of

Iv. Harvest of Experiments

This experiment let me understand to use the program to solve the actual problem, not only just write a code on it, but the best to write three kinds of code: pseudo-code, product code, test code, such a program can be

practical meaning, convenient to use. and the use of unit testing can be in the future of the program design to make their own responsible module function definition as clear as possible, the module internal changes will not affect the other modules, and the quality of the module

Volume can be stabilized, the volume Guarantee. I hope to learn more about how to solve practical problems by using Java in the future.

Two experimental reports on Java experiment

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.