Java Experiment II

Source: Internet
Author: User

Two experimental reports on Java experiment

Experimental content

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 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 in Blog, note the Experiment report focus is Run Results, encountered the problem(Tools find, install, use, program edit, Debug, run, etc.), Solutions(empty methods such as "Check Network", "Ask classmates", "reading" and so on are 0 points) and Analysis(What can be learned from it, what gains, 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, before you Laboratory Building in the ~/Code directory with your own school number to create a directory, code and UML figure to be placed in this directory, no study number will require redo, and then follow the following steps to practice。 Experimental Step (i) Unit test

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

First create a directory:

(1) Pseudo-code:

Percentile five-point system:

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

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

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

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

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

Other, turn to "error" (2) Product Code:

(3) Test code:

Write the test code first. 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

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. There is a unit test tool JUnit in Java to aid TDD, and we use TDD to rewrite the example of the previous percentile five-point system,

Specific operations such as:

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, which constructs the system into a "real-world" object in a way that is very close to the problem domain terminology, and focuses on howto implement the functional specification 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. Ood Modeling uses the graphical Modeling language UML (Unified Modeling Language), UML is a generic modeling language, we experiment with Umbrello for modeling, and Windows recommends that you use STARUML.

The result of a process abstraction is a function, and the result of the data abstraction is an abstract data Type,adt, which can be used as an ADT with inherited and polymorphic mechanisms. Data Abstraction is the core and origin of OOP.

, encapsulation is the hiding of data and related behaviors to implement information. Classes are encapsulated in Java,

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 modularity (modularity) and information hiding ( Information hiding) , interface (interface) is an accurate description of the package. Dogclasses use classes and access control (Private,public) to hide Properties color , open interfaces 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:

Class diagrams in UML are used to describe classes

Inheritance is the foundation of software reusability, and it is the main way to improve the expansibility and maintainability of software system. As shown above, based on encapsulation, inheritance can implement code reuse , it should be noted that inheritance more important role is to implement polymorphism . Object-oriented objects allow different classes of objects to respond to the same message, which means that the same message can be used in a variety of different ways depending on the sending object, and we call this behavior polymorphic. In Java, polymorphism refers to a phenomenon in which different class objects 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) design pattern preliminary (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 principle of class design 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, 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
    • ...
    • Attern 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
      • The pseudo-code class has the addition operation complex number of the imaginary part real complex number subtraction operation complex multiplication complex number division operation
· (3) design pattern Real Example (iv) Exercise 1 using TDD to design the implementation of the complex class complex.

Product Code

public class Complexdemo {

Main method

public static void Main (string[] a) {

Complex B = New Complex (2, 5);

Complex C = New Complex (3,-4);

System.out.println (b + "+" + c + "=" + B.add (c));

System.out.println (b + "-" + c + "=" + B.minus (c));

System.out.println (b + "*" + c + "=" + b.multiply (c));

System.out.println (b + "/" + c + "=" + B.divide (c));

}

}

Complex class

Class Complex {

Private double m;//Real part

Private double n;//imaginary part

Public Complex (double m, double N) {

THIS.M = m;

THIS.N = n;

}

Add

Public Complex Add (Complex c) {

return new Complex (M + c.m, n + C.N);

}

Minus

Public Complex minus (Complex c) {

return new Complex (M-C.M, N-C.N);

}

Multiply

Public Complex Multiply (Complex c) {

return new Complex (M * c.m-n * C.N, M * C.N + n * c.m);

}

Divide

Public Complex Divide (Complex c) {

Double d = math.sqrt (C.M * c.m) + math.sqrt (C.N * C.N);

return new Complex ((M * c.m + N * C.N)/D, Math.Round ((M * c.n-n * c.m)/d));

}

Public String toString () {

String rtr_str = "";

if (n > 0)

Rtr_str = "(" + M + "+" + N + "I" + ")";

if (n = = 0)

Rtr_str = "(" + M + ")";

if (n < 0)

Rtr_str = "(" + M + n + "I" + ")";

return rtr_str;

}

}

Test code

public static complextest{

public static void Main (string[] args) {

Class Complex {

Private double m;//Real part

Private double n;//imaginary part

Public Complex (double m, double N) {

THIS.M = m;

THIS.N = n;

}

Public String toString () {

String rtr_str = "";

if (n > 0)

Rtr_str = "(" + M + "+" + N + "I" + ")";

if (n = = 0)

Rtr_str = "(" + M + ")";

if (n < 0)

Rtr_str = "(" + M + n + "I" + ")";

return rtr_str;

}

}

}

}

· 2. Own PSP (Personal software Process) time

Steps

Time-consuming percentage

Analysis of the requirements separately

5%

Design

20%

Code implementation

60g

Test

20%

Analysis Summary

15%

· 4. Summarize the benefits of unit testing

1, after the unit test code, quality can be guaranteed, reduce the bug

2, improve the feedback speed, unit test found that the problem is easy to locate.

3, change the code to make mistakes, after unit testing easy to find

4. Ensure that the last code modification does not break the functionality of the previous code.

Problems encountered and their solutions

1. Because the bedroom network speed is too slow, often lag, or results did not have time to save.

2. There is no code to run through but can not find the problem where, often because the Chinese semicolon problem,

3. The practice does not design the plural class complex pseudo-code, completes with the classmate's help.

Experimental summary

This experiment more focus on practical application, more attention to thinking exercise, this experiment I mastered some practical programming knowledge, at the same time for the basic content of the previous stage of learning is a review review.

In addition, the experiment is a lot of knowledge is the first to hear, the beginning will inevitably have embarrassed mood, in fact, according to step by step to be able to grasp, so later in the study should have the consciousness to cultivate their own spirit of study, improve learning ability.

Java Experiment II

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.