阅读以下说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。
说明
类Queue表示队列,类中的方法如下表所示。

类Node表示队列中的元素;类EmptyQueueException 给出了队列操作中的异常处理操作。
Java 代码
public class TestMain{ // 主类
public static void main(String args[]) {
Queue q = new Queue();
q.enqueue("first!");
q.enqueue("second!");
q.enqueue("third!");
(1) {
while(true)
System.out.println(q. dequeue());
}
catch((2)) ( }
}
}
public class Queue { // 队列
Node m_FirstNode;
public Queue() { m_FirstNode = null; }
public boolean isEmpty() {
if(m_FirstNode == null) return true;
else return false;
}
public void enqueue(Object newNode) {// 入队操作
Node next = m_FirstNode;
if(next==null) m_FirstNode = new Node(newNode);
else {
while(next.getNext() != null) next = next.getNext();
next.setNext(new Node(newNode));
}
}
public Object dequeue() (3) {// 出队操作
Object node;
if (isEmpty())
(4); // 队列为空,抛出异常
else {
node = m_FirstNode.getObject();
m_FirstNode = m_FirstNode.getNext();
return node;
}
}
}
public class Node { // 队列中的元素
Object m_Data;
Node m_Next;
public Node(Object data) { m_Data = data; m_Next = null; }
public Node(Object data, Node next) { m_Data = data; m_Next = next; }
public void setObject(Object data) { m_Data = data; }
public Object getObject0 { return m_Data; }
public void setNext(Node next) { m_Next = next; }
public Node getNext() { return m_Next; }
}
public class EmptyQueueException extends (5) { // 异常处理类
public EmptyQueueException0 {
System.out.println("队列已空 ! ");
}
}
第1题:
阅读下列函数说明和Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
类Queue表示队列,类中的方法如下表所示。

类Node表示队列中的元素;类EmptyQueueException给出了队列操作中的异常处理操作。
public class TestMain { //主类
public static viod main (String args[]){
Queue q=new Queue();
q.enqueue("first!");
q.enqueue("second!");
q.enqueue("third!");
(1) {
while(true)
System.out.println(q.dequeue());
}
catch( (2) ){ }
}
public class Queue { //队列
Node m_FirstNode;
public Queue(){m_FirstNode=null;}
public boolean isEmpty(){
if(m_FirstNode==null)return true;
else return false;
}
public viod enqueue(Object newNode) { //入队操作
Node next=m_FirstNode;
if(next==null)m_FirstNode=new Node(newNode);
else{
while(next.getNext()!=null)next=next.getNext();
next.setNext(new node(newNode));
}
}
public Object dequeue() (3) { //出队操作
Object node;
if (isEempty())
(4); //队列为空, 抛出异常
else{
node=m_FirstNode.getObject();
m_FirstNode=m_FirstNode.getNext();
return node;
}
}
}
public class Node{ //队列中的元素
Object m_Data;
Node m_Next;
public Node(Object data) {m_Data=data; m_Next=null;}
public Node(Object data, Node next) {m_Data=data; m_Next=-next;}
public void setObject(Object data) {m_Data=data;}
public Object getObject(Object data) {return m_data;}
public void setNext(Node next) {m_Next=next;}
public Node getNext() {return m_Next;}
}
public class EmptyQueueException extends (5) { //异常处理类
public EmptyQueueException() {
System.out.println("队列已空! ");
}
}
第2题:
阅读以下函数说明和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);
}
}
第3题:
阅读下列说明、图和Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
已知对某载客车辆(Car)进行类建模,如图7-1所示,其中类Engine表示发动机引擎,类Wheel表示车轮,类Body表示车身,类Driver表示司机,类Passenger表示乘客。

