如何用JAVA实现这个盒子?

StayHungry 2007-10-17 09:02:43
假设需要制造一个带有四个按钮和两个灯泡的盒子并且有以下功能:
(1)有四个按钮输入,分别称为B1,B2,B3,B4;
(2)有两个灯泡作为输出,分别称为L1和L2;
(3)B1是打开电源的按钮;
(4)B4是关闭电源的按钮;
(5)B2和B3是操作按钮;
(6)在B1被按下后及B4被按下前,系统应称为电源打开状态;
(7)在B4被按下后及B1被按下前,系统应称为电源关闭状态;
(8)在电源关闭状态下,B2和B3按钮不起作用;
(9)在电源关闭状态下,灯应不亮;
(10)从最近一次电源打开状态算起,如果B2被按下的次数比B3被按下的次数多,L1亮,否则L2亮;
(11)任何时候都不能有一个以上的灯泡亮;
(12)如果其中的一个灯泡出现故障,另一个灯泡应以2秒钟的间隔闪烁,而不管B2和B3的操作过程。当B4按下时,闪烁停止;当B1被按下是,闪烁重新开始。当故障别排除后闪烁停止,
系恢复正常状态;

小弟初学JAVA,请问用JAVA如何实现这个程序?希望大家可以帮帮我,谢谢大家了!
...全文
455 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
StayHungry 2007-10-22
  • 打赏
  • 举报
回复
太谢谢了,请问可以加我QQ吗?150412707
qybao 2007-10-20
  • 打赏
  • 举报
回复
我觉得这道题关键是考设计思想,这里面包含设计模式的精髓,相反结果倒不是很要
其实这道题最好的设计模式就是借鉴java的awt或swing,button相当于awt的button,box相当于容器,就当作frame好了,lamp相当button以外的component,就当作image或panel都行。这样的话可以模仿awt写一套class
以下给出模型,具体就不写了
interface ButtonListener {
onClick(ButtonEvent e);
}

class ButtonEvent {
Object src;
int eventId;
Map optionalInfo;

public ButtonEvent(Object src, int eventId) {
//
}
}

class Component {
//这里追加共有的属性和方法,比如setSize, setLocation, setBounds之类的,这样可以设置控件的大小位置,比如要控件放在box的那个位置,尺寸多少
}

class Button extends Component{
List listeners;
int clickCount;

public void addButtonListener(ButtonListener l) {

}

public void removeButtonListener(ButtonListener l) {

}

protected doClick() {
//这里面要发生ButtonEvent事件,然后回调listener
}
}

class Lamp extends Component {
int state; //状态
Color color; //灯光颜色
//还可以设置些比如功率,额定电压等等属性,然后追加相应的方法

FlashThread flashThread; //闪烁用线程

public powerOn() {

}

public powerOff() {

}

public flash(long time) {

}

public stopFlash() {

}

class FlashThread extends Thread {
public void run() {

}
}
}

class Container {
//这里设置容器的共同属性和方法,比如add(Component c)等等,用以添加控件到容器上
}

class Box extends Container implements ButtonListener {
Button[4] buttons;
Lamp[2] lamp;

public Box () {

}

public void onClick(ButtonEvent e) {
Object obj = e.getSource();
if (obj == buttons[0]) {

} else if ...

}
}
以上这样的设计模式,是按sun的awt或swing的思想去实现的,应该最符合面向对象的设计
LZ可能现在理解有限,不过将来等你技术有所提高后再回过头来看,这道题还是蛮值得研究的。
就用swing写一套基于图形化的,其实不难,有空再给你写一个。

qybao 2007-10-20
  • 打赏
  • 举报
回复
上面是按钮和灯类,以下是Box类,也是主程序
两部分合起来就可以测试了(本来是在一个帖里回复的,但是字数超过了,只好分开了)


