20165306 experiment two Java object-oriented programming

Source: Internet
Author: User

Experiment two Java object-oriented programming experiment requirements

1. Submit the last three JUnit test cases (normal, error, boundary condition) through, to have paint and watermark, enter their own school number. This submission point examines whether JUnit will be used, and test cases should at least contain normal, error, and boundary conditions.

2. Learn Java using JUnit and study StringBuffer in TDD. Submit your Unit test case and test pass, plus a number watermark. Testing yourself will not write junit test cases.

3. Expand the design pattern example, experience the application of OCP principle and dip principle, and understand the design pattern preliminarily. Use your own study number%6 to take the remainder operation, according to the result of code expansion.

4. Submit: Unit test code and run success and code on the cloud Codes link, to add the number watermark. Task: Develop a plural class complex in TDD mode.

Experimental content object-oriented programming-1
    • Install JUnit: Select, File->Settings Select, Plugins search JUnit , click Install JetBrains plugin , install after:

    • Put the test code in testing and set the environment variable Mark Directory as->Test Sources Root .

    • Download the junit.jar package, select File->Project Structure , Dependancies->JARs or Directories add Junit.jar, right-click Test code Selection Run ‘MyTuilTest‘ , and the Red Code section returns to normal.

    • Product Code:

    • Normal conditions, error conditions, boundary conditions:

    • Test pass:

Object-Oriented Programming-2
    • Product Code:

    • Test pass:

Object-oriented Programming-3
    • 1.OCP (open-closed Principle, open-closed principle)

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

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

Based on the OCP, with the object-oriented polymorphism (polymorphic), more flexibility in handling change embrace changes, the OCP can be implemented by: (1) abstraction and inheritance, (2) interface-oriented programming.

    • The 2.DIP content is:

High-level modules should not be dependent on low-layer modules. Both should be dependent on abstraction.

Abstraction should not depend on detail. The details should depend on abstraction.

Through the interface or abstract class, dip in the application through the method of dependency injection decoupling, reuse of low-level modules, reuse implementation, release dependency.

    • Let the system support the byte class and add the test code in the MyDoc class to indicate that it was added correctly:
Server Classesabstract class Data {abstract public void displayvalue ();}    Class Integer extends Data {int value;    Integer () {value=100;    } public void Displayvalue () {System.out.println (value);    }}class byte extends Data {byte value;    Byte () {value=3;    } public void Displayvalue () {System.out.println (value); }}//Pattern Classesabstract class Factory {abstract public Data createdataobject ();}    Class Intfactory extends Factory {public Data createdataobject () {return new Integer ();    }}class Bytefactory extends Factory {public Data createdataobject () {return new Byte ();    }}//client classesclass Document {Data pd; Document (Factory pf) {PD = PF.    CreateDataObject (); } public void Displaydata () {PD.    Displayvalue ();    }}//test Classpublic class MyDoc {static Document d,e;        public static void Main (string[] args) {d = new Document (new Intfactory ()); E = new Document (New Bytefactory ());        D.displaydata ();    E.displaydata (); }}
    • Part of the code:

    • Test results:

Object-oriented Programming-4
    • Task: Developing a plural class complex in TDD, with the following requirements:
// 定义属性并生成getter,setterdouble RealPart;double ImagePart;// 定义构造函数public Complex()public Complex(double R,double I)//Override Objectpublic boolean equals(Object obj)public String toString()// 定义公有方法:加减乘除Complex ComplexAdd(Complex a)Complex ComplexSub(Complex a)Complex ComplexMulti(Complex a)Complex ComplexDiv(Complex a)
    • Product Code:
Import Java.lang.integer;import Java.util.objects;public class Complex {double Realpart;    Double Imagepart;    Public double Getrealpart () {return realpart;    } public double Getimagepart () {return imagepart;        } public Complex () {realpart = 0;    Imagepart = 1;        } public Complex (double r,double I) {realpart = R;    Imagepart = I;        } public boolean equals (Object obj) {if (this = = obj) {return true; } if (! (        obj instanceof Complex)) {return false;        } Complex Complex = (Complex) obj; if (complex. Realpart! = ((Complex) obj).        Realpart) {return false; } if (complex. Imagepart! = ((Complex) obj).        Imagepart) {return false;    } return true;        The public string toString () {string s = new string ();        if (Imagepart > 0) {s = Getrealpart () + "+" + getimagepart () + "I";  } if (Imagepart = = 0) {          s = Getrealpart () + "";        } if (Imagepart < 0) {s = Getrealpart () + "" + getimagepart () + "I";        } if (Realpart = = 0) {s = Getimagepart () + "I";    } return s;    } Complex Complexadd (Complex a) {return new Complex (Realpart + A.realpart,imagepart + a.imagepart);    } Complex complexsub (Complex a) {return new Complex (Realpart-a.realpart,imagepart-a.imagepart); } Complex Complexmulti (Complex a) {return new Complex (realpart*a.realpart-imagepart*a.imagepart,realpart*a.image    Part + Imagepart*a.realpart); } Complex Complexdiv (Complex a) {return new Complex ((Realpart * a.imagepart + imagepart * a.realpart)/(A.imag Epart * A.imagepart + a.realpart * a.realpart), (Imagepart * a.imagepart + realpart * a.realpart)/(A.realpart * a.RealPa)    RT + A.realpart * a.realpart)); }}
    • Test pass:

Object-Oriented Programming-5
    • Knowledge Points:

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 how to 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.

    • Requirements:

Use WHITESTARUML to model the code in experiment two, send class diagram, plus number watermark. At least two classes in a class diagram.

    • Since I have not mastered the UML usage, I have not modeled the code of Experiment two, and finally chose the programming problem in the book to model.

Experimental experience

This experiment for me is more difficult, not only the code, as well as download and installation software, although the teacher's tutorial is very complete, but I will encounter many problems. Thanks to my teammates for their help and cheer!

20165306 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.