试题六(共15分)
阅读以下说明和Java代码,填补Java代码中的空缺(1)~(5),将解答写在答题纸的对应栏内。
【说明】
已知某公司主要有两大类耗电资产( Asset):计算机(ComputerAsset)和建筑物( Building Asset)。为了节约能源,通过控制各种电源,将可关闭的房灯、计算机显示器等在夜间关闭。
为了实现上述需求,设计了如图6-1所示的类图,并用下面的Java代码加以实现。
【Java代码】
abstract class Asset{ /*通用资产,基类*/}
interface PowerSwitchable{ /*可在夜间关闭电源的物体实现该接口*/
public void powerDown();
public void powerUp();
}
abstract class BuildingAsset extends Asset{/*建筑物资产*/
protected int room;
public BuildingAsset(int room){ this.room= room; }
}
abstract class BuildingLight extends BuildingAsset{
//灯的通用信息:flourescent/incandescent等,略
BuildingLight(int roomNumber){ super(roomNumber);}
}
classEmergencyLight (1) {/*应急灯,永不关闭*/
EmergencyLight(int roomNumber){
super(roomNumber);
}
}
class RoomLights (2) {
RoomLights(int roomNumber){ super(roomNumber); }
public void powerDown(){ /*关电源,代码略*/}
public void powerUp(){/*开电源,代码略*/}
}
/*ComputerAsset、 Computer CPU和Computer Monitor代码略*/
public class BuildingManagement{
Asset things[]= new Asset[24];
int numltems=0;
public void goodNight(){/*值班员定时“关闭”时调用,关闭可关闭的电源*/
for (int i=0; i if( things[i] instanceof (3) ) ((PowerSwitchable)things[i]).powerDown(); } /*goodMorning()与goodNight()类似,依次调用powerUp(),其实现细节此处略*/ public void add(Asset thing){ /*为建筑添加资产*/ things[ (4) ]=thing; } public static void main(String[] args){ BuildingManagementbl= (5) BuildingManagement(); bl.add(new RoomLights(101)); //101房间的控制灯 bl.add(new EmergencyLight(101)); //101房间的应急灯 bl.add(new ComputerCPU(10104));//101房间4号桌上的计算机主机 bl.add(new ComputerMonitor(10104)); // 101房间4号桌上的计算机显示器 bl.goodNight(); } }
订单号:
遇到问题请联系在线客服
订单号:
遇到问题请联系在线客服