题目
阅读下列说明、图和Java代码,填补空缺。
[说明]
已知对某载客车辆(Car)进行类建模,如图13-2所示,其中类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.printin("司机尚未上车!"); 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();
}
}
第1题
阅读以下说明和Java代码,填补空缺。
[说明]
java.util库中提供了Vector模板类,可作为动态数组使用,并可容纳任意数据类型。
该类的部分方法说明如下所示:
方法名 含义
add(k) 向vector对象的尾部添加一个元素k
removeElementAt(i) 删除序号为i的元素(vector元素序号从0开始)
isEmpty() 判断vector对象是否含有元素
size() 返回vector对象中所包含的元素个数
[Java代码]
Import ________;
public class JavaMain {
static private final int ________ =6;
public static void main(String[]args) {
Vector theVector=new Vector< _______ >();
//初始化theVector,将theVector的元素设置为0至5
for(int cEachItem=0; cEachItem<ARRAY_SIZE; cEachItem++)
theVector. add(________ );
showVector(theVector); //依次输出theVector巾的元素
theVector. removeElementAt(3);
showVector(theVector);
}
public static void showVector(Vector theVector){
if(theVector. isEmpty()){
System.out.printin("theVector is empty.");
return;
}
for(int loop=0; loop<theVector.size(); loop++) {
System.out.print(theVector.get(loop));
System.out.print(",");
}
System.out.printin();
}
}
该程序运行后的输出结果为:
0, 1, 2, 3, 4, 5
___________
第2题
() 阅读下列说明和JAVA代码,将应填入空(n)处的字句写在答题纸的对应栏内。【说明】 某中学开展中外中学生野外生存夏令营活动,由于中外学生的语言障碍,随队为外籍学员配置一名翻译。以下代码采用适配器(Adapter)模式模拟翻译适配器。其类图如下:
第3题
阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都
有开关按钮,对应着一个不同的灯。利用该遥控器能够统一控制房间中该厂商所有品牌
灯具的开关,现采用Command(命令)模式实现该遥控器的软件部分。command模式
的类图如图6-1所示。
【Java代码】
}
第4题
阅读下列说明、图和Java代码,回答问题1至问题3。
【说明】
已知四个类之间的关系如图2-4所示,分别对每个类的方法进行编号,例如Shape的perimeter()为1号,表示为1:perimeter(),Rectangle类的perimeter()为2号,表示为 2:perimeter(),依此类推,其中,每个类的perimeter方法签名相同。
【Java代码】
Triangle tr=new Triangle();
Square sq=new Square();
Shape sh=tr;
关于上述Java代码中sh和tr的以下叙述中,哪两个是正确的(写出编号)。
①sh和tr分别引用同一个对象;
②sh和tr分别引用同一类型的不同的对象:
③sh和tr分别引用不同类型的不同对象:
④sh和tr分别引用同一个对象的不同拷贝;
⑤sh和tr所引用的内存空间是相同的。
第5题
阅读下列说明、图和C++代码,填补空缺。
[说明]
已知对某载客车辆(Car)进行类建模,如图12-2所示,其中类Engine表示发动机引擎,类Wheel表示车轮,类Body表示车身,类Driver表示司机,类Passenger表示乘客。
const int __________ =7; //定义最多载客数
const int MAX_WHEELS=5; //定义最多轮胎数
class Body{ //此处代码省略 ); //车身类
class Passenger{ //此处代码省略}; //乘客类
Class Wheel{ //此处代码省略}; //车轮类
Class Driver{ //司机类
public:
string name; //表示第几路公交车司机
Driver(string driverName):marne(__________ ){}; //构造函数
};
class Engine{ //引擎类
public:
string engineNo; //引擎编号
Engine(string engineNo){ __________ ->engineNo=engineNo; }
//构造函数
};
Class Car{ //汽车类
protected:
Engine *engine; Driver *driver; Body body;
Wheel *wheels[MAX_WHEELS]; Passenger *passengers[MAX_PASSENGERS];
public:
Car(Driver *driver)(//构造函数
this->driver=driver;
engine=new Engine("TX653 6型号引擎");
for(int index=0; index<MAX_WHEELS; index++){
wheels[index]=new Wheel();
}
for(int index=0; index passengers[index]=NULL;
}
}
virtual -Car(){ //析构函数
for (int index=0; index<MAX_WHEELS; index++)
delete wheels[index];
delete __________ ;
}
int getPassengerNumber(){ //获取车上乘客数量
//此处代码省略
}
void getonpassenger(Passenger *apassenger ){
//乘客上车
//此处代码省略
}
void run(){ //开车
if(driver==NULL){cout<< "司机尚未上车!"; return; }
//此处代码省略
}
};
void main(){
Driver driver("第五路公交车司机");
Car car(__________ );
Passenger passengers[MAX_PASSENGERS];
for(int index=0; index<MAX_PASSENGERS; index++)
//乘客上车处理
car. getOnPasSenger(&passengers[index]);
car. run();
}
第6题
请阅读以下技术说明、类图及Java代码,根据要求将(1)~(5)空缺处的内容填写完整。
[说明]
已知对某载客车辆(Car)进行类建模,如图4-20所示,其中类Engine表示发动机引擎,类Wheel表示车轮,类Body表示车身,类Driver表示司机,类Passenger表示乘客。
第7题
试题六(共15分)
阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
现欲开发一个软件系统,要求能够同时支持多种不同的数据库,为此采用抽象工厂模式设计该系统。以SQL Server和Access两种数据库以及系统中的数据库表Department为例,其类图如图6-1所示。
【Java代码】
import java.util.*;
class Department{/*代码省略*/}
interface IDepartment{
(1) ;
(2) ;
}
class SqlserverDepartment (3) {
public voidInsert(Department department){
System.out.println(”Insert a record into Department in SQL Server!");
∥其余代码省略
}
public Department GetDepartment(int id){
/*代码省略*/
}
}
classAccessDepartment(4) {
public void Insert(Department department){
System.out.println("Insert a record into Department in ACCESS!”);
∥其余代码省略
}
public Department GetDepartment(int id){
/*代码省略*/
}
}
(5) {
(6) ;
}
class SqlServerFactory implements IFactory{
public IDepartment CreateDepartment(){
retum new SqlserverDepartment();
}
∥其余代码省略
}
class AccessFactory implements IFactory{
public IDepartment CreateDepartment(){
return new AccessDepartment();
}
∥其余代码省略
}
第8题
试题六(共15分)
阅读下列说明和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图6-1所示的类图。
【java代码】
class invoice{
public void printInvoice(){:
System.out.println ("This is the content of the invoice!");
}
}
class Decorator : extends Invoice {
protected Invoice ticket;
public Decorator(lnvoice t){
ticket = t;
}
public void printinvoice(){
if(ticket != NULL)
(1);
}
}
class FootDecorator extends Decorator{
public FootDecorator(lnvoice t){
super(t);
}
public void printinvoice (){
Systent.out.println("This is the header of the invoice! ");
(2) ;
}
}
class FootDecorator extends Decorator {
public FootDecorator(invoice t):{
super(t);
}
public void printlnvoice(){
(3) ;
Systent.out.println("This is the header of the invoice! ");
}
}
Class test {
public static void main(string[] args){
Invoice t =new invioce();
Invoice ticket;
Ticket= (4) ;
Ticket. Printinvoice();
Systent.out.println(“--------------“)
Ticket= (5) ;
Ticket. Printinvoice();
}
}
程序的输出结果为:
This is the header of the invoice!
This is the content of the invoice!
This is the footnote of the invoice!
----------------------------
This is the header of the invoice!
This is the footnote of the invoice!
第9题
试题七(共 15 分)
阅读下列说明、图和Java 代码,回答问题1 至问题3,将解答写在答题纸的对应栏内。
[说明]
已知四个类之间的关系如图 7-1 所示,分别对每个类的方法进行编号,例如 Shape的 perimeter()为 1 号,表示为“1:perimeter()” ,Rectangle 类的 perimeter()为 2 号,表示为“2:perimeter()” ,依此类推,其中,每个类的 perimeter方法签名相同。
[Java 代码]
Triangle tr = new Triangle();
Square sq = new Square();
Shape sh = tr;
[问题 1] 关于上述 Java代码中 sh 和 tr 的以下叙述中,哪两个是正确的(写出编号) 。
① sh 和 tr 分别引用同一个对象;
② sh 和 tr 分别引用同一类型的不同的对象;
③ sh 和 tr 分别引用不同类型的不同对象;
④ sh 和 tr 分别引用同一个对象的不同拷贝;
⑤ sh 和 tr 所引用的内存空间是相同的。
[问题 2] 写出下面消息对应的方法编号(如果该消息错误或者没有对应的方法调用,请
填写“无” ) 。
tr.height() (1)
sh.perimeter() (2)
sq.height() (3)
sq.perimeter() (4)
sh.height() (5)
tr.perimeter() (6)
[问题 3] 下列赋值语句中哪两个是合法的(写出合法赋值语句的编号) 。
① sq = sh; ② sh = tr; ③ tr = sq; ④ sq = tr; ⑤ sh = sq;
第10题
试题六(共15分)
阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
某大型商场内安装了多个简易的纸巾售卖机,自动出售2元钱一包的纸巾,且每次仅售出一包纸巾。纸巾售卖机的状态图如图6-1所示。
采用状态(State)模式来实现该纸巾售卖机,得到如图6-2所示的类图。其中类State为抽象类,定义了投币、退币、出纸巾等方法接口。类SoldState、SoldOutState、NoQuarterState和HasQuarterState分别对应图6-1中纸巾售卖机的4种状态:售出纸巾、纸巾售完、没有投币、有2元钱。
【Java代码】
import j ava.util.*;
interface State{
public void insertQuarter(); //投币
public void ejectQuarter(); //退币
public void turnCrank();腑安下“出纸巾”按钮
public void dispense(); //出纸巾
}
class TissueMachine {
(1 ) soldOutState, noQuarterState, hasQuarterState, soldState, state;
state = soldOutState;
int count = 0; //纸巾数
public TissueMachine(intnumbers) { /*实现代码省略 */}
public StategetHasQuarterState() { returnhasQuarterState; }
public StategetNoQuarterState() { returnnoQuarterState, }
public State getSoldState() { return soldState; }
public State getSoldOutState() { return soldOutState; }
public int getCount() { return count, }
//其余代码省略
}
class NoQuarterState implements State {
TissueMachine tissueMachine;
public void insertQuarter() {
tissueMachine.setState((2));
}
//构造方法以及其余代码省略
}
class HasQuarterState implements State {
TissueMachine tissueMachine;
public void ejectQuarter() {
tissueMachine.setState ((3));
}
//构造方法以及其余代码省略
}
class SoldState implements State {
TissueMachine tissueMachine;
public void dispense() {
if(tissueMachine.getCount() > 0) {
tissueMachine.setState ((4));
} else {
tissueMachine.setState ((5)); }
}
}
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!