题目
阅读下列说明和Java代码,应填入(n)处。
【说明】
某游戏公司现欲开发一款面向儿童的模拟游戏,该游戏主要模拟现实世界中各种鸭子的发声特征、飞行特征和外观特征。游戏需要模拟的鸭子种类及其特征如表10-7所示:
为支持将来能够模拟更多种类鸭子的特征,采用策略设计模式(Strategy)设计的类图如图10-12所示:
其中,Duck为抽象类,描述了抽象的鸭子,而类RubberDuck、MallardDuck、 CottonDuck 和 RedHeadDuck分别描述具体的鸭子种类,方法fly()、quack()和display()分别表示不同种类的鸭子都具有飞行特征、发声特征和外观特征;接口FlyBehavior与 QuackBehavior分别用于表示抽象的飞行行为与发声行为;类FlyNoWay与FlyWithWings分别描述不能飞行的行为和用翅膀飞行的行为;类Quack、Squeak与QuackNoWay分别描述发出“嘎嘎”声的行为、发出橡皮与空气摩擦声的行为与不发声的行为。请填补以下代码中的空缺。
【Java代码】
(1) FlyBehavior{
public void fly();
};
(2) QuackBehavior{
public void quack();
};
class FlyWithWings implements FlyBehavior{
public void fly(){System.out.println(“使用翅膀飞行!”);}
};
class FlyNoWay implements FlyBehavior{
public void fly(){System.out.println(“不能飞行!”);}
};
class Quack implements QuackBehavior{
public void quack(){System.out.println(“发出\‘嘎嘎\’声!”); }
};
class Squeak implements QuackBehavior{
public void quack(){System.out.println(“发出空气与橡皮摩擦声 !”);
}
};
class QuackNoWay implements QuackBehavior{
public void quack(){System.out.println(“不能发声!”);}
};
abstract class Duck{
protected FlyBehavior (3);
protected QuackBehavior (4);
public void fly(){ (5); }
public void quack() { (6);};
public (7) void display();
};
class RubberDuck extends Duck{
public RubberDuck(){
flyBehavior=new (8);
quackBehavior=new (9);
}
public void display(){/*此处省略显示橡皮鸭的代码*/ }
};
//其他代码省略
第1题
() 阅读下列说明和JAVA代码,将应填入空(n)处的字句写在答题纸的对应栏内。【说明】 某中学开展中外中学生野外生存夏令营活动,由于中外学生的语言障碍,随队为外籍学员配置一名翻译。以下代码采用适配器(Adapter)模式模拟翻译适配器。其类图如下:
第2题
阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都
有开关按钮,对应着一个不同的灯。利用该遥控器能够统一控制房间中该厂商所有品牌
灯具的开关,现采用Command(命令)模式实现该遥控器的软件部分。command模式
的类图如图6-1所示。
【Java代码】
}
第3题
试题六(共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();
}
∥其余代码省略
}
第4题
试题六(共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!
第5题
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
[说明]
编写一个字符界面的Java Application 程序,接受用户输入的10个整数,并输出这10个整数的最大值和最小值。
[Java 代码]
import java. io.* ;
public class abc
{
public static void main ((1))
{int i, n=10, max=0, min=0, temp=0;
try {
BufferedReader br = new BufferedReader (
new InputStreamReader (System.in ) );
max = min =Integer. parselnt (br. readLine () );
}. (2) (IOExccption e ) {} ;
for (i=2 ; i<=n ; i++ ) {
try {
BufferedReader br = new (3) (
new InputStreamReader (System. in ) );
temp =(4) (br. readLine ());
if (temp>max ) max=temp;
if (temp<min) (5)
} catch (IOException e ) {};
}
System.out.println ("max="+max+"\nmin="+min );
}
}
第6题
阅读下列函数说明和Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
通常情况下,用户可以对应用系统进行配置,并将配置信息保存在配置文件中,应用系统在启动时首先将配置文件加载到内存中,这些内存配置信息应该有且仅有一份。
下面的代码应用了单身模式(Singleton)以保证Configure类只能有一个实例。这样,Configure类的使用者无法定义该类的多个实例,否则会产生编译错误。
include <iostream.h>
class Configure{
(1);
Configure(){}; //构造函数
public:
static Configure *Instance();
public:
int GetConfigureData(){return data;} //获取配置信息
int SetConfigureDate(int m_data)
{data=m_data; return data;} //设置配置信息
private:
static Configure* _instance;
int data; //配置信息
};
(2)=NULL;
Configure * Configure∷Instance()
{ if(_instance==NULL)
{ _instance=(3);
//加载配置文件并设置内存配置信息,此处省略
}
return (4);
}
void main()
{ Configure *t=NULL;
t=(5);
int d=t->GetConfigureData();
//获取配置信息后进行其它工作,此处省略
}
第7题
阅读以下说明和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() ;
}
}
第8题
阅读以下说明、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>
<applet
codebase="."
code="(5) "
name="TestApplet"
width="400"
height="300"
>
</applet>
</body>
</html>
第9题
阅读以下说明和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>
第10题
阅读以下说明、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>
<applet
codebase="."
code="(5)"
name="TestApplet"
width="400"
height="300"
>
</applet>
</body>
</html>
第11题
阅读以下说明、Java代码,将应填入(n)处的字句写在对应栏内。
【说明】
本程序输出10000之内的所有完全数。完全数是指等于其所有因子和(包括1,但不包括这个数本身)的数。例如:6=1×2×3,6=1+2+3,则6是一个完全数。
【程序】
public class PerfectNum
{
Public static void main(String args[])
{
int count=1;
for(int i=1; i<10000; i++)
{
int y=0;
for(int j=1; j<i; j++)
if((1))
y=(2)
if((3))
{
System.out.print((4) +String.valueOf('\t'));
(5)
If(count%3==0)
System.out.printin();
}
}
}
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!