标签:
题目:
/*
⑴ 有四个按钮输入,分别称为B1,B2,B3和B4;
⑵ 有两个灯泡作为输出,分别称为L1和L2;
⑶ B1是打开电源的按钮;
⑷ B4是关闭电源的按钮;
⑸ B2和B3 是操作按钮;
⑹ 在B1被按下后及B4被按下前,系统应称为电源打开状态;
⑺ 在B4被按下后及B1被按下前,系统应称为电源关闭状态;
⑻ 在电源关闭状态下,B2和B3按钮不起作用;
⑼ 在电源关闭状态下,灯应不亮;
⑽ 从最近一次电源打开状态算起,如果B2被按下的次数比B3被按下的次数多,L1亮,否则L2亮。
⑾ 任何时候都不能有一个以上的灯泡亮;
⑿ 如果其中的一个灯泡出现故障,另一个灯泡应以2秒钟的间隔闪烁,而不管B2和B3的操作过程。当B4按下时,闪烁停止;当B1被按下时,闪烁重新开始。当故障被排除后闪烁停止,系统恢复正常状态。
*/
一个盒子
两个灯
四个开关
分析各个对象应该具有的属性,以及相关操作。
/*Button 对象*/ package homework01; public class Button { String name; int pressTime; boolean Pressed; public Button(String name) { this.name = name; pressTime = 0; Pressed =false; } boolean isPressed(){ return Pressed; } int getPressTime(){return pressTime;} void press(){ Pressed = true; pressTime++;} void reset(){ Pressed =false; pressTime = 0; } }
/*Light 灯对象*/ package homework01; public class Light { private String name; boolean use_state; boolean light_state; public Light(String name){ this.name = name; use_state = true; light_state =false; } boolean isGood(){return use_state;} boolean isLight(){return light_state;} void light(){ light_state = true; System.out.println(this.name + " light!"); } void die(){ light_state = false; System.out.println(this.name + " dies!"); } void goWrong(){ use_state = false;} void goUse(){ use_state = true; } void flash(){ System.out.println(this.name + " flashing!");} }
/*盒子对象*/ package homework01; import homework01.Button; import homework01.Light; public class Box { private Button B1 = new Button("B1"); Button B2 = new Button("B2"); Button B3 = new Button("B3"); Button B4 = new Button("B4"); Light L1 = new Light("L1"); Light L2 = new Light("L2"); boolean lightState = false; public void checkLightState(){ if(!(L1.isGood() && L2.isGood())){ if(!L1.isGood() && lightState && L2.isGood()) { L2.flash(); } else if(!L2.isGood() && lightState && L1.isGood()){ L1.flash(); } } else if(lightState){ if(B2.getPressTime() > B3.getPressTime()) { L1.light(); } else { L2.light(); } } } void lightGoWrong(String light){ if(light == "L1") { L1.goWrong(); } else if(light == "L2") L2.goWrong(); checkLightState(); } void lightGoUse(String light){ if(light == "L1") { L1.goUse(); } if(light =="L2") { L2.goUse(); } checkLightState(); } void pressButton(String button) { if(button == "B1") { B1.press(); this.lightState = true; System.out.println("****power on****"); B4.reset(); } if(button == "B2" && this.lightState) { B2.press(); } if(button == "B3" && this.lightState) { B3.press(); } if(button == "B4") { B4.press(); this.lightState = false; System.out.println("****power down****"); B1.reset(); B2.reset(); B3.reset(); } checkLightState(); } }
/*模拟操作,作为测试*/ package homework01; import homework01.Box; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Box B = new Box(); B.pressButton("B1");//start B.pressButton("B2");// B.pressButton("B4"); B.pressButton("B2"); B.pressButton("B1"); B.lightGoWrong("L1"); B.lightGoWrong("L2"); B.lightGoUse("L1"); B.lightGoUse("L2"); B.pressButton("B4"); B.lightGoWrong("L1"); B.lightGoWrong("L2"); B.lightGoUse("L1"); B.lightGoUse("L2"); } }
标签:
原文地址:http://www.cnblogs.com/learn-to-rock/p/5453021.html