grid.setCharAt(text.charAt(i),p);
p=p.add(1,0);
}
}
publicvoiddescribe(){
System.out.println("CGText"+location.toString()+""+text);
}
}>
以下是CGObject类、CGPoint类、CGBox类、CGText类及Point类之间的相互关系。
注意CGObject类是抽象类,其类名用斜体表示。
九、KeyboardInput类
KeyboardInput类扩展了JavaAPI的DataInputStream类,用来提供获取用户键盘输入的一系列常用的简单方法。
其类图设计为:
代码为:
importjava.lang.System;
importjava.io.DataInputStream;
importjava.io.InputStream;
importjava.io.IOException;
//KeyboardInput.java
publicclassKeyboardInputextendsDataInputStream{
publicKeyboardInput(InputStreaminStream){
super(inStream);
}
publicchargetChar()throwsIOException{
Stringline=readLine();
if(line.length()==0)return'';
returnline.charAt(0);
}
publicStringgetText()throwsIOException{
Stringline=readLine();
returnline;
}
publicintgetInt()throwsIOException{
Stringline=readLine();
Integeri=newInteger(line);
returni.intValue();
}
publicPointgetPoint()throwsIOException{
System.out.print("x-coordinate:
");
System.out.flush();
intx=getInt();
System.out.print("y-coordinate:
");
System.out.flush();
inty=getInt();
returnnewPoint(x,y);
}
}
十、CDrawApp类
主程序由CDrawApp类所构成。
它包含main()方法,main()方法建立类CDraw的对象,然后调用该对象的run()方法。
其中CDraw类属于内部类,当然你也可以将它单独作为一个类文件编辑、编译,其效果是一样的。
其中类与内部类之间的关系,用关联关系来表达,外部类用一个十字交叉圆圈表示,箭头指向内部类。
如下图所示:
其代码实现为:
importjava.io.DataInputStream;
importjava.io.IOException;
//CDrawApp.java
classCDrawApp{
publicstaticvoidmain(Stringargs[])throwsIOException{
CDrawprogram=newCDraw();
program.run();
}
}
classCDraw{
//Variabledeclarations
staticKeyboardInputkbd=newKeyboardInput(System.in);
BorderedPrintCGridgrid;
//Methoddeclarations
CDraw(){
grid=newBorderedPrintCGrid();
}
voidrun()throwsIOException{
booleanfinished=false;
do{
charcommand=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);
}
chargetCommand()throwsIOException{
System.out.println("CDrawAppP-AddaPointU-UndoLastAdd");
System.out.print("MainMenuB-AddaBoxC-ClearGrid");
System.out.println("X-ExitCDrawApp");
System.out.print("T-AddTextS-ShowGrid");
System.out.print("Entercommand:
");
System.out.flush();
returnCharacter.toUpperCase(kbd.getChar());
}
voidaddPoint()throwsIOException{
System.out.println("AddPointMenu");
System.out.println("Location:
");
Pointp=kbd.getPoint();
System.out.print("Character:
");
System.out.flush();
charch=kbd.getChar();
if(ch=='')ch='+';
CGPointcgp=newCGPoint(p,ch);
cgp.addToGrid(grid);
}
voidaddBox()throwsIOException{
System.out.println("AddBoxMenu");
System.out.println("UpperLeftCorner:
");
Pointul=kbd.getPoint();
System.out.println("LowerRightCorner:
");
Pointlr=kbd.getPoint();
System.out.print("Character:
");
System.out.flush();
charch=kbd.getChar();
if(ch=='')ch='#';
CGBoxbox=newCGBox(ul,lr,ch);
box.addToGrid(grid);
}
voidaddText()throwsIOException{
System.out.println("AddTextMenu");
System.out.println("Location:
");
Pointp=kbd.getPoint();
System.out.print("Text:
");
System.out.flush();
Stringtext=kbd.getText();
CGTextcgt=newCGText(p,text);
cgt.addToGrid(grid);
}
}
主程序CDrawApp类与相应类之间的关系为:
按照本文次序分别编译以上的10个大类,然后运行主程序CdrawApp即可。
在程序运行时请注意:
当选择增加点、框或者文本串后,选择ShowGrid才能出现网格,并显示结果。
本文通过一个具体的程序开发过程,详细说明了如何使用UML类图来设计Java应用程序,使得程序开发可视化,文档标准化,便于相互协作与管理,是Java应用程序开发的方向。
利用UML类图设计Java应用程序详解
(二)
在第一部分中,我们实现了5个类。
在本部分中,我们接着说明如何利用UML类图来设计余下的各个类。
为减少篇幅,本部分着重讲解UML类图及应用,对Java实现代码不再详细描述。
下载本文全部代码
六、CGPoint类
CGPoint类说明了如何利用非抽象类扩展抽象类。
CGPoint类是CGObject的子类,CGPoint类扩展了CGObject类,CGPoint类没有再它所继承的变量中增加变量,它所声明的方法只有构造函数和要求它实现的抽象方法。
其类图如下:
Java实现代码为:
//CGPoint.java
publicclassCGPointextendsCGObject{
//Methoddeclarations
publicCGPoint(intx,inty,charch){
location=newPoint(x,y);
drawCharacter=ch;
}
publicCGPoint(intx,inty){
this(x,y,'+');
}
publicCGPoint(Pointp){
this(p.getX(),p.getY(),'+');
}
publicCGPoint(Pointp,charch){
this(p.getX(),p.getY(),ch);
}
publicvoiddisplay(PrintCGridgrid){
grid.setCharAt(drawCharacter,location);
}
publicvoiddescribe(){
System.out.print("CGPoint"+String.valueOf(drawCharacter)+"");
System.out.println(location.toString());
}
}
七、CGBox类
CGBox类也扩展了CGObject类。
CGBox类提供了在网格上显示矩形的附加变量。
CGBox类的类图如下:
相应的代码为:
//CGBox.java
publicclassCGBoxextendsCGObject{
//Variabledeclarations
protectedPointlr;//Lowerrightcornerofabox
//Methoddeclarations
publicCGBox(PointulCorner,PointlrCorner,charch){
location=ulCorner;
lr=lrCorner;
drawCharacter=ch;
}
publicCGBox(PointulCorner,PointlrCorner){
this(ulCorner,lrCorner,'#');
}
publicvoiddisplay(PrintCGridgrid){
intwidth=lr.getX()-location.getX()+1;
intheight=lr.getY()-location.getY()+1;
PointtopRow=newPoint(location);
PointbottomRow=newPoint(location.getX(),lr.getY());
for(inti=0;igrid.setCharAt(drawCharacter,topRow);
grid.setCharAt(drawCharacter,bottomRow);
topRow=topRow.add(1,0);
bottomRow=bottomRow.add(1,0);
}
PointleftCol=newPoint(location);
PointrightCol=newPoint(lr.getX(),location.getY());
for(inti=0;i>height;++i){
grid.setCharAt(drawCharacter,leftCol);
grid.setCharAt(drawCharacter,rightCol);
leftCol=leftCol.add(0,1);
rightCol=rightCol.add(0,1);
}
}
publicvoiddescribe(){
System.out.print("CGBox"+String.valueOf(drawCharacter)+"");
System.out.println(location.toString()+""+lr.toString());
}
}
八、CGText类
CGText类是CGObject中的第三个子类。
其类图与代码分别如下:
//CGText.java
publicclassCGTextextendsCGObject{
//Variabledeclarations
Stringtext;
//Methoddeclarations
publicCGText(Pointp,Strings){
location=p;
drawCharacter='';
text=s;
}
publicvoiddisplay(PrintCGridgrid){
Pointp=newPoint(location);
for(inti=0;igrid.setCharAt(text.charAt(i),p);
p=p.add(1,0);
}
}
publicvoiddescribe(){
System.out.println("CGText"+location.toString()+""+text);
}
}
以下是CGObject类、CGPoint类、CGBox类、CGText类及Point类之间的相互关系。
注意CGObject类是抽象类,其类名用斜体表示。
九、KeyboardInput类
KeyboardInput类扩展了JavaAPI的DataInputStream类,用来提供获取用户键盘输入的一系列常用的简单方法。
其类图设计为:
代码为:
importjava.lang.System;
importjava.io.DataInputStream;
importjava.io.InputStream;
importjava.io.IOException;
//KeyboardInput.java
publicclassKeyboardInputextendsDataInputStream{
publicKeyboardInput(InputStreaminStream){
super(inStream);
}
publicchargetChar()throwsIOException{
Stringline=readLine();
if(line.length()==0)return'';
returnline.charAt(0);
}
publicStringgetText()throwsIOException{
Stringline=readLine();
returnline;
}
publicintgetInt()throwsIOException{
Stringline=readLine();
Integeri=newInteger(line);
returni.intValue();
}
publicPointgetPoint()throwsIOException{
System.out.print("x-coordinate:
");
System.out.flush();
intx=getInt();
System.out.print("y-coordinate:
");
System.out.flush();
inty=getInt();
returnnewPoint(x,y);
}
}
十、CDra