In the first part, we have achieved five classes. In this section, we will then explain how to use the UML class diagram to design the remaining classes. In order to reduce the space, this part focuses on the UML class diagram and the application, and the code is implemented for Java no longer detail. 6. CGPoint CGPoint classes describe how to use non-abstract classes to extend abstract classes. The CGPoint class is a subclass of CGObject. The CGPoint class extends the CGObject class. The CGPoint class does not increase the variable in the variable it inherited, and the method it declares only the construct function and the abstract method that requires it. Its class diagram is as follows: Java implementation code is: // cgpoint.java public class cgpoint extends cgobject {// method Declarations public cgpoint (int x, int y, char ch) {location = new point (x, y); Drawcharacter = Ch;} public cgpoint (int x, int y) {this (x, y, ' ');} public cgpoint (POINT P) {this (p.GETX (), p.Gety (), ' ') Public cgpoint (POINT P, CHAR CH) {this (p.GETX (), p.Gety (), ch);} public void display (printcgrid grid) {grid.setcharat (drawcharacter, location); public void Describe () {system.out.print ("CGPoint" String.Valueof (Drawcharacter) ""); system.tostring ());}} 7. CGBOX class CGBox class also expands CGObject class. The CGBox class provides additional variables that display rectangles on the grid.
FIG CGBox class categories as follows: the corresponding code: // CGBox.java public class CGBox extends CGObject {// Variable declarations protected Point lr; // Lower right corner of a box // Method declarations public CGBox (Point ulCorner, Point lrCorner, char ch) {location = ulCorner; lr = lrCorner; drawCharacter = ch;} public CGBox (Point ulCorner, Point lrCorner) {this (ulCorner, lrCorner, '#');} public void display (PrintCGrid grid) {int Width = lr.getx () - location.getx () 1; int Height = lr.gety () - location.gety () 1; point Toprow = new point (location); Point Bottomrow = New Point (location. Getx (), lr.gety ()); for (int i = 0; i
FIG its class and the code are as follows: // CGText.java public class CGText extends CGObject {// Variable declarations String text; // Method declarations public CGText (Point p, String s) {location = p; drawCharacter = ''; text = S; PUBLIC VOID DISPLAY (PRINTCGRID GRID) {POINT P = New Point (location); for (int i = 0; i
It contains the main () method, main () method to create a class CDRAW object, and then call the RUN () method of the object. Where the CDRAW class belongs to the internal class, of course, you can also edit it separately as a class file, compile, its effect is the same. The relationship between the class and the internal class is expressed in association, and the external class is represented by a cross-crossed circle, and the arrow points to the internal class.
As shown in the figure below: The code is implemented as: import java.lang.system; import java.io.DataInputStream; import java.io ioException; //cdrawapp.java class cdrawapp {public static void main (string args []) THROWS IOException {CDraw program = new CDraw (); program.run ();}} class CDraw {// Variable declarations static KeyboardInput kbd = new KeyboardInput (System.in); BorderedPrintCGrid grid; // Method declarations CDraw () {grid = New borderedprintcgrid ();} void run () throws ooException {boolean finic = false; do {char command = getcommand (); switch (command) {copy 'p': addpoint (); system.out.println (); break Case 'b': addbox (); system.out.println (); break; case 't': addText (); system.out.println (); break; case 'u': grid.deletelastObject (); System.out.println (); Break; Case 'C': grid.cleargrid (); system.out.println (); breaf; case 's': grid.show (); break; casse 'x': finished = True; default: system.out.println ();}} while (! finished);} char getcommand () TH Rows IOException {system.out.println ("CDRAWAPP P - Add A Point U - undo Last Add"); System.out.Print ("Main Menu B - Add A Box C - Clear Grid"; System.out.Println ("X - EXIT CDRAWAPP"; System.out.Print ("T - Add Text S - Show Grid"; System.out.print ("Enter Command:"); System.out.Flush (); Return Character .touppercase (Kbd.getChar ());} void addpoint () throws ot.Println ("Add Point Menu"); System.out.Println ("Location:"); POINT P = Kbd.getPoint ); System.out.print ("Character:"); system.out.flush (); char ch = kbd.getchar (); if (ch =