网友您好, 请在下方输入框内输入要搜索的题目:

题目内容 (请给出正确答案)

阅读以下说明和JAVA 2代码,将应填入(n)处的字句写在对应栏内。

[说明]

以下程序为类类型的变量应用实例,通过异常处理检验了类CCircle的变量的合法性,即参数半径应为非负值。仔细阅读代码和相关注释,将程序补充完整。

[JAVA代码]

//定义自己的异常类

class CCircleException extends Exception

{

}

// 定义类 CCircle

class CCircle

{

private double radius;

public void setRadius ( double r ) (1)

{

if ( r<0 ) {

(2)

}

else

(3)

}

Public void show ( ) {

System. out. println ( "area="+3.14*radius*radius );

}

}

public class ciusample

{

public static void main ( String args[] )

{

CCircle cir=new CCircle( );

(4) {

cir. setRadius ( -2.0 )

}

(5)

{

System. out. println ( e+" throwed" ) ;

}

cir. show( ) ;

}

}


参考答案

更多 “ 阅读以下说明和JAVA 2代码,将应填入(n)处的字句写在对应栏内。[说明]以下程序为类类型的变量应用实例,通过异常处理检验了类CCircle的变量的合法性,即参数半径应为非负值。仔细阅读代码和相关注释,将程序补充完整。[JAVA代码]//定义自己的异常类class CCircleException extends Exception{}// 定义类 CCircleclass CCircle{private double radius;public void setRadius ( double r ) (1){if ( r<0 ) {(2)}else(3)}Public void show ( ) {System. out. println ( "area="+3.14*radius*radius );}}public class ciusample{public static void main ( String args[] ){CCircle cir=new CCircle( );(4) {cir. setRadius ( -2.0 )}(5){System. out. println ( e+" throwed" ) ;}cir. show( ) ;}} ” 相关考题
考题 阅读以下说明和Java程序,将应填入(n)处的字句写在对应栏内[说明]以下程序的功能时三角形、矩形和正方形的面积输出。程序由5个类组成:areatest是主类,类Triangle,Rectangle和Square分别表示三角形、矩形和正方形,抽象类Figure提供了一个计算面积的抽象方法。[Java程序]public class areatest {public static viod main(string args[]){Figure[]Figures={New triangle(2,3,3),new rectangle(5,8),new square(5)};for(int i=0; i<Figures.length;i++){system.out.println(Figures+"area="+Figures.getarea());}}}public abstract class figure {public abstract double getarea();}public class rectangle extends (1) {double height;double width;public rectangle (double height,double width){this.height=height;this.width=width;}public string tostring(){return"rectangle:height="+height+",width="+width+":";}public double getarea(){return (2)}}public class square exends (3){public square(double width){(4);}public string tostring(){return"square:width="+width":";}}public class triangle entends (5){double la;double lb;double lc;public triangle(double la,double lb,double lc){this.la=la;this.lb=lb;this.lc=lc;}public string tostring()(return"triangle:sides="+la+","+lb+","+lc+":";}public double get area(){double s=(la+lb+lc)/2.0;return math.sqrt(s*(s-la)*(s-lb)*(s-lc));}}

