Experiment two Java object-oriented programming

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 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 set up a directory, code and UML in the ~/code directory of the lab building with your own number. Figure to be placed in this directory, no study number will require redo, and then follow the following steps to practice .

Third, the experimental steps

(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

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:

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 into "error"

Product Code:

The good Myutil.java are as follows:

public class myutil{

public static String Percentage2fivegrade (int grade) {

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

If (Grade < 60)

Return "Failed";

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

else if (Grade < 70)

return "Pass";

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

else if (Grade < 80)

Return "Medium";

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

else if (Grade < 90)

return "good";

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

else if (Grade < 100)

return "excellent";

Other, turn into "error"

Else

return "error";

}

}

Test code:

public class Myutiltest {

public static void Main (string[] args) {

Percentile score is 50 o'clock should return to five grade system "fail"

if (Myutil.percentage2fivegrade (50)! = "Failed")

System.out.println ("Test failed!");

Else

System.out.println ("Test passed!");

}

}

(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. There is a unit test tool JUnit in Java to aid in TDD, and we use TDD to rewrite the example of the previous percentile to five-point system, and realize the benefits of developing with the test tool support. 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-in the popup menu >source folder Create a new test catalog, we put the mouse on the test directory, right-click, select New->junit test Case in the pop-up menu to create a new test cases class Myutiltest,

We add the first test case testnormal, note that before the test case must have the annotation @test, the test case method name arbitrary, enter the following code:

Import Org.junit.Test;

Import Junit.framework.TestCase;

public class Myutiltest extends TestCase {

@Test

public void Testnormal () {

Assertequals ("Fail", Myutil.percentage2fivegrade (55));

Assertequals ("Pass", Myutil.percentage2fivegrade (65));

Assertequals ("Medium", Myutil.percentage2fivegrade (75));

Assertequals ("Good", Myutil.percentage2fivegrade (85));

Assertequals ("excellent", Myutil.percentage2fivegrade (95));

}

}

Input complete

Red Fork Description Code There is a syntax error, the reason is very simple, the Myutil class does not exist, the Percentage2fivegrade method in the class does not exist, we are in the SRC directory of Tdddemo a new Myutil class, and implement the Percentage2fivegrade method

Now the test code is not syntax error, we put the mouse on the Myutiltest.java, right-click, select Run As->junit Test

The test results showed a green bar, indicating that the test passed.

The coding rhythm of TDD is:

    • Add test code, JUnit appears red bar
    • Modify the Product Code
    • JUnit appears green bar, Task complete

(ii) Object-oriented three elements

(1) Abstract

(2) encapsulation, inheritance and polymorphism

  • Steps:
  • 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 to pop up a Christmas box and 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
  • (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)

    (iv) Practice

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

    2. Report the time of your PSP (Personal software Process)

    Steps

    Take

    Percentage

    Demand analysis

    1h

    10%

    Design

    1h 10%

    Code implementation

    2h 20%

    Test

    1h 10%

    Analysis Summary

    1h 10%

    3. Implement to have pseudo-code, product code, test code.

    4. Summarize the benefits of unit testing

  • Four, the experimental code

  • Note: Create a folder to save the code last

    (i) Three kinds of code

    One, pseudo-code

    Second, the test code

    Third, the product code

    (ii), object-oriented three elements

Five, exercises

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

Pseudo code
Plural class
Class has imaginary part real part
Addition operations of complex numbers
Subtraction of complex numbers
Multiplication of complex numbers
Division operations of complex numbers

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;
}

}

}

}

Second, the experimental report of the statistics of their own PSP (Personal software Process) time

Steps

Take

Percentage

Demand analysis

1h

14.2%

Design

1. 5h 21st. 4%

Code implementation

2h 28.6%

Test

1.5h 21.4%

Analysis Summary

1h 14.2%
Vi. problems encountered in the experiment and solutions1, in the experiment, because the network is not stable, often appear card network, broken network situation, in the test 3 code when the time is not enough and accidentally missed delay can only be re-done.    The test code just started to run, and later found that it only works if the pseudo-code and the test code exist simultaneously. In the Product code debugging, the first time assertequals problems, and then re-done again, found to be textcase fill the wrong. 2, in the experiment two, the shell did not know how to open, and later asked the students to successfully complete the experiment. Vii. Experimental Summary and experienceThis experiment compared to the first experiment I have a great breakthrough, more skilled use of virtual machine. Unit testing for my help is also very large, more breakthroughs in their own, in the face of the problem when they learned to step after stage of the debugging, dare to try and also cultivate their perseverance and patience, TDD this has a certain difficulty code has a knowledge and understanding. Object-oriented three elements in the video before the study, through the experiment and review to consolidate again, better grasp and use this knowledge.  In the later experiments, I will follow the teacher's black out of the steps of demand analysis, redesign, can not rush to quickly write code.       Through this experiment, I dare to write code, and put the basic knowledge of the use of virtual machine, in the process of learning programming, more cultivate their perseverance quality. Green channel: Good text to the top concern my collection this article contact me

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.