public class Box extends JFrame implements ActionListener {
private BoxButton[] buttons;
private Lamp[] lamps;
private JButton[] accidentButtons;
private int state;
private int lastOn;
static final int interval = 2000;

public static void main(String[] args) {
new Box();
}

public Box() {
super("Box Test");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = getContentPane();
container.setLayout(null);

lamps = new Lamp[2];
lamps[0] = new Lamp(Color.red);
lamps[0].setBounds(10, 10, 80, 80);
container.add(lamps[0]);

lamps[1] = new Lamp(Color.green);
lamps[1].setBounds(100, 10, 80, 80);
container.add(lamps[1]);

buttons = new BoxButton[4];
buttons[0] = new BoxButton("电源ON");
buttons[0].setBounds(10, 120, 80, 20);
buttons[0].addActionListener(this);
buttons[0].setToolTipText("打开电源");
container.add(buttons[0]);

buttons[1] = new BoxButton("电源OFF");
buttons[1].setBounds(100, 120, 100, 20);
buttons[1].addActionListener(this);
buttons[1].setToolTipText("关闭电源");
container.add(buttons[1]);

buttons[2] = new BoxButton("红灯+");
buttons[2].setBounds(210, 120, 80, 20);
buttons[2].addActionListener(this);
buttons[2].setToolTipText("红灯点击数控制");
container.add(buttons[2]);

buttons[3] = new BoxButton("绿灯+");
buttons[3].setBounds(300, 120, 80, 20);
buttons[3].addActionListener(this);
buttons[3].setToolTipText("绿灯点击数控制");
container.add(buttons[3]);

accidentButtons = new JButton[2];
accidentButtons[0] = new JButton("红灯Err");
accidentButtons[0].setBounds(10, 160, 80, 20);
accidentButtons[0].addActionListener(this);
accidentButtons[0].setToolTipText("红灯故障/修复控制");
container.add(accidentButtons[0]);

accidentButtons[1] = new JButton("绿灯Err");
accidentButtons[1].setBounds(100, 160, 80, 20);
accidentButtons[1].addActionListener(this);
accidentButtons[1].setToolTipText("绿灯故障/修复控制");
container.add(accidentButtons[1]);

JLabel[] label = new JLabel[2];
label[0] = new JLabel();
label[0].setBounds(10, 200, 360, 20);
label[0].setText("[电源ON]:打开电源 [电源OFF]:关闭电源");
label[1] = new JLabel();
label[1].setBounds(10, 220, 360, 20);
label[1].setText("[?灯+]:?灯点击数控制 [?灯Err/Fix]:?灯障碍控制");
container.add(label[0]);
container.add(label[1]);

state = Lamp.STATE_OFF;
lastOn = -1;

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int)((screen.getWidth()-getWidth())/2);
int y = (int)((screen.getHeight()-getHeight())/2);
setLocation(x, y);
setVisible(true);
}

protected void powerOn() {
if (isPowerOn()) return;

System.out.println("电源ON");
state = Lamp.STATE_ON;
if (lastOn != -1) {
lamps[lastOn].powerOn();
}
checkLamp();
}

protected void powerOff() {
if (! isPowerOn()) return;

System.out.println("电源OFF");
state = Lamp.STATE_OFF;
lamps[0].powerOff();
lamps[1].powerOff();
}

public boolean isPowerOn() {
return state == Lamp.STATE_ON;
}

protected void doLampControl(int idx) {
if (! isPowerOn()) return;

System.out.println("电灯控制");
if (idx == 0) buttons[2].addClickCount();
else if (idx == 1) buttons[3].addClickCount();

if (buttons[2].getClickCount() > buttons[3].getClickCount()) {
lamps[1].powerOff();
lamps[0].powerOn();
lastOn = 0;
} else if (buttons[3].getClickCount() > buttons[2].getClickCount()) {
lamps[0].powerOff();
lamps[1].powerOn();
lastOn = 1;
}
}

protected void doLampAccident(int idx) {
if (idx == 0) {
if (accidentButtons[0].getText().indexOf("Err") > 0) {
lamps[0].setAccident();
accidentButtons[0].setText("红灯Fix");
System.out.println("红灯故障");
} else {
lamps[0].fixAccident();
accidentButtons[0].setText("红灯Err");
System.out.println("红灯修复");
lamps[1].stopFlash();
}
} else if (idx == 1) {
if (accidentButtons[1].getText().indexOf("Err") > 0) {
lamps[1].setAccident();
accidentButtons[1].setText("绿灯Fix");
System.out.println("绿灯故障");
} else {
lamps[1].fixAccident();
accidentButtons[1].setText("绿灯Err");
System.out.println("绿灯修复");
lamps[0].stopFlash();
}
}

checkLamp();
}

