試題六(共15分)
閱讀以下說(shuō)明、圖和Java代碼,填補(bǔ)Java代碼中的空缺(1)—(5),將解答寫(xiě)在答題紙的對(duì)應(yīng)欄內(nèi)。
【說(shuō)明】
已知某公司主要有兩大類耗電資產(chǎn)(Asset):計(jì)算機(jī)(ComputerAsset)和建筑物(BuildingAsset)。為了節(jié)約能源,通過(guò)控制各種電源,將可關(guān)閉的房燈、計(jì)算機(jī)顯示器等在夜間關(guān)閉。
為了實(shí)現(xiàn)上述需求,設(shè)計(jì)了如下圖所示的類圖,并用下面的Java代碼加以實(shí)現(xiàn)。
【Java代碼】
abstract class Asset{ /*通用資產(chǎn),基類*/ }
interface PowerSwitchable{ /*可在夜間關(guān)閉的電源的物體實(shí)現(xiàn)該接口*/
public void powerDown( );
public void powerUP( );
}
abstract class BuildingAsset extends Asset{ /*建筑物資產(chǎn)*/
protected int room;
public BuildingAsset(int room){ this.room=room; }
}
abstract class BuildingLight extends BuildingAsset{
//燈的通用信息:fluorescent / incandescent 等,略
BuildingLight(int room Number){ super(roomNumber); }
}
class EmergencyLight(1){ /*應(yīng)急燈,永不關(guān)閉*/
EmergencyLight(int roomNumber){
super(roomNumber);
}
}
class RoomLights (2){
RoomLights(int roomNumber){ super(roomNumber);}
public void powerDown(){ /*關(guān)電源,代碼略*/ }
public void powerUp(){ /*開(kāi)電源,代碼略*/ }
}
/*ComputerAsset、ComputerCPU和ComputerMonitor代碼略*/
public class BuildingManagement{
Asset things[]=new Asset[24];
int numItems=0;
public void goodNight(){ /*值班員定時(shí)“關(guān)閉”時(shí)調(diào)用,關(guān)閉可關(guān)閉的電源*/
for(int i=0;i<things.length;i++)
if(things[i] instanceof (3))
((PowerSwitchable)things[i]).powerDown();
}
/*goodMorning( )與goodNight( )類似,依次調(diào)用powetUp( ),其實(shí)現(xiàn)細(xì)節(jié)此處略*/
public void add(Asset thing){ /*為建筑添加資產(chǎn)*/
things[(4)]=thing;
}
public static void main(String[ ]args) {
BuildingManagement b1=(5) BuildingManagement( );
b1.add(new RoomLights(101)); //101房間的控制燈
b1.add(new EmergencyLight(101)); //101房間的應(yīng)急燈
b1.add(new ComputerCPU(10104)); //101房間4號(hào)桌上的計(jì)算機(jī)主機(jī)
b1.add(new ComputerMonitor(10104)); //101房間4號(hào)桌上的計(jì)算機(jī)顯示器
b1.goodNight( );
delete b1;
}
}