当前位置:首页计算机类软件水平考试初级程序员->阅读以下说明和Java程序,填补代码中的空缺(1)~(5),

阅读以下说明和Java程序,填补代码中的空缺(1)~(5),将解答填入答题纸的对应栏内。

【说明】

对部分乐器进行建模,其类图如图6-1所示,包括:乐器(Instrument)、管乐器(Wind)、打击乐器( Percussion)、弦乐器(Stringed)、木管乐器(Woodwind)、铜管乐器(Brass)。

初级程序员,章节练习,基础复习,案例分析

下面是实现上述设计的Java代码,其中音乐类(Music)使用各类乐器(Instrument)进行演奏和调音等操作。

【Java代码】

enum Note{ /* 枚举各种音调 */

MIDDLE_C, C_SHARP, B_FLAT; //其他略

}

interface Instrument { /* 接口,乐器 */

(1) ; //play方法接口

void adjust() ; //adjust方法接口

}

class Wind (2) {

public void play(Note n) { System.out.println("Wind.play()"+n); }

public void adjust() { System.out.println("Wind.adjust()"); }

}

/* 类Percussion和Stringet实现代码略 */

class Brass (3) {

public void play(Note n) { System.out.println("Brass.play()"+n); }

public void adjust () { System.out.println("Brass.adjust()"); }

}

class Woodwind extends Wind {

public void play (Note n) { System.out.println("Woodwind.play()"+n); }

}

public class Music {

void tune(Instrument_i) { i.play(Note.MIDDLE_C); }

void adjust(Instrument i) { i.adjust(); }

void tuneAll (4) e ) {

for(lnstrument i : e) {

adjust(i);

tune(i);

}

}

public static void main(String[] args) {

Music music= (5) Music();

Instrument[] orchestra={ new Wind(), new Woodwind() };

music.tuneAll(orchestra);

}

}

本程序运行后的输出结果为:

Wind.adjust()

Wind.play() MIDDLE_C

Wind.adjust()

Woodwind.play() MIDDLE_C

查看答案 纠错
答案:
本题解析:

(1)void play(Note n)

(2)implements Instrument

(3)extends Wind

(4)Instrument[]

(5)new

本题考查Java语言程序设计的能力,涉及类、对象、方法的定义和相关操作。要求考生根据给出的案例和代码说明,认真阅读理清程序思路,然后完成题目。

先考察题目说明。本题目中涉及的部分乐器,音乐类利用各类乐器进行演奏和调音等操作。根据说明进行设计,题目给出了类图(图6-1类图所示)。

图中父接口Instrument代表乐器,Java中设计为接口。Java中定义接口也即定义了抽象数据类型,用interface关键字。Instrument包含表示进行演奏的接口方法play()和表示调音的接口方法adjust(),接口方法默认为public,且没有方法实现。其中play()的参数Note实现为枚举类型(enum),以枚举各种音调。这两个方法由具体实现类完成实现,所以Instrument的play()和adjust()为方法声明,没有实现体,实现由子类完成:

void play(Note n);

void run();

Wind、Percussion和Stringed是实现接口Instmment的三个类,用关键字implements。Java中实现接口的类必须全部实现接口中的方法,才能成为具体类,否则未被实现的方法需要加上abstract关键字,并且相应类必须为抽象类。Wind、Percussion和Stringed均为具体类,都要实现Inistrument的play()方法和adjust()方法,只是各自演奏和调音方式有所不同,所以都包含了Instrument的play()方法接口和adjust()方法接口,并加以实现:

public void play(Note n) { /* 代码略 */ }

public void adjust() { /* 代码略 */ }

Wind的两个子类型Woodwind和Brass都继承自Wind, Java中继承用extends关键字,从而Woodwind和Brass也都实现了Instrument的play()方法和adjust()方法。图6-1中Woodwind类对应的Woodwind的实现中只有play()方法,没有adjust()方法的实现,因此其父类Wind的adjust()方法自动复制并被调用。

Music类对各类乐器进行演奏和调音操作。方法tune()为一个乐器的定调,其参数为乐器Instrument接口类型;方法adjust()为一个乐器进行调音,其参数也为Instrument接口类型;函数tuneAll()为每个乐器定调,其参数是所有乐器数组。Java中数组一旦创建,就可以通过成员length获取数组中成员个数。Java 5.0升始,对集合还支持foreach,对集合中每个元素循环进行处理:

for(Instrument i : e) {

adjust(i);

tune(i),

}

Music中的tune()和adjust()的参数均为Instrument接口类型引用i,调用play()和adjust()方法,其真正执行的方法根据所传实际对象而定,即动态绑定。

主控逻辑代码在Music类中程序主入口main()方法中实现。在main()方法中,先初始化Music类的对象,引用名称music,即:

Music music=new Music();

并初始化各类乐器对象数组orchestra,各类乐器用父接口Instrument类型,因为向上转型是安全的,可以自动向上转型成为Instrument类型,用父接口类型表示其各个子类型,即:

