Java Course Lab report Experiment two Java object-oriented programming

Source: Internet
Author: User
Tags dashed line getcolor

Course: Java Programming Class: 1352 Name: Albert Wong Study No.: 20135315

Score: Instructor: Lou Jia Peng Experimental Date: 2015.5.7

Experiment level: Preview degree: Experiment time: 15:50--20:50

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

Experimental Purpose:

1. Mastering unit Tests 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 instrument:

Name

Model

Number

Pc

Lenovo

1

Laboratory building Environment

/

1

Eclipse

/

1

first, the contents and steps of the experiment

One, unit testing and TDD

Code Listing 1:

public class myutil{

public static String Percentage2fivegrade (int grade) {

if ((Grade < 0))

return "error";

else if (Grade < 60)

Return "Failed";

else if (Grade < 70)

return "Pass";

else if (Grade < 80)

Return "Medium";

else if (Grade < 90)

return "good";

else if (grade <= 100)

return "excellent";

Else

return "error";

}

}

Code Listing 2:

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

}

}

Two, object-oriented three elements

Code Listing 1:

public class Animaltest {

public static void Main (String[]args) {

Dog D=new Dog ();

D.setcolor ("Yellow");

GetInfo (d);

Cat c=new Cat ();

C.setcolor ("Black");

GetInfo (c);

}

public static void GetInfo (Dog d) {

System.out.println (D.tostring ());

}

public static void GetInfo (Cat c) {

System.out.println (C.tostring ());

}

}

Code Listing 2:

Package dog;

public class Dogtest {

public static void Main (string[] args) {

Dog G=new Dog ();

G.setcolor ("Yellow");

GetInfo ();

}

public static void GetInfo (Dog d) {

System.out.println (D.tostring ());

}

}

Code Listing 3:

Public abstract class Animal {

private String color;

Public String GetColor () {

return color;

}

public void SetColor (String color) {

This.color = color;

}

Public abstract String shout ();

}

public class Dog extends animal{

Public String shout () {

return "Bark";

}

Public String toString () {

Return "The Dog ' s color is" + this.getcolor () + ", and it shouts" + this.shout () + "!";

}

}

public class Cat extends animal{

Public String shout () {

Return "Meow Meow";

}

Public String toString () {

Return "The Cat ' s color is" + this.getcolor () + ", and it shouts" + this.shout () + "!";

}

}

Iii. Examples of design patterns

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)

OCP is one of the most important principles in Ood, and the OCP's content is:

Software entities (class, modules, function, etc) should open for extension,but closed for modification.

Software entities (classes, modules, functions, etc.) should be open to expansion and closed to modifications.

Based on the OCP use of object-oriented polymorphism (polymorphic), more flexibility in handling change-embracing changes OCP can be achieved with abstraction and inheritance, interface-oriented programming means

SRPThe content is:

    • There should never be + than one reason for a class to change
    • Never have more than one reason to modify a class

LSPThe content is:

    • Subtypes must is substitutable for their base types
    • Functions that use pointers or references to base classes must is able to use objects of derived classes without knowing I T
    • The subclass must be able to be used by its base class
    • Using a pointer to a base class or a referenced function, you must be able to use it without knowing the specific derived class object type

ISPThe content is:

    • Clients should not being forced to depend upon interfaces the They do
    • Customers should not rely on interfaces that they do not use

DIPThe content is:

    • The modules should not depend upon low level modules. Both should depend upon abstractions
    • Abstractions should not depend upon details. Details should depend upon abstractions
    • High-level modules should not be dependent on low-layer modules. Both should be dependent on the abstract
    • Abstraction should not depend on detail. Details should depend on abstraction

Note the following 5 points:

Code:

Abstract class Data {
Abstract public void Displayvalue ();
}
Class Integer extends Data {
int value;
Integer () {
value=100;
}
public void Displayvalue () {
System.out.println (value);
}
}
20135215
Abstract class Factory {
Abstract public Data createdataobject ();
}
Class Intfactory extends Factory {
Public Data CreateDataObject () {
return new Integer ();
}
}
20135215
Class Document {
Data PD;
Document (Factory pf) {
PD = PF. CreateDataObject ();
}
public void Displaydata () {
Pd. Displayvalue ();
}
}
public class MyDoc {
static Document D;
public static void Main (string[] args) {
D = new Document (new Intfactory ());
D.displaydata ();
}
}

(iv) Experiment exercises use TDD to design the complex of the complex class.

Code Listing 1:

Package complex;

Public class complextest{

public static void main (string[] args) {

Complex c=new Complex ();

Complex c1=new Complex (6,12);

Complex c2=new Complex (11,32);

C1. Output ();

C2. Output ();

System. out. println ("These two plural and for:");

System. out. println (C.add (c1, C2). r+ "+" +c.add (c1, C2). i+ "I");

System. out. println ("These two complex differences are:");

System. out. println (C.reduce (c1, C2). r+ "+" +c.reduce (c1, C2). i+ "I");

}

}

Code Listing 2:

Package complex;

Public class complex{

double r,i;

Complex ()

{

this.r=0;//Real numbers

this.i=0;//plural

}

Complex (double re,double im)

{

This.r=re;

This.i=im;

}

Complex reduce (Complex p1,complex p2)

{

Complex P =new Complex (p1.r-p2.r,p1.i-p2.i);

return p;

}

Complex Add (Complex p1, Complex p2) {

Complex P =new Complex (p1.r+p2.r,p1.i+p2.i);

return p;

}

void Output ()

{

System. out. println ("The value of the complex number is:");

if (this.i!=0)

System. out. println (this.r+ "+" +this.i+ "I");

Else

System. out. println (THIS.R);

}

}

Benefits of the experiment:

By first writing the pseudo-code, you can make the product code more fluid, writing test code to minimize the bug in the product code. Therefore, unit testing can effectively improve the programming efficiency and reduce the bugs in the program written.

Problems encountered in the experiment:

The definition of a plural class is not known.

Second, the experimental feelings

1, the experiment took a long time, and finally do not go on. I deeply appreciate the importance of the network and the ability to learn new ways and use new tools.

2, UML diagram is a little abstract, the implementation of the virtual machine when the operation is not skilled, just beginning has not understood how to bring that box up (because the experimental building virtual machine has been a problem, I did not calm down to see the operation steps on the "Click the class icon on the toolbar, and then click" in Class Diagram ") , after the arrow is not drawn up, Baidu tutorial, should be first click on the toolbar arrow icon (Implementation Relationship) and then click on the starting class, a dashed line, and then click on the class. Finally I can draw out the picture.

3, TDD Way sample test code to understand, but they write when they do not know which statements how to use, had to write their own test code, may not be strict TDD way, but I think that the idea of my own design some of the independent function is a great help.

4, do practice code, a see to realize the plural class do not know what to do, on the internet search "Complex", found that there is output, addition, subtraction function, and then I followed the idea of writing pseudo-code. The beginning of the time did not pull out the function, verbose write a lot, and then extract the print function, each function alone, the code will become less.

Then I finished the above report, see the teacher gave the plural class and the function of multiplication, I put the code out, and then realized the multiplication function:

Java Course Lab report 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.