UML has become a standard graphical tool for object-oriented design, in various figures defined in UML, this article only involves class diagrams. The Java application consists of many classes, and the design and implementation of class diagrams is the core of the Java implementation of object-oriented applications. In this paper, the design and implementation of a specific application will be described in detail, using the UML class diagram design Java application, making the development process standardize, visualize, and the code programming is simplified. In the class diagram, the class is described as a three-layer box. The top layer is classified, generally expressed in bold fonts. If the class is abstract, its name is represented by a bevel; if the class is an interface, the << Interface> is labeled above the class name. The intermediate layer contains the attributes (or variables) of the class, and the bottom layer contains a class method. Similar to the class name, if the method is abstract, its name is also represented by a bevel. The application CDRAWAPP application we want to design is on the character-based grid, box, and text strings, which involves many concepts and application methods of Java-oriented objects, very systematic, comprehensive, after you study carefully, Fortune can quickly grasp the UML class map and apply it to the actual Java application development process. In order to reduce the length of the code, let the program easy to understand, here the Java console window display program run results. The program consists of 10 major categories, which are described below. First, the first class defined in the CDrawApp program is the Point class, which is used to identify a little on the grid via the X and Y coordinates. Its class diagram is: In this class, there are 2 member variables x and y, class diagrams, "-" indicates that variables or methods are private, " " indicates public, "#" represents protected. This class defines three different constructor, which is an example of overload. Then, seven access methods are designed. The getx () and get () methods returns a little x and y coordinates, respectively. SetX () and setY () methods set the value of these coordinates based on the values of the parameters XValue and YValue. Two add () methods create a new POINT object by adding a value by the coordinates of the visited point. New examples of the New operator build class. It is followed by the constructor of the initialization new generation instance. The toString () method returns an object of class String that describes a point with an ordered pair.
Based on the design class, its Java implementation code is: // point.java public class point {// variable declarations private int x; private int y; // method () {x = 0; y = 0; PUBLIC POINT (INT XVALUE, INT YVALUE) {x = xvalue; y = yvalue;} public point (POINT P) {x = p.GETX (); y = p.gety ();} Return X;} public int game () {Return y;} public void setX (int xvalue) {x = xValue;} public void sety {y = yvalue;} public point add (point p) {Return New Point (x p.getx (), y get ());} public point add (int i, int j) {return new point (x i, y j);} public string toString () {Return NEW STRING (" String.Valueof (x)", " String.Valueof (Y) ") ")") ");}} 2, CGRID class CGRID class is used to define the character grid of the specified size. It provides the basic method set to be expanded by adding other classes in these methods. The class diagram of this class is: CGRID class declaration 3 variables: width, depth, and grid [] []. Width and depth variables are used to specify horizontal and vertical sizes of Grid [] [], and Grid [] [] is an array of characters to save grid characters. Variables in CGRID are declared as protected, which specifies that they can only be accessed in any subclass that declares their packets and CGRID. The CGRID class has only one single constructor, which sets the value of Width and depth, assigns the Grid [] [] array, and then calls BlankGrid () assigns a space for Grid [] []. CGRID has four access methods. The BlankGrid () method is just simply invoke FillGrid (). The FillGrid () method sets each element of Grid [] [] to a CH parameter. The getCharFrom () method is used to find the character of a given position in the grid. SetCharat () is used to set a point in the grid to a specific character. Use the Point class in getcharfrom () and setcharat () methods to define their parameters, which is an example of association between classes and classes. We will discuss it later.
According to the above class diagram, CGrid original class code: // CGrid.java public class CGrid {// Variable declarations protected int width; protected int depth; protected char grid [] []; // Method declarations public CGrid (int widthValue , int depthValue) {width = widthValue; depth = depthValue; grid = new char [depth] [width]; blankGrid ();} public void blankGrid () {fillGrid ( '');} public void fillGrid (char ch) { For (int J = 0; j
CGObject class code: // CGObject.java public abstract class CGObject {// Variable declarations public Point location; public char drawCharacter; // Method declarations public void addToGrid (PrintCGrid grid) {grid.addCGObject (this);} public abstract Void Display (Printcgrid Grid); Public Abstract Void Describe ();} four, the PrintCGrid class PrintCGrid class is a subclass of the CGRID class, which defines an additional variable and method that allows an object to be added to the grid. It also provides a method of displaying a grid. The class diagram of the PrintcGrid class is as shown below: The relationship between the PrintCGrid class and the CGRID class is the relationship between subclasses and parent. In the class diagram, use the solid line to point to the parent class from the subclock from the subclass with the hollow arrow. Since the PrintCGRID class is a subclass of the CGRID class, the extends clause must be added in the PrintCGrid class declaration. This means that all variables and methods defined in CGRID can be obtained in printcgrid. This is the powerful function of inheritance and one of the main features of object-oriented languages. Printcgrid uses CGRID as a foundation that adds variables and methods displayed by other grids on this basis.
PrintCGrid original class code: import java.lang.System; // PrintCGrid.java public class PrintCGrid extends CGrid {// Variable declarations protected CGObject displayList []; protected static final int maxObjects = 100; protected int numObjects; // Method declarations public PrintCGrid (int x, int y) {super (x, y); numObjects = 0; displayList = new CGObject [maxObjects];} public void addCGObject (CGObject obj) {if (numObjects
The maxObjects variable declares to static and final. Use the Static modifier declared variables, they are used as all objects of a class instance, and they are not copied by each instance, and the static variables become class variables. Variables that are not declared are static are example variables, and each object as an instance of a class is copied. Final modifiers are used to identify variables to register. The variables declared with the final modifier must be initialized when declared, and the value other than before declare. The maxObjects constant is initialized to 100, indicating the maximum number of objects that can be added to DisplayList []. NumObjects variables are used to statistically add objects in DisplayList [] in the grid. Printcgrid has a constructor. This constructor has two parameters: x and y, which represent the horizontal and vertical dimension of the grid. Constructor calls the super () method, and transmits the two variables to the past. The super () method is an example of constructing a function call statement. It uses x and y as the constructor of the parent class (ie, the CGRID class) of the PrintCGRID. The constructor of the CGRID initializes its width and depth variables, assigns the Grid [] [] array, and assigns the array element with spaces. After the constructor of the CGRID is running, the constructor of the PrintCGRID continues to set the NumObjects to 0 and assign the DisplayList [] array. PrintCGRID provides 10 access methods. The AddCgObject () method adds an object to the DisplayList [] array. The deletecgobject () method deletes an object located in the specified index position. All subsequent objects move forward to fill the vacancies left by the deleted object. The deletelasticObject () method deletes the last object by simply putting NumObjects. GetNumObjects method Returns the number of objects in DisplayList []. GetObject method Returns the object of the location specified in DisplayList []. The ClearGrid () method clears all objects by setting NumObjects to 0. DrawGrid () methods use methods that inherited from the CGRID class, empty the grid, then call the display () method of each object in DisplayList []. DisplayGrid [] method Displays each line grid in the console window. It is an example of inheritance. Grid [] [] The ancestor is defined in the CGRID class, inherited by the PrintCGrid. It is updated by a DrawGrid () method of all subclasses of the CGObject class. The PrintGrid () class uses it to print characters in the console window. Valueof () method is used in the DisplayGrid () method, which is a static method of the String class. It converts the character array into String objects. The static method is similar to static variables, which are used as a whole, rather than use as individual objects of class instances. Due to object-oriented, the static method can only access static variables. All static methods are foregoing and cannot be covered. The DisplayRow () method displays a line of mesh on the console window, and the show () method combines the DrawGrid () and the DisplayGRID () method into a method. 5. The BorderPrintcgrid class BorderPrintcGrid class is a subclass of the PrintCGrid class, which extends to the CGRID class. It adds variables and methods that generate the boundaries of the class PrintCGRID object. The class diagram is as follows: The BorderPrintcGrid class has four private variables: Useborder, Bordercharacter, Horizedge, and Vertge. Useborder is a Boolean type variable that determines whether the boundary should be displayed. BorderChacter is a character used to display the boundary. Horizedge and Vertge are used to display the horizontal and vertical boundaries of the boundary. There are two BorderPrintcGrid classes.
The first constructor has no parameters. It calls the constructor of the PrintCGRID class constructs a grid of 75-character wide 20 rows, and its boundary characters are *. SetBorderDefaults () method is used to initialize the variable of the BorderedPrintcGrid class. The second constructor is similar to the first constructor, but it provides the functionality that directly specifies the grid size and boundary character. The BorderPrintcGrid class provides four access methods. SetBorderDefaults () method uses the enableborder () method and setBorderChacter () method to initialize the variable of the BORDEREDPRINTCGRID class. EnableBorder () Method Sets UseBorder to True or False. SetBorderCharacter () Method Sets the BorderCharacter, HorizeDge, and Vertge variables used by the DisplayGrid () method. The BORDEREDPRINTCGRID class is a subclass of PrintCGRID, and its class diagram is represented as: the DisplayGrid () method overwrites the DisplayGrid () method of the PrintCGrid class. By redefining the method to meet your needs. The super statement will call PrintCgrid.DisplayGrid (). Implemented class code: // BorderedPrintCGrid.java public class BorderedPrintCGrid extends PrintCGrid {// Variable declarations private boolean useBorder; private char borderCharacter; private String horizEdge; private String vertEdge; // Method declarations public BorderedPrintCGrid () {super (75 20); setBorderDefaults ('*');} public borderedprintcgrid (int x, int y, char ch) {Super (x, y); setBorderDefaults (ch);} private void setborderdefaults (char ch) {useborder = true; setBorderCharacter (ch);} public void enableBorder (boolean toggle) {useBorder = toggle;} public void setBorderCharacter (char ch) {borderCharacter = ch; char border [] = new char [width 2]; for (int i = 0 i