Java Experiment II (object-oriented)

Source: Internet
Author: User
Tags visibility

Note: Due to network Kaka, part of the content is done on Eclipse's own computer

Lesson: Java Programming Class: 1351

Name: Kang Wenmin No.: 20135112

Score: Instructor: Lou Jia Peng Experimental Date: 2015.05.06

Test level: Preview degree: Experiment time: May 8, 2015-May 9

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

Experiment Name: Java Object-oriented programming

Experimental purposes and requirements:

1. Students who do not have a Linux base are advised to start with the Linux basics (new version) Vim Editor course

2. Complete the experiment, write the experiment Report, the experiment report is published in the blog Garden, note that the experiment report focuses on the running results , problems encountered (tool find, installation, use, program editing, debugging, running, etc.), solutions (empty methods such as "Check the network", "Ask classmates", "reading" and so on all get 0 points) and analysis (from which can get what revelation, what harvest, lessons, etc.). The report can refer to the guidance of Fan Fei Dragon Teacher

3. Plagiarism is strictly forbidden, and the results of the experiment of the perpetrator are zero, and other punitive measures are added.

4. please first in the laboratory building in the ~/code directory with their own school number to create a directory, code and UML diagram to put in this directory, no study number will be required to redo, and then follow the following steps to practice .

Experimental instrument:

Name

Model

Number

Pc

1

Virtual machines

Laboratory Building

1

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:

(a) Unit testing

1. Take "percentile to five points" as an example, introduce three kinds of code

(1) Pseudo-code (2) Product Code (3) test code

2. Follow the instructions in the experiment to enter the code and run the test results:

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

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

About the use of test tools:

Open Eclipse, click File->new->java Project to create a new Tdddemo Java project;

In the Tdddemo project, put the mouse on the project name Tdddemo, right-click, select New->source folder in the pop-up menu to create a new test directory testing;

Put the mouse on the test directory, right-click, select the New->junit test case in the pop-up menu to create a new test cases class myutiltest;

Thus, according to the experimental code given, the experimental results are obtained:

(ii) Object-oriented three elements

(1) abstract (2) encapsulation, inheritance, polymorphism

Using the class diagram in UML to describe the class dog, first open the shell in the environment of the experimental building, enter Umbrello in the command line, open the UML modeling software Umbrello;

Click the class icon on the toolbar and then click in class Diagram, and a box will pop up to enter the class name dog.

Put the mouse on the dog class, right-click, select Properties, in the popup dialog box in the display to remove the public only option;

Put the mouse on the dog class, right-click, select New->attribute, in the Pop-up dialog box, fill in the Type,name, and choose a good visibility;

Put the mouse on the dog class, right-click, select New->operation, in the Pop-up dialog box, fill in the Type,name, and choose visibility.

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

    • + indicates public
    • #表示 protected
    • -Indicates private

The operation results are as follows:

Save the file as "Jiangwenmin", there is code/20135112

The corresponding Java code is:

(iii) Preliminary design pattern

S.O.L.I.D Principles

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)

Third, the problems encountered and solutions

1, see the experimental instructions to open the shell, their own silly in the group asked "where the shell on the table". Later just remembered to turn over the record of the experiment one, understand should open xface, then the system automatically run shell program. Linux systems are relatively unfamiliar to themselves, and will not be able to do more review of previous experimental records.

2, when doing UML diagram, animal class of shout () does not display italic body. Think of the steps that might be missing, and I forgot to tick the "abstract method" column.

Iv. Harvest of Experiments

1. I heard the test code and TDD for the first time. Before learning C also understand to the Code white box, black box test, but often this test behavior is spontaneous, not systematic, there is no record, or even think of where to test where. After learning the JUnit test case, it is found that such a unit test system is very useful, not only beautiful interface (see green strips of good fun), but also powerful, can well record the process of developer testing, easy to manage and maintain code. Sometimes it takes more time to test the code than the product code, but because of this, the integrity and robustness of the program can be ensured.