Instrument[] orchestra= { new Wind(), new Woodwind() };

Instrument orchestra[]= { new Wind(), new Woodwind() };

然后调用music的tuneAll()方法:music.tuneAll(orchestra),实现为orchestra中的每个乐器定调,其参数为orchestra数组。数组orchestra中元素的类型为Instrument,所以tuneAll()的参数也应该为Instrument类型数组,而非其子类型。在tuneAll()方法体内部,为每个数组元素调用当前对象的tune()和adjust()。

数组orchestra中第一个元素为Wind类型对象,第二个元素为Woodwind类型对象。tuneAll()中for循环的第一次执行时tune()方法中语句i.play(Note.MIDDLE_C);调用Wind中的play()方法,因此输出Wind.play() MIDDLE_C;adjust()方法中语句i.adjust();为调用Wind类的adjust()方法,输出为Wind.adjustootuneAll()中循环的第二次执行tune()方法中语句i.play(Note.MIDDLE_C);时,调用Woodwind中的play()方法,因此输出Woodwind.play() MIDDLE_C;adjust()方法中语句i.adjust();为调用Woodwind类的adjust()方法,Woodwind没有实现adjust()方法,即Wind的adjust()方法,因此输出为Wood.adjust()。

因此,空(1)需要定义接口play(Note n),题目代码中已经给出用分号结尾,所以空(1)为void play(Note n);空(2)需要实现接口Instrument,即implements Instrument;空(3)需要继承Wind,即extends Wind;空(4)需要定调的乐器数组,即Instrument[];空(5)处为创建Music类的对象的关键字new。

更新时间:2021-12-14 00:36

你可能感兴趣的试题

单选题

( )is that it provides guidance and direction on how quality will be managed and verified throughout the project.

  • A.Plan Quality Management
  • B.Manage Quality
  • C.Control Quality
  • D.Project Charter
查看答案
单选题

( )the process of determining,documenting,and managing stakeholder needs and requirements to meet Project objectives.

  • A.Plan Scope Management
  • B.Collection Requirements
  • C.Validate Scope
  • D.Control Scope
查看答案
单选题

The information security management system preserves the confidentiality,integrity and availability of information by applying a( ).

  • A.technology management process
  • B.resource management process
  • C.quality management process
  • D.risk management process
查看答案
单选题

( )is a decentralized database,ensure that the data will not be tampered with and forged.

  • A.Artificial intelligence
  • B.Blockchain
  • C.Sensing technology
  • D.Big datA
查看答案
单选题

( )puts computer resources on the web,and must meet the requirements of super capacity,super concurrency,super speed and super security.

  • A.Cloud computing
  • B.Big datA
  • C.Blockchain
  • D.Internet of things
查看答案
单选题

分配甲、乙、丙、丁四个人去完成五项任务。每人完成各项任务时间如表所示。由于任务多于人数,故规定其中有一个人可兼完成两项任务,其余三人每人完成一项。为了花费时间最少,( )应该完成两项任务。

高级信息系统项目管理师,历年真题,2021年下半年《信息系统项目管理师》真题

  • A.甲
  • B.乙
  • C.丙
  • D.丁
查看答案
单选题

已知某公司生产AB两种产品,其中生产1件A产品需要1个单位的甲资源,3个单位的丙资源;生产1件B产品需要2个单位的乙资源和2个单位的丙资源。已知现有甲乙丙三种资源4个单位、12个单位和18个单位。通过市场预测,可知A产品的单位市场利润为2元,B产品的单位市场利润为5元。该公司获得最大的市场利润应生产A产品(68)件,此时(69)资源仍有剩余。

  • A.甲
  • B.乙
  • C.丙
  • D.甲及丙
查看答案
单选题

已知某公司生产AB两种产品,其中生产1件A产品需要1个单位的甲资源,3个单位的丙资源;生产1件B产品需要2个单位的乙资源和2个单位的丙资源。已知现有甲乙丙三种资源4个单位、12个单位和18个单位。通过市场预测,可知A产品的单位市场利润为2元,B产品的单位市场利润为5元。该公司获得最大的市场利润应生产A产品(68)件,此时(69)资源仍有剩余。

  • A.0
  • B.2
  • C.4
  • D.6
查看答案
单选题

某项目2016年投资额12万元,2018年开始取得项目的净收益(产品一原料辅料及公用工程)6万元/年,2018-2021年每年还会产生其他成本(包括人员工资、管理成本、制造成本等)1.1万元/年;増值税0.35万元/年、营业税金及附加0.05万元/年。则该项目的静态投资回收期为(66)年,截止到2021年底该项目的投资收益率是(67)。

  • A.0.25
  • B.0.33
  • C.0.35
  • D.0.6
查看答案
单选题

安全审计的手段主要包括( )。

  • A.①②③
  • B.②③④
  • C.①②④
  • D.①③④
查看答案