Experiment two Java object-oriented programming

Source: Internet
Author: User

A Experimental content 1. Initial mastery of unit testing and TDD2. Understand and master the object-oriented three elements: encapsulation, inheritance, polymorphism 3. Initial mastery of UML modeling 4. Familiar with S.O.L.I.D principle 5. Understanding design mode two. Experimental requirements 1. Students without a Linux base are advised to first learn the LinuxBasic Primer (new version)"VimEditorCourse 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, the problems encountered (tool find, installation, use, program editing, debugging, running, etc.), solutions (empty methods such as "Check Network", "Ask classmates", "reading" 0 points) as well as 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 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 .

Experiment Report on your PSP (Personal software Process) time

percent

[

TD valign= "Top" width= "175" " valign= "Top" width= "175" >

steps

Time-consuming

Requirements analysis

1h

Design

2h

+

Code implementation

30mins

Ten

Test

30mins

Ten

min Summary

30mins

Ten

Three Experimental Step (i) unit Test (1) three codes

Programming is intellectual activity, not typing, what to do before programming, how to do to think clearly to write the program, write well. With a lot of students at present, a program to open the editor to write code, I hope that students develop a habit, when you want to solve problems with the procedure, you will write 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.

1 First write pseudocode , in a specific programming language translation, is the available product code, of course, we have to choose Java.

2 Test Cases are a set of test inputs, execution conditions, and expected results for a particular goal to test a program path or verify that a specific requirement is met. Here our test input is "", the expected result is " less than lattice ."

3The general code is most prone to error at the boundary, we have not tested the boundary condition, we have input as"0, -, -, the, -,"these boundary conditions are tested and found in the boundary condition where the input -when there is aBug. We modifyMyutil.java,The criteria for good judgment include the input as -the situation.

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

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.

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

2 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 catalog,

3 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

4 Create a new Myutil class in the src directory of Tdddemo and implement the Percentage2fivegrade method because the Myutil class does not already exist, and the class The Percentage2fivegrade method does not exist either .

5 Put the mouse on the Myutiltest.java, right-click, select Run As->junit Test

6 a green bar appears, indicating that the test passed

(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, in the abstract, andthes.o.l.i.d design principle is a good guide.

(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

The most important is the design pattern, the position of design pattern in object-oriented can be equal to the position of the data structure in the process programming.

(3) design pattern Real Example

Design Pattern provides a subsystem or component for refining a software system, or a diagram between them, which describes the public rendition structure of a communication component, which can solve a design problem in a particular context.

(iv) Practice

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

Pseudo-code // plural class with the addition of the real complex number of the virtual part complex number of the subtraction operation complex multiplication complex number division operation

 // product code   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 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; }

}

}

}

Four Experimental Thoughts

The experiment compared to the experiment I think it is more difficult, the experiment one time and did not do in the laboratory building, so it is not clear the use of the requirements, in this experiment is finally into the experimental building, just beginning to use the time is not used to, but after the use of the discovery is actually very convenient, the experiment although difficult, spent a lot of But that's a lot more to learn than videos and textbooks.

Five Problems encountered and their solutions

1. The experimental building went in but the experiment 2 points Open did not respond, to help the teacher found that is the browser problem, after the change of Firefox is good.

2.UML do not know where to open, ask the classmate know is the point terminal virtual machine, and then came out.

Experiment two Java object-oriented programming

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.