2, the preliminary study of UML diagram. UML diagram can be a glance to summarize the properties and functions of each class, easily see the relationship between the various types, easy to extract and abstract. I heard that UML can and Java code to transform each other, is really magical, need to explore.

3, learned the design of solid ideas. The next three is not too understanding, but the first two figures are very image, and in the actual learning of Java process Experience the SRP, OCP thought. There is also a reflection in the exercises.

4, the compilation exercises let me feel that is in the Java code. Previously compiled code, and did not reflect the "object-oriented" thinking, in my case, just a grammar of the C program. And today's plural class, let me realize what is abstract, encapsulation, and the first, the active use of static (before has not been thought to be used statically, the various modifiers are not very sensitive, think of the use, unexpectedly do not need)

Five, exercises

Pseudo code

Class has imaginary part real part
Addition operations of complex numbers
Subtraction of complex numbers
Multiplication of complex numbers
Division operations of complex numbers

Print a complex number

Print the arithmetic statement

public class Complexdemo {

public static void Main (string[] args) {
Complex x = new Complex (1.0,-2.0);
Complex y = new Complex (3.0, 4.0);
Complex.printsentence (x, y);
}

}
Class Complex
{
Private double A, b;
Complex (double a,double b)//Constructs a method to initialize a complex number
{
THIS.A = A;
this.b = b;
}
Static Complex Add (Complex X,complex y)
{
return new Complex (X.A+Y.A, x.b+y.b);
}
Static Complex minus (Complex X,complex y)
{
return new Complex (X.A-Y.A, x.b-y.b);
}
Static Complex multiple (Complex X,complex y)
{
return new Complex (X.A * y.a-x.b * y.b, X.A * y.b + x.b * y.a);
}
Static Complex Divide (Complex X,complex y)
{
Complex conjugate = new Complex (y.a,-y.b); Finding the conjugate complex number of the divisor y
Double mo = (y.a) * (Y.A) + (y.b) * (y.b); Calculates the modulus of Y

Complex T = multiple (x,conjugate);
return new Complex (T.a/mo, t.b/mo);
}
Static String print (Complex x)
{
if (x.b > 0)
return x.a + "+" + x.b + "I";
Else
return x.a + "-" + (-X.B) + "I";
}
static void Printsentence (Complex x, Complex y)
{
System.out.println ("(" + print (x) + ") + (" + print (y) + ") =" +print (Add (x, y)));
System.out.println ("(" + print (x) + ")-(" + Print (y) + ") =" +print (minus (x, y)));
System.out.println ("(" + print (x) + ") * (" + print (y) + ") =" +print (multiple (x, y)));
System.out.println ("(" + print (x) + ")/(" + print (y) + ") =" +print (Divide (x, y)));
}

}

The use of construction methods: Use the construction method to load the characteristics of the operation, to the complex initialization.

Returns an object: initially wanted to print directly x.a+ "+" +x.b+ "I", later found return new Complex (X.A+Y.A, x.b+y.b); Such statements are more readable and easy to maintain

Division of Labor: four of the complex number of methods of operation, provide interface (return value), internal operation process is encapsulated. Given the SRP single-duty principle, the ability to print statements in the arithmetic method is discarded

Extraction: Printing a complex number of processes to be used multiple times, can be extracted, in line with the dry principle

Encapsulation: In the beginning, I thought about calling the print (ADD (x, y) output statement function of the object in the main function, which later felt violated the OCP open-closure principle, because the Print method and the Add method inside the class were exposed. Finally, four kinds of printed statements are encapsulated as a function, called printsentence (Complex x, Complex y), and because the method involves two objects, not suitable for an object to invoke, so the thought of calling with the class name, so static, the relevant methods are all static.

Operation Result:

Vi. Time Statistics

Steps

Take

Percentage

Demand analysis

3min

3.2%

Design

10min 10.8%

Code implementation

50min 53.8%

Test

20min 21.5%

Analysis Summary

10min 10.8%

Java Experiment II (object-oriented)

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.