protected void checkLamp() {
if (! isPowerOn()) return;
if (lamps[0].getState() == Lamp.STATE_ERR) {
if (lamps[1].getState() != Lamp.STATE_ERR) {
lamps[1].powerOn();
lamps[1].flash(interval);
}
} else if (lamps[1].getState() == Lamp.STATE_ERR) {
if (lamps[0].getState() != Lamp.STATE_ERR) {
lamps[0].powerOn();
lamps[0].flash(interval);
}
}
}

public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();

if (obj == buttons[0]) {
powerOn();

} else if (obj == buttons[1]) {
powerOff();

} else if (obj == buttons[2]) {
doLampControl(0);

} else if (obj == buttons[3]) {
doLampControl(1);

} else if (obj == accidentButtons[0]) {
doLampAccident(0);

} else if (obj == accidentButtons[1]) {
doLampAccident(1);

} else {
// do nothing
}
}
}
qybao 2007-10-20
  • 打赏
  • 举报
回复
抽空写了个图形界面的sample,LZ慢慢学习研究吧


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class BoxButton extends JButton {
int clickCount;

public BoxButton(String text) {
super(text);
}

public void addClickCount() {
clickCount++;
}

public int getClickCount() {
return clickCount;
}
}

final class FlashThread extends Thread {
private Lamp lamp;
private long interval;
public FlashThread(Lamp lamp, long interval) {
this.lamp = lamp;
this.interval = interval;
}

public void run() {
try {
while (true) {
if (lamp.getState() != Lamp.STATE_FLASH) break;

System.out.println("闪烁...");
lamp.repaint();
sleep(interval/2);
lamp.repaint();
sleep(interval/2);
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}

class Lamp extends JPanel {
public static final int STATE_ERR = -1;
public static final int STATE_OFF = 0;
public static final int STATE_ON = 1;
public static final int STATE_FLASH = 9;

protected int state;
protected Color color;
protected FlashThread flashThread;
protected int flashOrder = 0;

public Lamp() {
super();
color = Color.white;
state = STATE_OFF;
}

public Lamp(Color color) {
super();
this.color = color;
state = STATE_OFF;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
doPaint(g);
}

protected void doPaint(Graphics g) {
if (state != STATE_ON && state != STATE_FLASH) return;

g.setColor(color);
if (state == STATE_FLASH) {
if (flashOrder == 0) {
g.setColor(getBackground());
flashOrder = 1;
} else {
flashOrder = 0;
}
}
int x = (int)(getWidth() / 2);
int y = (int)(getHeight() / 2);
int r = (x>y) ? y : x;
g.fillOval(x, y, r, r);
}

public void powerOn() {
if (state != STATE_OFF) return;

state = STATE_ON;
System.out.println(color + " 电源ON");
repaint();
}

public void powerOff() {
if (state == STATE_OFF) return;
if (state != STATE_ERR) {
state = STATE_OFF;
}
System.out.println(color + " 电源OFF");
repaint();
}

public void flash(long interval) {
if (state != STATE_ON) return;
state = STATE_FLASH;

System.out.println(color + " 闪烁...");
flashThread = new FlashThread(this, interval);
flashThread.start();
}

public void stopFlash() {
if (state != STATE_FLASH) return;

state = STATE_ON;
System.out.println(color + " 闪烁停止");
repaint();
}

public int getState() {
return state;
}

public Color getColor() {
return new Color(color.getRGB());
}

public void setAccident() {
if (state == STATE_ERR) return;
if (state != STATE_OFF) {
//
}
state = STATE_ERR;
repaint();
}

public void fixAccident() {
if (state != STATE_ERR) return;
state = STATE_OFF;
repaint();
}
}
StayHungry 2007-10-19
  • 打赏
  • 举报
回复
能不能简要说明一下?
  • 打赏
  • 举报
回复
图形化,要弄死人的。
  • 打赏
  • 举报
回复
图形化,要弄死人的。
StayHungry 2007-10-19
  • 打赏
  • 举报
回复
谢谢高手的编码,请问可不可以用图形化的方式输出呢?
dyw31415926 2007-10-18
  • 打赏
  • 举报
回复

//保存为TestBox.java
//测试类
public class TestBox{
public static void main(String[] args){
Box box = new Box();

box.b1(); //打开电源
box.b2();//按b2 (对应L1)
box.b3(); //按b3 (对应L2)
box.b3(); //按b3 (对应L2)
box.showL1L2Status();
box.brokenLight1(); //砸烂L1
box.showL1L2Status();//显示2个灯的状态
box.b4(); //关灯
box.showL1L2Status(); //显示2个灯的状态
box.b1(); //再开灯
box.showL1L2Status(); //显示2个灯的状态
box.repairLight(); //关灯,修理灯
box.showL1L2Status(); //显示2个灯的状态
box.b1(); //修理后重新打开电源
box.b2();
box.showL1L2Status();

}
}
//盒子类
class Box {
private boolean power = false;//电源
private Light l1 = new Light();//电灯一
private Light l2 = new Light();//电灯二
public void b1(){ //开打电源
power = true;
l1.setCount(0); //每次重开电源,重新计算L1,L2被按的次数
l2.setCount(0);
}
public void b2(){ //按b2按钮,对应L1
if( !power){ //如果电源没开,不响应,
return;
}
else{
this.l1.AddCount();
//System.out.println("l2:" + l1.getCount());
if( !checkBroken()){ //只有灯没坏时才响应
int light1Count = l1.getCount();
int light2Count = l2.getCount();
if( light1Count > light2Count){
l1.setStatus("light");
l2.setStatus("unlight");
}
else {if ( light2Count > light1Count){
l2.setStatus("light");
l2.setStatus("unlight");
}
else {
l1.setStatus("unlight");
l2.setStatus("unlight");
}}
}//if(!checkBroken)
else{ //灯坏了,就不响应
if( l1.getStatus().equals("broken")){
l2.setStatus("glimpse");
}
else{
l1.setStatus("glimpse");
}
}// checkBroken else
}
}
public void b3(){ //参照b2的注释
if( !power){
return;
}
else{
this.l2.AddCount();
//System.out.println("l2:" + l2.getCount());
if( !checkBroken()){
int light1Count = l1.getCount();
int light2Count = l2.getCount();
if( light1Count > light2Count){
l1.setStatus("light");
l2.setStatus("unlight");
}
else{ if ( light2Count > light1Count){
l2.setStatus("light");
l1.setStatus("unlight");
}
else {
l1.setStatus("unlight");
l2.setStatus("unlight");
} }
}//if(!checkBroken)
else{
if( l1.getStatus().equals("broken")){
l2.setStatus("glimpse");
}
else{
l1.setStatus("glimpse");
}
}// checkBroken else
}
}
public void b4(){ //关电源
power = false;
//没坏的话,就设置灯为关闭状态,否则保持原来的状态
if ( (!this.l1.getStatus().equals("broken"))
&&(!this.l2.getStatus().equals("broken"))){
l1.setStatus("unlight");
l2.setStatus("unlight");
}
}
//关灯,并将坏的灯泡休息好
public void repairLight(){
System.out.println("操作:关电灯,修理");
b4();
l1.setStatus("unlight");
l2.setStatus("unlight");
}
//将L1砸烂
public void brokenLight1(){
l1.setStatus("broken");
l2.setStatus("glimpse");
}
//将L2砸烂
public void brokenLight2(){
l2.setStatus("broken");
l1.setStatus("glimpse");
}
//显示目前灯的状态
public void showL1L2Status(){
if(power){
System.out.println("L1的状态:" + l1.getStatus());
System.out.println("L2的状态:" + l2.getStatus());
}
else{
System.out.println("L1的状态:unlight");
System.out.println("L2的状态:unlight");
}
}
//检查是否有灯坏了,有的话返回true,否则返回false
public boolean checkBroken(){
boolean flag = false;
if( l1.getStatus().equals("broken") || l2.getStatus().equals("broken")){
flag = true;
}
return flag;
}

}
//灯泡类
class Light{
private String status = "unlight";//状态,从unlight light glimpse broken中选一个
private int count = 0;
//被按下按钮的次数+1
public void AddCount(){
this.count++;
}
//取得被按下的次数
public int getCount(){
return this.count;
}
//设置被按下的次数
public void setCount(int count){
this.count = count;
}
//取得灯泡的状态
public String getStatus(){
return this.status;
}
//设置灯泡的状态
public void setStatus(String status){
this.status = status;
}

}


62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