Java Experiment 2 Experimental report (20135131)

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 in the ~/code Directory of the Lab building with your own number, and the code and UML diagram should be placed in this directory. If you do not have a study number, you will need to redo it, then follow the steps below to practice .

Third, the experimental steps

(a) Unit testing

(1) Three kinds of code

    • Pseudo code
    • Product Code
    • Test code

We 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:

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

(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

30min

12.5%

Design

50min

21%

Code implementation

60min

25%

Test

60min

25%

Analysis Summary

40min

16.5%

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

4. Summarize the benefits of unit testing

Four, the experiment

Five, exercises

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

}

}

}

Vi. problems encountered and their solutions

Problems: First of all, from the beginning of the experiment can not be normal use of the experimental building, and then give up the experiment building, because the first part of the experiment is not a non-experimental building environment;

Secondly, in the process of familiarity with UML later, the speed is too slow, resulting in the whole process of special slowness, and because of their own impatience, always appear wrong, such as more dozen parentheses, or the hand mistakenly deleted the content. Originally wanted to recover, began to press the Undo, eventually unknown reason back to the desktop, and then all the things are gone. At present, this still does not solve, so I start from the beginning, and later also the arrow to get the reverse, at present do not know how to do.

There is the test code in the process of writing because you are not familiar with the programming process so it is not very well written, and finally to the product code to write the test code, so the test code has a lot of problems, it took a lot of time to modify and write the code eventually will not run smoothly.

Vii. Summary and experience

The experiment takes too long to practice, not only the problem of the network, but also is not familiar with the experimental environment and experimental content. In addition, the benefit of unit testing is that it allows the person who writes the program to constantly consider the problems that may arise from a variety of programs, to make the program more reliable, to think more fully, and to make users more successful. There are still some problems in writing code, there is no correct programming idea in the mind, the code is changed and changed. In the future practice still need to work hard, time and again the failure will eventually give me a lot of experience.

Java Experiment 2 Experimental report (20135131)

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.