Detailed description of Java application design using UML class diagrams (2)

Source: Internet
Author: User
Tags gety
In the first part, we implement five classes. This section describes how to use UML class diagrams to design the remaining classes. To reduce the length, this section focuses on UML class diagrams and applications, and does not describe the Java implementation code in detail.

Vi. cgpoint class
The cgpoint class illustrates how to use a non-abstract class to extend the abstract class. The cgpoint class is a subclass of cgobject. The cgpoint class extends the cgobject class, And the cgpoint class does not add any variables it inherits, the method it declares only the constructor and the abstract method that requires it to implement. The class diagram is as follows:
  
Java implementation code:
// 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. Out. println (location. tostring ());
}
}
VII. cgbox type
The cgbox class also extends the cgobject class. The cgbox class provides additional variables for displaying rectangles on a grid. The cgbox class diagram is as follows:
  
The Code is as follows:
// 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 <width; ++ I ){
Grid. setcharat (drawcharacter, toprow );
Grid. setcharat (drawcharacter, bottomrow );
Toprow = toprow. Add (1, 0 );
Bottomrow = bottomrow. Add (1, 0 );
}
Point leftcol = new point (location );
Point rightcol = new point (LR. getx (), location. Gety ());
For (INT I = 0; I> height; ++ I ){
Grid. setcharat (drawcharacter, leftcol );
Grid. setcharat (drawcharacter, rightcol );
Leftcol = leftcol. 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 ());
}
}
VIII. cgtext
The cgtext class is the third subclass of cgobject. The class diagram and 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 <text. Length (); ++ I ){
Grid. setcharat (text. charat (I), P );
P = P. Add (1, 0 );
}
}
Public void describe (){
System. Out. println ("cgtext" + location. tostring () + "" + text );
}
}>
The following are the relationships among cgobject, cgpoint, cgbox, cgtext, and point classes. Note that the cgobject class is an abstract class and its class name is in italic.
  
9. keyboardinput class
The keyboardinput class extends the Java API's datainputstream class to provide a series of common simple methods for getting user keyboard input. The class diagram is designed as follows:
  
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 ioexception {
System. Out. 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. cdrawapp
The main program consists of the cdrawapp class. It contains the main () method, and the main () method establishes the cdraw class object, and then calls the run () method of the object. The cdraw class is an internal class. Of course, you can edit and compile it as a class file, and the effect is the same.
The relationship between the class and the internal class is expressed by the association relationship. The external class is represented by a cross circle and the arrow points to the internal class. As shown in:
  
The code is implemented as follows:
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 ioexception {
Boolean finished = false;
Do {
Char command = getcommand ();
Switch (command ){
Case '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 ();
Break;
Case's ':
Grid. Show ();
Break;
Case 'X ':
Finished = true;
Default:
System. Out. println ();
}
} While (! Finished );
}
Char getcommand () throws 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 ioexception {
System. Out. 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 = '') CH = '+ ';
Cgpoint CGP = new cgpoint (p, CH );
CGP. addtogrid (GRID );
}
Void addbox () throws ioexception {
System. Out. println ("add box menu ");
System. Out. println ("upper left corner :");
Point ul = KBD. getpoint ();
System. Out. println ("lower right corner :");
Point LR = KBD. getpoint ();
System. Out. Print ("character :");
System. Out. Flush ();
Char CH = KBD. getchar ();
If (CH = '') CH = '#';
Cgbox box = new cgbox (UL, LR, CH );
Box. addtogrid (GRID );
}
Void addtext () throws ioexception {
System. Out. println ("Add text menu ");
System. Out. println ("Location :");
Point P = KBD. getpoint ();
System. Out. Print ("text :");
System. Out. Flush ();
String text = KBD. gettext ();
Cgtext CGT = new cgtext (p, text );
CGT. addtogrid (GRID );
}
}
The relationship between the main program cdrawapp class and the corresponding class is:
  
Compile the above 10 categories in the order of this article, and then run the main program cdrawapp. Note when running the program: After adding a vertex, box, or text string, select Show grid to display the grid and the result.
Through a specific program development process, this article describes in detail how to use UML class diagrams to design Java applications so that program development can be visualized and documents can be standardized for mutual collaboration and management, is the direction of Java application development.

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.