Detailed Detailed Details of Java Applications with UML Class Diagraph (2)

zhaozj2021-02-16  173

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 Height; i) {Grid.setCharat (Drawcharacter, Leftcol); Grid.setCharat (Drawcharacter, Rightcol); Leftcol = Leftco L.ADD (0,1); rightcol = rightcol.add (0,1);}} public void describe () {system.out.print ("cgbox" string.valueof (Drawcharacter) "); system .out.println (location.toString () " lr.tostring ());}} 8. The CGText class CGText class is the third subclass in cgobject.

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 is the relationship between the CGObject class, the CGPoint class, the CGBox class, the CGText class, and the POINT class. Note that the CGObject class is an abstraction class whose name is expressed. Nine, the KeyboardInput class KEYBOARDINPUT class extends the Java API's DataInputStream class to provide a series of common simple methods for obtaining user keyboard input. Which is designed to FIG class: code: import java.lang.System; import java.io.DataInputStream; import java.io.InputStream; import java.io.IOException; //KeyboardInput.java public class KeyboardInput extends DataInputStream {public KeyboardInput (InputStream Instream) {Super (Instream);} PUBLIC Char getchar () THROWS IOEXCEPTION {String line = readline (); if (line.Length () == 0) Return '' '; return line.charat (0);} public String getText () throws IOException {String line = readLine (); return line;} public int getInt () throws IOException {String line = readLine (); Integer i = new Integer (line); return i.intValue (); } Public point getPoint () throws ot.print ("x-coordinate:"); system.out.flush (); int x = getint (); system.out.print ("y-coordinate:" ); System.out.flush (); int y = getint (); Return New Point (x, y);}} 10, the CDRAWAPP class main program is constructed by the CDRAWAPP class.

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 =

转载请注明原文地址:https://www.9cbs.com/read-7957.html

New Post(0)