题目
阅读下列说明和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
现欲实现一个图像浏览系统,要求该系统能够显示BMP、JPEG和GIF三种格式的文件,并且能够在Windows和Linux两种操作系统上运行。系统首先将BMP、JPEG和GIF三种格式的文件解析为像素矩阵,然后将像素矩阵显示在屏幕上。系统需具有较好的扩展性以支持新的文件格式和操作系统。为满足上述需求并减少所需生成的子类数目,采用桥接(Bridge)设计模式进行设计所得类图如图5—7所示。采用该设计模式的原因在于:系统解析BMP、GIF与JPEG文件的代码仅与文件格式相关,而在屏幕上显示像素矩阵的代码则仅与操作系统相关。
【Java代码】
class Matrix{//各种格式的文件最终都被转化为像素矩阵
//gt处代码省略
};
abstract class ImageImp{
public abstract void doPaint(Matrix m); //显示像素矩阵m
};
class WinImp extends ImageImp{
public void doPaint(Matrix m){/*调用windows系统的绘制函数绘制像素矩阵*/}
};
class LinuxImp extends ImageImp{
public void doPaint(Matrix m){/*调用Linux系统的绘制函数绘制像素矩阵*/}
};
abstract class Image{
public void setImp(ImageImp imp){(1) =imp;}
public abstract void parseFile(String fiieName);
protected (2) imp;
};
class BMP extends Image{
public void parseFile(String fiieName){
//此处解析BMP文件并获得一个像素矩阵对象m(3) ;//显示像素矩阵m
}
};
classGIF extends Image{
//此处代码省略
};
class JPEG extends Image{
//此处代码省略
};
publ.ic class j avaMain{
public static v。id main (string[]args) {
//在windows操作系统上查看demO.bmp图像文件
Image imagel= (4) ;
ImageImp imageImpl= (5) ,(6鱼) ;
imagel.parseF’ile(“demo.bmp”);
}
}
现假设该系统需要支持10种格式的图像文件和5种操作系统,不考虑类Matrix和类ja.vaMain,若采用桥接设计模式则至少需要设计 (7) 个类。
第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题
阅读以下说明和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 );
}
}
第5题
试题六(共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!
第6题
阅读以下说明和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() ;
}
}
第7题
阅读下列函数说明和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();
//获取配置信息后进行其它工作,此处省略
}
第8题
阅读以下说明和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)
第9题
阅读以下说明、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>
第10题
阅读以下说明、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();
}
}
}
第11题
阅读以下说明和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>
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!