考题 阅读以下说明和Java源程序,将应填入(n)处的字句写在对应栏内。【说明】以下程序能够计算三角形、矩形和正方形的周长并输出。程序由5个类组成:AreaTest是主类,类Triangle、Rectangle和Square分别表示三角形、矩形和正方形,抽象类Figure提供了一个计算周长的抽象方法。【程序】public class girthTest{public static void main (String args[]){Figure[]figures={new Triangle (2,3,3),new Rectangle(5,8),new Square(5)};for(int i=0;i<figures.length;i++){System.out.println(figures[i]+"girth="+figures[i].getGirth());}}}public abstract class Figure{public abstract double getGirth();}public class Rectangle extends (1) {double height;double width;public Rectangle(double height,double width){this.height=height;this.width=width;}public String toString(){return "Rectangle:height="+height+",width="+width+":";}public double getGirth(){return (2);}}public class Square extends (3) {public Square(double width){(4);}public Stdng toString(){return "Square:width='+width+":";}}public class Triangle extends (5) {double la;double lb;double lc;public Triangle(double la,double lb,double lc){this.la=la;this.lb=lb;this.lc=lc;}public String toString(){return "Triangle:sides=" +la+"," +lb+"," +lc+":";}public double getGirth(){return la+lab+lc;}}

考题 阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。【说明】下面的Java程序演示了程序竞争资源(Mutex的实例对象)而引起程序死锁的一种例子。【Java程序】import java.applet.*;import java.awt.*;//此处声明一个互斥类class Mutex { }class A extends (1){private Mutex first,second;public A(Mutex f,Mutex s){first = f;second = s;}public void run(){//锁定first变量(2) (first){try{ //本线程挂起,等待重新调度Thread.sleep(1); //注意此处(1)不是小题序号}catch(InterruptedException e){}System. out. println("threadA got first mutex");(2) (second) //锁定second变量{ //do somethingSystem. out. println("threadA got second mutex");} //释放second变量} //释放first变量}}class B extends (1){private Mutex first,second;public B(Mutex f,Mutex s){(3) ;second = s;}public void run(){(2) (second) //锁定second变量{//do somethingtry{Thread.sleep(((int)(3*Math.random()))*1000);//本线程挂起,等待重新调度}catch(InterruptedException e){}System.out.println("threadB got second mutex");(2) (first) //锁定first变量{//do somethingSystem.out.println("threadB got first mutex");} //释放first变量} //释放second变量}}public class DeadlockExample{public static void main(String arg[]){Mutex mutexX = new Mutex();Mutex mutexY = new Mutex();AthreadA = new A(mutexX,mutexY);B threadB = new B (4);threadA.(5);threadB.start();}}

考题 阅读以下说明和Java代码。[说明]已知类Stock和类JavaMain都定义在JavaMain.java文件中,类Stock的定义中有四处错误,分别在代码的第01、02、06、07行。请修改错误并给出修改后该行的完整代码,并写出改正错误后程序运行的输出结果。[Java代码]01 public class Stock{02 static {03 shares=0;04 share_val=0.0;05 }06 private Stock(){getData();}07 private Stock(int n, double pr=0){08 shares=n;09 share_val=pr;10 getData();11 }12 public void getData(){13 System.out.print(shares+":"+share_val+"");14 }15 private int shares; //非静态变量16 private double share_val; //非静态变量17 };1819 public class JavaMain{20 public static void main(String args[]){21 Stock a=new Stock();22 Stock b=new Stock(1,67.5);23 //其它代码省略,且代码无输出24 }25 }

考题 阅读下列Java程序和程序说明,将应填入(n)处的字句写在对应栏内。【说明】下面的程序先构造Point类,再顺序构造Ball类。由于在类Ball中不能直接存取类Point中的xCoordinate及yCoordinate属性值,Ball中的toString方法调用Point类中的toString方法输出中心点的值。在MovingBall类的toString方法中,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值。public class Point{private double xCoordinate;private double yCoordinate;public Point 0 }public Point(ouble x, double y){xCoordinate = x;yCoordinate = y;}public String toString(){return "( + Double.toString(Coordinate)+ ","+ Double.toString(Coordinate) + ");}//other methods}public class Ball{(1); //中心点private double radius; //半径private String colour; ///颜色public Ball() { }public Ball(double xValue, double yValue, double r)// 具有中心点及半径的构造方法{center=(2);//调用类Point 中的构造方法radius = r;}public Ball(double xValue, double yValue, double r, String c)// 具有中心点、半径及颜色的构造方法{(3);//调用3个参数的构造方法colour = c;}public String toString(){return "A ball with center" + center, toString() + ", radius"+ Double.toString(radius) + ", colour" + colour;}//other methods}public class MovingBall. (4){private double speed;public MovingBall() { }public MovingBall(double xValue, double yValue, double r, String e, double s){(5);// 调用父类Ball中具有4个参数的构造方法speed = s;}public String toString( ){ return super, toString( ) + ", speed "+ Double.toString(speed); }//other methods}public class Tester{public static void main(String args[]){MovingBall mb = new MovingBall(10,20,40,"green",25);System.out.println(mb);}}

考题 阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。【说明】下面代码里,instr是一个抽象类,它被两个子类wind和rain分别实现,而brass又继承了wind类,并覆盖了play方法。【程序】(1) class instr{(2) Void play();}class wind (3) instr{void play(){System.out.println("wind play!");}}class rain extends instr {void play(){System.out.println("rain play!");}}class brass extends (4) {void play(){System.out.println("brass play!"));}}public class music{static void tuneAll(instr e[]){for(int i=0;i<e.length;i++)e[i].play();}public (5) void main(String[]args){instr orch[]=new instr[3];int i=0;orch[i++]=new wind();orch[i++]=new rain();orch[i++]=new brass();tuneAll(orch);}}

考题 试题八(共15分)阅读以下说明和Java程序代码,将应填入(n) 处的字句写在答题纸的对应栏内。[说明]在下面的 Java 程序代码中,类SalesTicket 能够完成打印票据正文的功能,类HeadDecorator 与FootDecorator 分别完成打印票据的台头和脚注的功能。已知该程序运行后的输出结果如下所示,请填补该程序代码中的空缺。这是票据的台头!这是票据正文!这是票据的脚注!------------------------这是票据的台头!这是票据的脚注![Java程序代码]public class SalesTicket {public void printTicket() {System.out.println("这是票据正文!");}}public class Decorator extends SalesTicket{SalesTicket ticket;}}public class FootDecorator extends Decorator{public FootDecorator(SalesTicket t) {(2) ;}public void printTicket() {super.printTicket();System.out.println("这是票据的脚注!");}}public class Main {public static void main(String[] args) {T = new HeadDecorator( (3) );T. (4) ;System.out.println("------------------------");T = new FootDecorator( (5) );T.printTicket();}}public Decorator(SalesTicket t){ticket = t;}public void printTicket(){if(ticket != null)ticket.printTicket();}}public class HeadDecorator extends Decorator{public HeadDecorator(SalesTicket t) {(1) ;}public void printTicket() {System.out.println("这是票据的台头!");super.printTicket();

考题 阅读以下说明和JAVA2代码,将应填入(n)处的字句写在对应栏内。【说明】以下程序实现了利用鼠标任意移动该圆形的位置,仔细阅读代码和相关注释,将程序补充完整。【代码6】import java.awt.*;import java.awt.event.*;public class CIUSAMPLE extends Frame. implements MouseMotionListener, MouseListener{static CIUSAMPLE frm=new CIUSAMPLE ();int x=70,y=60,posX=70,posY=60,dx,dy;public static void main ( String args[]){frm.setTitle ("Dragging a circle");frm.setSize ( 200,150 );(1)frm.addMouseMotionListener ( frm );frm. setVisible ( true );}public void mousePressed ( MouseEvent e ){(2)dy=e.getY () -posY;}public void mouseDragged ( MouseEvent e ){(3)y=e.getY () -dy;if ( dx>0 dx<50 dy>0 dy<50 ) //如果指我落在正方形区域内{Graphics g=getGraphics ();(4)}}public void paint ( Graphics g ){g.setColor ( Color. pink ); // 设置绘图颜色为粉红g.fillOval ( x,y,50,50 ); //以基准点为图形的左上角绘出圆形(5)posY=y;}public void mouseMoved ( MouseEvent e ) { }public void mouseReleased ( MouseEvent e ) { }public void mouseEntered ( MouseEvent e ) { }public void mouseExited ( MouseEvent e ) { }public void mouseClicked ( MouseEvent e ) { }}

考题 阅读以下说明、Java代码和HTML文档,将应填入(n)处的字句写在答题纸的对应栏内。【说明】当用户启动html浏览器并首次打开下面的HTML文档时,Java Applet小程序在显示面板上显示字符串“Welcome!”;当html页面被其他窗口遮挡后再次显示时,小程序在显示面板上显示“Welcome back!”。【Java代码】import javA.awt.*;import javA.applet;public class HelloApplet extends (1) {public void paim((2)){g.drawString(message,10,20);message="welcome back!”;}public void (3) (){message="Welcome!”;}private (4) message;}【HTML文档】<html><head><title>HTML Test HelloApplet Page</title></head><body>Test HelloApplet小程序<br><appletcodebase="."code="(5) "name="TestApplet"width="400"height="300"></applet></body></html>

考题 阅读以下说明和C++代码,填入(n)处。[说明]以下C++代码使用虚函数实现了同一基类shape派生出来的Class rectangle、Class triangle、Class circle实现了计算矩形、圆形面积的计算。仔细阅读以下代码,将(n)处语句补充完整。[代码5-1]include<iostream.h>define PI 3.14159class shape {//基类protected:(1);public:(2);(3);};[代码5-2]class rectangle: public shape {public:rectangle (int x2,int y2,int r2): (4) {};double area ( ) {return x*y; };};class circle: public shape {public:circle (int x3,int y3,int r3):(5){};double area ( ) {return r*r*PI; };};[代码5-3]void main ( ){rectangle r (10,20,0);circle c (0,0,30);shape (6);cout<<"长方形面积="<<s1->area ( ) <<endl;cout<<"圆形面积="<<s2->area ( ) <<endl;}[运行结果]长方形面积=200圆形面积=2827.43

考题 阅读以下函数说明和Java代码,将应填入(n)处的字句写在对应栏内。【说明】现要编写一个画矩形的程序,目前有两个画图程序:DP1和DP2,DP1用函数draw_a_line(x1,y1,x2,y2)画一条直线,DP2则用drawline(x1,x2,y1,y2)画一条直线。当实例画矩形时,确定使用DP1还是DP2。为了适应变化,包括“不同类型的形状”和“不同类型的画图程序”,将抽象部分与实现部分分离,使它们可以独立地变化。这里,“抽象部分”对应“形状”,“实现部分”对应“画图”,与一般的接口(抽象方法)与具体实现不同。这种应用称为Bridge(桥接)模式。图9-6显示了各个类间的关系。这样,系统始终只处理3个对象:Shape对象、Drawing对象、DP1或DP2对象。以下是 Java语言实现,能够正确编译通过。【Java代码】//DP1.java文件public class DP1{static public void draw_a line(double x1,double y1,double x2,double y2){//省略具体实现}}//DP2.java文件public class DP2{static public void drawline(double x1,double y1,double x2,double y2){//省略具体实现}}//Drawing.java文件(1) public class Drawing{abstract public void drawLine(double x1, double y1, double x2, double y2);}//V1Drawing.java文件public class V1Drawing extends Drawing{public void drawLine(double x1, double y1, double x2, double y2){DP1.draw_a_line(x1,y1,x2,y2);}}//V2Drawing.java文件public class V2Drawing extends Drawing{public void drawLine(double x1,double y1,double x2, double y2)(//画一条直线(2);}}//Shape.java文件abstract public class Shape{abstract public void draw();private (3) _dp;Shape(Drawing dp){_dp=dp;}protected void drawLine(double x1,double y1,double x2, double y2){(4);}}//Rectangle.java文件public class Rectangle extends Shape{private double_x1,_x2,_y1,_y2;public Rectangle(Drawing dp,double x1,double y1,double x2,double y2){(5);_x1=x1;_x2=x2;_y1=y1;_y2=y2;}public void draw(){//省略具体实现}}

考题 阅读以下函数说明和Java代码,将应填入(n)处的字句写在对应栏内。【说明】下面的程序先构造Point类,再顺序构造Ball类。由于在类Ball中不能直接存取类Point中的xCoordinate及yCoordinate属性值,Ball中的toString方法调用Point类中的toStrinS方法输出中心点的值。在MovingBsll类的toString方法中,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值。【Java代码】//Point.java文件public class Point{private double xCoordinate;private double yCoordinate;public Point(){}public Point(double x,double y){xCoordinate=x;yCoordinate=y;}public String toStrthg(){return"("+Double.toString(xCoordinate)+","+Double.toString(yCoordinate)+")";}//other methods}//Ball.java文件public class Ball{private (1);//中心点private double radius;//半径private String color;//颜色public Ball(){}public Ball(double xValue, double yValue, double r){//具有中心点及其半径的构造方法center=(2);//调用类Point中的构造方法radius=r;}public Ball(double xValue, double yValue, double r, String c){//具有中心点、半径和颜色的构造方法(3);//调用3个参数的构造方法color=c;}public String toString(){return "A ball with center"+center.toString()+",radius "+Double.toString(radius)+",color"+color;}//other methods}class MovingBall (4) {private double speed;public MovingBall(){}public MoyingBall(double xValue, double yValue, double r, String c, double s){(5);//调用父类Ball中具有4个参数的构造方法speed=s;}public String toString(){return super.toString()+",speed"+Double.toString(speed);}//other methods}public class test{public static void main(String args[]){MovingBall mb=new MovingBall(10,20,40,"green",25);System.out.println(mb);}}

考题 阅读以下说明、Java代码和HTML文档,将应填入(n)处的字句写在对应栏内。【说明】当用户启动html浏览器并首次打开下面的html文档时,Java Applet小程序在显示面板上显示字符串“欢迎您!”;当html页面被其他窗口遮挡后再次显示时,小程序在显示面板上显示“欢迎您回来!”[Java代码]import java.awt.*;import java.applet.*;public class HelloApplet extends (1) {public void paint( (2) ){g.drawString(message,10,20);message="欢迎您回来!";}public void (3) (){message="欢迎您!";}private (4) message;}[HTML文档]<html><head><title>HTML Test HelloApplet Page</title></head><body>Test HenoApplet 小程序<br><appletcodebase="."code="(5)"name="TestApplet"width="400"height="300"></applet></body></html>

考题 阅读以下说明和Java代码,回答问题1和问题2,将解答填写在对应栏内。【Java代码】class usethread implements (1) {int numusethread(int n){num=n;}public void (2) {for(int i=0;i<3;i++)System.out.println("running:"+num);System.out.println("finished:"+num);}public class multhread{public static void main(String args[]) (3) InterruptedException{Thread m1=new Thread(new usethread(1));Thread m2=new Thread(new usethread(2));m1.start();m2.start();m1.join();m2.join();}}【问题1】补充完整上面Java代码中(n)处。【问题2】写出上面Java代码运行的结果。

考题 阅读以下说明和Java源程序,将应填入(n)处的字句写在答题纸的对应栏内。说明以下程序的功能是计算三角形、矩形和正方形的面积并输出。程序由5个类组成:AreaTest是主类,类Triangle、Rectangle和Square分别表示三角形、矩形和正方形,抽象类Figure提供了一个计算面积的抽象方法。程序public class AreaTest{public static void main(String args[]){Figure[]figures={new Triangle(2,3,3),new Rectangle(5,8), new Square(5)};for(int i=0;i<figures.1ength;i++){System.out.println(figures[i]+"area="+figures[i].getArea());}}}public abstract class Figure{public abstract double SetAJea();public class Rectangle extends (1) {double height;double width;public Rectangle(double height,double width){this.height=height;this.width=width;}public String toString(){return "Rectangle:height="+height+",width="+width+":";}public double getArea() { return (2);} } public class Square extends (3) {public Square(double width) {(4);}public String toString() {return "Square:width="+width+":";} } public class Triangle extends (5). {double la;double lb;double lc;public Triangle(double la,double lb,double lc) {this.la=la; this.lb=lb; this.lc=lc;public String toString(){return "Triangle: sides="+la+","+lb+","+lc+":";public double getArea() {double s=(la+lb+lc)/2.0;return Math.sqrt(s*(s-la)*(s-lb)*(s?1c));}}

考题 阅读以下说明和Jrdva代码,将应填入(n)处的字句写在对应栏内。[说明]在销售系统中常常需要打印销售票据,有时需要在一般的票据基础上打印脚注。这样就需要动态地添加一些额外的职责。如下展示了Decorator(修饰)模式。SalesOrder对象使用一个SalesTicket对象打印销售票据。图6-1显示了各个类间的关系。以下是Java语言实现,能够正确编译通过。[图6-1][Java代码]//Component.java文件public (1) class Component {abstract publ ic void prtTicket();}//salesTicket.java文件public class SalesTicket extends Component{public void prtTicket(){//Sales ticket printing code hereSystem.out.printin("SalesTicket");}}//Decorator.java文件publ ic abstract class Decorator extends Component{public void prtTicket(){if(myComp!=null)myComp.prtTicket();}private (2) myComp;public Decorator(Component myC){myComp=myC;}}//Footer.java文件public class Footer extends Decorator {public Footer(Component myC){(3);}public void prtTicket(){(4);prtFooter();}publ ic void prtFooter(){//place printing footer code hereSystem.out.println("Footer");}}//salesorder.java文件public class SalesOrder{void prtTicket(){Component myST;myST=new Footer( (5) );//Print Ticket with footers as neededmyST.prtTicket();}}(1)

考题 阅读以下说明和JAVA 2代码,填入(n)处的。[说明]以下JAVA程序实现了在接口interface iShape2D的定义和应用,仔细阅读代码和相关注释,将程序补充完整。[代码6-1]interface iShape2D //定义接口{(1)(2)}(3)//实现CRectangle类{int width, height;(4) CRectangle (int w,int h) {width=w;height=h;}public void area ( ){ //定义area( )的处理方式System. out.println ("area="+width*height);}}(5)//实现CCircle类{double radius;(6) CCircle (double r) {radius=r;}public void area ( ) { //定义area( )的处理方式System.out.println ("area="+pi*radius*radius);}}[代码6-2]public class app10_4{public static void main(String args[]){CRectangle rect=new CRectangle (5,10);rect.area ( ); //调用CRectangle类里的area ( ) methodCCircle cir=new CCircle (2.0);cir.area ( ); //调用CCircl类里的area ( ) method}}

考题 阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。【说明】以下程序的功能是计算正方体、球体和圆柱体的表面积和体积并输出。程序由4个类组成:类cube、sphere和cylinder分别表示正方体、球体和圆柱体;抽象类 container为抽象类,提供了两个纯虚拟函数surface_area()和volum(),作为通用接口。【C++程序】include<iostream.h>define pi 3.1416class container{protected:double radius;public:container(double radius) {container::radius=radius;}virtual double surface_area()=0;virtual double velum()=0;};class cube:(1){ //定义正方体类public:cube(double radius):container(radius){};double surface_area () {return 6 * radius * radius;}double volum() {return radius * radius * radius;}};class sphere:(2){ //定义球体类public:sphere(double radius): container(radius){};double surface_area() { return (3);}double volum() {return pi * radius * radius * radius * 4/3;}};class cylinder:(4){ //定义圆柱体类double height;public:cylinder(double radius,double height):container(radius){container::height=height;}double surface_are a () { return 2 * pi * radius * (height+radius); }double volum () {return (5);}};void main(){container * p;cube obj1 (5);sphere obj2(5);cylinder obj3(5,5);p=obj1;cout<<“正方体表面积”(<<p->surface_area()<<end1;cont<<“正方体体积”<<p->volume()<<end1;p=obj2;cout<<“球体表面积”<<p->surface_area()<<end1;cout<<“球体体积”<<p->volume()<<end1;p=obj3;cout<<“球体表面积”<<p->surface_area()<<end1;cout<<“球体体积”<<p->volume()<<end1;}

考题 阅读以下说明和Java以及HTML代码,将应填入(n)处的字句写在对应栏内。[说明]编写一个Applet程序,接受HTML文件传递的整数参数,根据该参数指定Applet中文本框的长度。编写对应的HTML文件运行这个Applet。[Java 代码]import java. applet.*;import java. awt.*;public class chuangdics extends (1){TextField tf=new TextField ();int tfLength=0;public void init(){try{tfLength=Integer. parseInt( (2) ("length"));tf. setColumns (tfLength);add (tf);}catch (NumberFormatException nfe){tf. (3) ("HTML 文件传人的参数格式错误。");add(tf);}}}[HTML 代码]<html><head><title>(4)</title></head><body><hr><applet code=chuangdics width=700 height=150><param name=length value=20>(5)<hr></body></htmI>

考题 阅读以下说明和JAVA 2代码,将应填入(n)处的字句写在对应栏内。[说明]以下程序实现了在applet里移动图形文件,仔细阅读代码和相关注释,将程序补充完整。[代码6-1]import j ava. awt. *;import j ava.awt.event.*;import java.applet. Applet;public class AppCIU extends Applet implements MouseMotionListener, MouseListener{Image IMG nClick=over(this) title=放大; // 声明 Image 类类型的变量 IMG nClick=over(this) title=放大int x=70,y=60,posX=70,posY=60,dx,dy;public void init ( ){IMG nClick=over(this) title=放大=getImage ( getCodeBase ( ) ,"mouse.gif" ); //载入影像addMouseListener ( this );addMouseMotionListener ( this );}public void mousePressed ( MouseEvent e ){dx=e.getX()-posX; //取得按下之点与基准点X方向的距离dy=e.getY()-posY; //取得按下之点与基准点Y方向的距离}public void mouseDragged ( MouseEvent e ){(1)(2)if ( dx>0 dx<120 dy>0 dy<60 ) //如果指针落在图形上方{Graphics g=getGraphics ( );(3)}}public void paint ( Graphics g ){(4)(5)(6)}public void mouseMoved ( MouseEvent e ) {};public void mouseReleased ( MouseEvent e ) {};public void mouseEntered ( MouseEvent e ) {};public void mouseExited ( MouseEvent e ) {};public void mouseClicked ( MouseEvent e ) {};}

考题 阅读以下说明和Visual Basic代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】某绘图系统定义了一个抽象类IShape,现有三个类CPoint、CLine和CCircle,它们都具有IShape界面。相应的类图关系如图7-1所示。已知某第三方库已经提供了XCircle类,且完全满足CCircle图元显示时所需的功能。代码7-1是抽象类IShape的类模块内容,代码7-2实现了类CCircle的IShape界面,并使用了XCircle提供的显示功能。XCimle提供的显示功能方法接口为displayIt。【代码7-1】Public Color As LongSub draw()'方法体不包括可执行语句End SubSub move(stepx As Single,stepy As Smgle)'方法体不包括可执行语句End Sub【代码7-2】(1)Private color As Long… ‘其他定义省略Private bridged As (2)Private Sub Class_Initialize ( )Set bridged=(3)End SubPrivate Property (4) ( )As LongIShape_Color = colorEnd PropertyPrivate Property (5) (ByVal newColor As Long)color=newColorEnd PropertyPrivate Sub IShape_draw ( ) '使用XCirele提供的显示功能(6)End SubPrivate Sub IShape_move (stepx As Single, stepy As Single)… '省略描述End Sub

考题 阅读以下说明和Java程序代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】在下面的Java程序代码中,类SalesTicket能够完成打印票据正文的功能,类 HeadDecorator与FootDecorator分别能够完成打印票据的台头和脚注的功能。已知该程序运行后的输出结果如下所示,请填补该程序代码中的空缺。这是票据的台头!这是票据正文!-------------------这是票据的脚注!这是票据的台头!这是票据的脚注!【tava程序代码】public class SalesTicket {public void printTicket() {System. out. println ( "这是票据正文 ! ");}}public class Decorator extends SalesTicket{SalesTicket ticket;public Decorator(SalesTicket t) {ticket = t;}public void printTicket(){if(ticket != null)ticket.printTicket();}public class HeadDecorator extends Decorator{public HeadDecorator(SalesTicket t) {(1)}public void printTicket() {System. out.println("这是票据的台头! ");super.printTicket();}}public class FootDecorator extends Decorator{public FootDecorator(SalesTicket t) {(2);}public void printTicket() {super, printTicket ();System. out.println ("这是票据的脚注!");}}public class Main {public static void main(String[] args) {T = new HeadDecorator( (3));T=(4);System.out.println("------------------------------------");T = new FootDecorator((5));T.printTicket ();}}

考题 第五题 阅读以下说明和Java代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 以下Java代码实现一个超市简单销售系统中的部分功能,顾客选择图书等物件 (Item)加入购物车(ShoppingCart),到收银台(Cashier)对每个购物车中的物品统计其价格进行结账。设计如图5-1所示类图。 问题:5.1 【Java代码】 interface Item{ public void accept(Visitor visitor); public double getPrice(); } class Book (1){ private double price; public Book(double price){(2);} public void accept(Visitor visitor){ //访问本元素 (3); } public double getPrice() { return price; } } //其它物品类略 interface Visitor { public void visit(Book book); //其它物品的visit方法 } class Cashier(4){ private double totalForCart; //访问Book类型对象的价格并累加 (5){ //假设Book类型的物品价格超过10元打8折 if(book.getPrice() totalForCart+=book.getPrice(); } else totalForCart+=book.getPrice()*0.8; } //其它visit方法和折扣策略类似,此处略 public double getTotal() { return totalForCart; } } class ShoppingCart { //normal shopping cart stuff private java.util.ArrayListitems=new java.util.ArrayList<>(); public double calculatePrice() { Cashier visitor=new Cashier(); for(Item item:items) { (6); } double total=visitor.getTotal(); return total; } public void add(Item e) { this.items.add(e); } }

考题 阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】某软件公司欲开发一款汽车竞速类游戏,需要模拟长轮胎和短轮胎急刹车时在路面上留 下的不同痕迹,并考虑后续能模拟更多种轮胎急刹车时的痕迹。现采用策略(Strategy)设计模式来实现该需求,所设计的类图如图 5-1 所示。 【Java代码】import java.util.*; interface BrakeBehavior{public (1) ;/*其余代码省略*/}class LongWheelBrake implements BrakeBehavior{public void stop(){System.out.println("模拟长轮胎刹车痕迹! ");}/*其余代码省略*/}class ShortWheelBrake implements BrakeBehavior {public void stop(){System.out.println("模拟短轮胎刹车痕迹! ");}/*其余代码省略 */}abstract class Car{protected (2) wheel;public void brake(){ (3) ;}/*其余代码省略*/}class ShortWheelCar extends Car {public ShortWheelCar(BrakeBehavior behavior){(4) ; } /*其余代码省略*/}class StrategyTest{public static void main(String[]args){BrakeBehaviorbrake =new ShortWheelBrake();ShortWheelCar car1= new ShortWheelCar(brake);car1. (5) ;}}

考题 阅读以下说明和Java代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 以下Java代码实现一个超市简单销售系统中的部分功能,顾客选择图书等物件 (Item)加入购物车(ShoppingCart),到收银台(Cashier)对每个购物车中的物品统计其价格进行结账。设计如图5-1所示类图。 【Java代码】 interface Item{ public void accept(Visitor visitor); public double getPrice();}class Book (1){ private double price; public Book(double price){(2);} public void accept(Visitor visitor){ //访问本元素 (3); } public double getPrice() { return price; }}//其它物品类略 interface Visitor { public void visit(Book book); //其它物品的visit方法 } class Cashier(4){ private double totalForCart; //访问Book类型对象的价格并累加 (5){ //假设Book类型的物品价格超过10元打8折 if(book.getPrice()(); public double calculatePrice() { Cashier visitor=newCashier(); for(Item item:items) { (6); } doubletotal=visitor.getTotal(); return total; } public void add(Item e) { this.items.add(e); }}

考题 阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】 某文件管理系统中定义了类OfficeDoc和DocExplorer,当类OfficeDoc发生变化时,类DocExplorer的所有对象都要更新其自身的状态,现采用观察者(Observer)设计模式来实现该需求,所设计的类图如图6-1所示。

考题 阅读下列说明和 Java 代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 生成器( Builder)模式的意图是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。图 6-1 所示为其类图。 阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 ???? 生成器(Builder)模式的意图是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。图5-1所示为其类图。 ? 【C++代码】 #include #include using namespace std; class Product { private:? string partA, partB; public:? Product() {?? }? ? void setPartA(const string} ???? void setPartB(const string}? ? //? 其余代码省略 }; class Builder { public:? ? ?? (1)?? ;? virtual void buildPartB()=0;? ? ? (2)?? ; }; class ConcreteBuilder1 : public Builder { private:? Product*?? product; public: ConcreteBuilder1() {product = new Product();???? } void buildPartA() {????? (3)???? ("Component A"); }? void buildPartB() {????? (4)???? ("Component B"); }?? Product* getResult() { return product; } //? 其余代码省略 }; class ConcreteBuilder2 : public Builder {? ??? ? ? ? /*??? 代码省略??? */ }; class Director { private:? ?? Builder* builder; public:?? Director(Builder* pBuilder) { builder= pBuilder;}? ?? void construct() { ????????????????? (5)???? ; ?????????????? //? 其余代码省略? ? }?? //? 其余代码省略 }; int main() {? ? ?? Director* director1 = new Director(new ConcreteBuilder1());? ? director1->construct();? ? ?? delete director1;? ? ? return 0; 【Java代码】 import jav(6)A.util.*; class Product {? ? ? ? private String partA;? ? ? ? private String partB;? ? ? ?? public Product() {}? ? ?? public void setPartA(String s) { partA = s; }? ? ? ? public void setPartB(String s) { partB = s; } } interface Builder {? ? public?????? (1)???? ;? ?? public void buildPartB();? ? ?? public?????? (2)???? ; } class ConcreteBuilder1 implements Builder {? ? ? ? private Product product;? ? ? ? public ConcreteBuilder1() { product = new Product();?? }? ? ? ?? public void buildPartA() {???????? (3)?? ("Component A"); } public void buildPartB() {???? ????(4)?? ("Component B"); }? ? ?? public Product getResult() { return product;} } class ConcreteBuilder2 implements Builder {?? ? ? ? ? //? 代码省略 } class Director {? ? ? ? private Builder builder;? ? ? ? public Director(Builder builder) {this.builder = builder; } public void construct() { ? ? ? ? ? ? ? ? ? (5)???? ; ? ? ? ? ? ? ? //? 代码省略? ? ?? } } class Test {? ? ?? public static void main(String[] args) { ???????????????? Director director1 = new Director(new ConcreteBuilder1()); ???????????????? director1.construct();? ? ? ?? }