【Java代码】
class Body{ //此处代码省略 ); //车身类
class Passenger{ //此处代码省略 )/ //乘客类
class Wheel{ //此处代码省略 ); //车轮类
class Driver{ //司机类
public String name; //表示第几路公交车司机
public Driver(String driverName){name = driverName/) //构造函数
};
class Engine{//引擎类
public String engineNo;//引擎编号
public Engine(String engineNo){this.engineNo=engineNo;)//构造函数
};
public class Car{//汽车类
static final int(1)=7; //定义最多载客数
static final int MAX WHEELS =5; //定义最多轮胎数
protected Engine engine;
protected Driver driver;
protected Body body=new Body();
protected Wheel[] wheels;
protected Passenger[]passengers;
public Car(Driver driver){ //构造函数
(2).driver=driver;
engine=new Engine("TX6536型号引擎");
wheels=new Wheel[MAX WHEELS];
passengers=new Passenger[MAX_PASSENGERS];
for(int index=0;index<MAX_WHEELS;index++){
wheels[index]=new Wheel();
}
for(int index=0;index<MAX_PASSENGERS;index++){
passengers[index]=null;
}
}
int getPassengerNumber(){//获取车上乘客数量
//此处代码省略
}
void getOnPassenger(Passenger aPassenger){//乘客上车
//此处代码省略
}
void run(){ //开车
if((3)){System.out.println("司机尚未上车!");return;}
//此处代码省略
}
public static void main(String args[]){
Driver driver=new Driver("第五路公交车司机");
Car car=new Car((4));
for (int index = 0 ; index < MAX_PASSENGERS; index ++)
car.getOnPassenger((5) Passenger());
car.run();
}
}
第4题:
阅读以下说明和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 ( ) method
CCircle cir=new CCircle (2.0);
cir.area ( ); //调用CCircl类里的area ( ) method
}
}
第5题:
阅读以下说明和 Java 代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 在股票交易中,股票代理根据客户发出的股票操作指示进行股票的买卖操作。其类图如图 6-1 所示。相应的Java 代码附后。
图6-1 类图
【 Java 代码】 import java.util.ArrayList; import java.util.List; class Stock { private String name; private int quantity; public Stock(String name ,int quantity) { this.name = name; this.quantity = quantity; } public void buy() { System.out.println("[ 买进]: " + name + ",数量. " + quantity);} public void sell() { System.out.println("[ 卖出]: " + name + ",数量. " + quantity);} } interface Order { void execute(); } class BuyStock (1) Order { private Stock stock; public BuyStock(Stock stock) { (2) = stock; } public void execute() { stock.buy();} } //类SellStock实现和BuyStock 类似,略 class Broker { private List<Order> orderList = new ArrayList<Order>(); public void takeOrder( (3) order) { orderList.add(order); } public void placeOrders() { for ( (4) order : orderList) { order.execute(); } orderList.clear(); } } public class StockCommand { public static void main(String[] args) { Stock aStock = new Stock("股票 A" ,10); Stock bStock = new Stock("股票 B" ,20); Order buyStockOrder = new BuyStock(aStock); Order sellStockOrder = new SellStock(bStock ); Broker broker = new Broker(); broker.takeOrder(buyStockOrder); broker.takeOrder(sellStockOrder); broker. (5) ; } }
第6题:
阅读以下说明和Java代码,填充程序中的空缺,将解答填入答题纸的对应栏内。
【说明】
某应急交通控制系统(TraficControIS,stem)在红灯时控制各类车辆(Vehicle)的通
行,其类图如图6-1所示,在紧急状态下应急车辆在红灯时可通行,其余车辆按正常规
则通行。

下面的Java代码实现以上设计,请完善其中的空缺。
【Java代码】
abstract class Vehicle
public Vehicle () { }
abstract void run
};
interface Emergency {
(1) ;
(2) ;
}
class Car extends Vehicle {
public Car () { }
void run () { /*代码略*/ }
};
class Truck extends Vehicle {
public Truck () { }
void run () { /*代码略*/ }
class PoliceCar (3)
boolean isEmergency = false;
public PoliceCar () { }
public PoliceCar(boolean b) { this . isEmergency =b; }
public boolean isEmergent () { return (4); }
public void runRedLight () { /*代码略*/ }
}
/*类Ambulance. FireEngine实现代码略*/
public class TraficControISystem {/。交通控制类。/
private Vehicle[ ]V=new Vehiele [24];
int numVehicles;
public void control() {
for (int i=0; i<numVehicles; i++) {
if (v[i] instanceof Enu rgency&& ((Emergency)V [i])
isEmergent()) {
( 5 ) . runRedLigh&39;: ( ) ;
}else
(6).run( )
}
}
void add (Vehicle vehicle) { v[numVehicles++]=vehicle;)/*添加车辆*/
void shutDown()(/*代码略*/}
public static void main (Stri.ng [ ] args) {
TraficControlSystem tcs = new TraficControlSystem() ;
tcs . add (new Car () ;
tcs .add (new PoliceCar () ;
tcs .add (new Ambulance () ;
tcs . add (new Ambulance (true》 ;
tcs . add (new FireEngine ( true》 ;
tcs . add (new Truck () ;
tcs . add (new FireEngine ( ) ;
tcs . control () ;
tcs . shutDown () ;
}
}
第7题:
试题六(共15分)
阅读以下说明、图和Java代码,填补Java代码中的空缺(1)~(6),将解答写在答题纸的
对应栏内。
【说明】
已知对某几何图形绘制工具进行类建模的结果如图6.1所示,其中Shape为抽象(abstract)类,表示通用图形,Box(矩形)、Ellipse(椭圆)和Line(线条)继承(extends)了Shape类,其中,Circle表示圆(即特殊的椭圆)。

下面的Java代码用于实现图 6-1所给出的设计思路,将其空缺处填充完整并编译运行,输出结果为:
Ellipse
Circle
Ellipse
C
E
【Java代码】
(1) class Shape{
public Shape(String name){
this.name= name;
}
(2) void paint();
String getName(){
retum this.name;
}
final String name;
};
//Box 和Line类似下面 Ellipse,其代码略
class Ellipse (3) {
public Ellipse(String name){
super(name);
System.out.println("Ellipse");
}
Void paintO{∥绘制现状示意代码
System.out.println(getName0);
}
};
class Circle (4) {
public Circle(String name){
super(name);
System.out.println("Circle");
}
};
class Diagram{
private Shape shapes[]= new Shape[2];
public void drawAShape(Shape shape){
shape.paint();
}
void erase A Shape(Shape shape){
∥删除形状,代码略
}
void drawShapes(){
shapes*0+= new Circle("C”);
shapes[l]= new Ellipse("E");
for (int i=O; i<2;++i) {
drawAShap(shapes[i]);//绘制形状
}
}
void close(){
for (int i=0;i<2; ++1) { []关闭图,删除所绘制图形
(5) ;
}
}
public static void main(String[] args){
Diagram diagram= (6) ;
diagram.drawShapes();
diagram.close();
}
}
第8题:
阅读以下说明和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 ();
}
}
第9题:
第10题:
第11题:
第12题:
第13题:
阅读以下技术说明和Java代码,将Java程序中(1)~(5)空缺处的语句填写完整。
[说明]
类Queue表示队列,类中的方法如表4-12所示。

类Node表示队列中的元素;类EmptyQueueException给出了队列中的异常处理操作。
[Java代码]
public class testmain { //主类
public static viod main (string args[]) {
Queue q= new Queue;
q.enqueue("first!");
q.enqueue("second!");
q.enqueue("third!");
(1) {
while(true)
system.out.println(q.dequeue());
}
catch( (2) ) { }
}
public class Queue { //队列
node m_firstnode;
public Queue(){m_firstnode=null;}
public boolean isempty() {
if (m_firstnode= =null)
return true;
else
return false;
}
public viod enqueue(object newnode) { //入队操作
node next = m_firstnode;
if (next = = null) m_firstnode=new node(newnode);
else {
while(next.getnext() !=null)
next=next.getnext();
next.setnext(new node(newnode));
}
}
public object dequeue() (3) { //出队操作
object node;
if (is empty())
(4)
else {
node =m_firstnode.getobject();
m_firstnode=m_firstnode.getnext();
return node;
}
}
}
public class node{ //队列中的元素
object m_data;
node m_next;
public node(object data) {m_data=data; m_next=null;}
public node(object data,node next) {m_data=data; m_next=next;}
public void setobject(object data) {m_data=data; }
public object getobject(object data) {return m_data; }
public void setnext(node next) {m_next=next; }
public node getnext() {return m_next; }
}
public class emptyqueueexception extends (5) { //异常处理类
public emptyqueueexception() {
system. out. println ( "队列已空!" );
}
}
Java运行时系统要求方法必须捕获或者指定它可以抛出的所有被检查的异常如表4-16所示。
try块和catch块是异常处理器的两个组件。在try块中产生的异常通常被紧跟其后的catch块指定的处理器捕获。
try {
可能抛出异常的语句
}
catch(异常类型 异常引用) {
处理异常的语句
}
catch语句可以有多个分别处理不同类的例外。Java运行时系统从上到下分别对每个catch语句处理的例外类型进行检测直到找到类型相匹配的catch语句为止。其中类型匹配是指catch所处理的例外类型与生成的例外对象的类型完全一致或者是它的父类因此catch语句的排列顺序应该是从特殊到一般。也可以用一个catch语句处理多个例外类型此时它的例外类型参数应该是这几个例外类型的父类程序设计中要根据具体的情况来选择catch语句的例外处理类型。
在选择要抛出的异常的类型时可以使用其他人编写的异常类也可以编写自己的异常类。本试题中采用自定义的类作为异常的类型。
仔细阅读试题中给出的Java源代码可知主类TestMain包含了异常处理机制用于检测在出队时“队列为空”的错误。因此(1)空缺处所填写的内容是“try”。
由catch语句的语法可知(2)空缺处所填写的内容是对相应异常类型的引用。结合试题中给出的关键信息“类EmptyQueueException给出了队列操作中的异常处理操作”即类EmptyQueueException是进行异常处理的类因此(2)空缺处所填写的内容是“EmptyQueueExceptione”或者“Exceptione”其中e是对象名可用任意合法标识符替换。
由于异常都是从超类Exception派生而来的因此(5)空缺处所填写的内容是“Exception”。
仔细阅读主类TestMain程序段可以看到在try块的内部并没有给出显示的抛出异常语句即没有出现throw语句。结合(4)空缺处所在行的注释—“队列为空抛出异常”可以推断在类Queue的方法dequeue中必定要抛出异常。因此(3)空缺处应指定方法dequeue可以抛出异常即所填写的内容是“throws EmptyQueueException”。
(4)空缺处可以使用throw语句抛出异常而使用throw语句时需要一个参数即一个可抛出的对象因此(4)空缺处所填写的内容是“throw(new EmptyQueueException())”。
Java运行时系统要求方法必须捕获或者指定它可以抛出的所有被检查的异常,如表4-16所示。
try块和catch块是异常处理器的两个组件。在try块中产生的异常通常被紧跟其后的catch块指定的处理器捕获。
try {
可能抛出异常的语句
}
catch(异常类型 异常引用) {
处理异常的语句
}
catch语句可以有多个,分别处理不同类的例外。Java运行时系统从上到下分别对每个catch语句处理的例外类型进行检测,直到找到类型相匹配的catch语句为止。其中,类型匹配是指catch所处理的例外类型与生成的例外对象的类型完全一致或者是它的父类,因此,catch语句的排列顺序应该是从特殊到一般。也可以用一个catch语句处理多个例外类型,此时它的例外类型参数应该是这几个例外类型的父类,程序设计中要根据具体的情况来选择catch语句的例外处理类型。
在选择要抛出的异常的类型时,可以使用其他人编写的异常类,也可以编写自己的异常类。本试题中采用自定义的类作为异常的类型。
仔细阅读试题中给出的Java源代码可知,主类TestMain包含了异常处理机制,用于检测在出队时“队列为空”的错误。因此(1)空缺处所填写的内容是“try”。
由catch语句的语法可知,(2)空缺处所填写的内容是对相应异常类型的引用。结合试题中给出的关键信息“类EmptyQueueException给出了队列操作中的异常处理操作”,即类EmptyQueueException是进行异常处理的类,因此(2)空缺处所填写的内容是“EmptyQueueExceptione”,或者“Exceptione”,其中,e是对象名,可用任意合法标识符替换。
由于异常都是从超类Exception派生而来的,因此(5)空缺处所填写的内容是“Exception”。
仔细阅读主类TestMain程序段可以看到,在try块的内部并没有给出显示的抛出异常语句,即没有出现throw语句。结合(4)空缺处所在行的注释—“队列为空,抛出异常”可以推断,在类Queue的方法dequeue中必定要抛出异常。因此(3)空缺处应指定方法dequeue可以抛出异常,即所填写的内容是“throws EmptyQueueException”。
(4)空缺处可以使用throw语句抛出异常,而使用throw语句时需要一个参数,即一个可抛出的对象,因此(4)空缺处所填写的内容是“throw(new EmptyQueueException())”。
第14题:
阅读以下说明和Java代码,回答问题1和问题2,将解答填写在对应栏内。
【Java代码】
class usethread implements (1) {
int num
usethread(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代码运行的结果。
第15题:
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
java.util库中提供了Vector模板类,可作为动态数组使用,并可容纳任意数据类型。该类的部分方法说明如下表所示:

【Java代码】
import (1);
public class JavaMain {
static private final int (2)= 6;
public static void main(String[] args){
Vector<Integer> theVector = new Vector< (3) >();
// 初始化 theVector, 将theVector的元素设置为0至5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
theVector.add((4));
showVector(theVector); // 依次输出theVector中的元素
theVector.removeElementAt(3);
showVector(theVector);
}
public static void showVector(Vector<Integer> theVector
if (theVector.isEmpty()) {
System.out.println("theVectcr is empty.");
return;
}
for (int loop = 0; loop < theVector.size(); loop++)
System.out.print(theVector.get(loop));
System.out.print(", ");
}
System.out.println();
}
}
该程序运行后的输出结果为:
0,1,2,3,4,5
(5)
第16题:
阅读以下说明和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( ) ;
}
}
第17题:
阅读以下说明和 Java 代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 设计 RGB 方式表示颜色的调色板,进行绘图。其类图如图 6-1 所示。该程序的 Java代码附后。
图6-1 类图
【Java 代码】 //颜色类 class MyColor { private int red ,green, blue; public MyColor( ) { red = o; green = 0; blue = 0; } public MyColor(int red ,int green ,int blue) { this.red = red; this.green = green; this.blue = blue; } //其他方法略 public String toString( ) { return "Red: " + red + "\tGreen: " + green + "\tBlue " + blue; } } //调色板类 class Palette { public int number; / /颜色数 private (1)palette; //颜色表 public Palette( ) { number = 256; palette = new MyColor[number); } public Palette(MyColor[] palette ,int number) { (2)= number; (3)= palette; } //其他方法略 public String toString( ) { String str = ""; for (int i = 0; i < number; i++) { str +=i+ " : " + palette[i] + "\n"; } return str; } //绘图类 class Drawing { public (4) int COLORNUMBER = 16; public static void main(String[] args) { Palette palette; int red ,green ,blue; MyColor[] color = new MyColor[COLORNUMBER]; for (int i = 0; i < COLORNUMBER; i++) { red = (int) (Math.random( ) * 256); green = (int) (Math.random( ) * 256); blue = (int) (Math.random( ) * 256); color [i] = (5) (red ,green ,blue); } palette = new Palette(color ,COLORNUMBER); System.out.println(palette); } }
第18题:
试题七(共 15 分)
阅读下列说明、图和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
已知对某载客车辆(Car)进行类建模,如图 7-1所示,其中类 Engine 表示发动机引擎,类 Wheel 表示车轮,类 Body 表示车身,类 Driver 表示司机,类 Passenger 表示乘客。

[Java 代码]
class Body{ //此处代码省略 }; //车身类
class Passenger{ //此处代码省略 }; //乘客类
class Wheel{ //此处代码省略 }; //车轮类
class Driver{ //司机类
public String name; //表示第几路公交车司机
public Driver(String driverName){name = driverName;} //构造函数
};
class Engine{ //引擎类
public String engineNo; //引擎编号
public Engine(String engineNo){ this.engineNo = engineNo; } //构造函数
};
public class Car{ //汽车类
static final int (1) = 7; //定义最多载客数
static final int MAX_WHEELS = 5; //定义最多轮胎数
protected Engine engine;
protected Driver driver;
protected Body body = new Body();
protected Wheel[] wheels;
protected Passenger[] passengers;
public Car(Driver driver){ //构造函数
(2) .driver = driver;
engine = new Engine("TX6536 型号引擎");
wheels = new Wheel[MAX_WHEELS];
passengers = new Passenger[MAX_PASSENGERS];
for (int index = 0; index < MAX_WHEELS; index++){
wheels[index] = new Wheel();
}
for (int index = 0; index < MAX_PASSENGERS; index++){
passengers[index] = null;
}
}
int getPassengerNumber(){ //获取车上乘客数量
//此处代码省略
}
void getOnPassenger(Passenger aPassenger ){ //乘客上车
//此处代码省略
}
void run(){ //开车
if( (3) ){ System.out.println("司机尚未上车!"); return;}
//此处代码省略
}
public static void main(String args[]){
Driver driver = new Driver("第五路公交车司机");
Car car = new Car( (4) );
for (int index = 0 ; index < MAX_PASSENGERS; index ++)
car.getOnPassenger( (5) Passenger());
car.run();
}
}
第19题:
试题七(共 15 分)
阅读以下说明和 Java 代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
java.util 库中提供了 Vector 模板类,可作为动态数组使用,并可容纳任意数据类型。该类的部分方法说明如下表所示:

[Java 代码]
import (1) ;
public class JavaMain {
static private final int (2) = 6;
public static void main(String[] args){
Vector<Integer> theVector = new Vector< (3) >();
// 初始化 theVector,将 theVector的元素设置为 0 至 5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
theVector.add( (4) );
showVector(theVector); // 依次输出 theVector中的元素
theVector.removeElementAt(3);
showVector(theVector);
}
public static void showVector(Vector<Integer> theVector){
if (theVector.isEmpty()) {
System.out.println("theVector is empty.");
return;
}
for (int loop = 0; loop < theVector.size(); loop++) {
System.out.print(theVector.get(loop));
System.out.print(", ");
}
System.out.println();
}
}
该程序运行后的输出结果为:
0, 1, 2, 3, 4, 5
(5)
第20题:
第21题:
第22题:
第23题: