java hw

Discussion in 'School Work Help' started by 3liminator, Nov 10, 2010.

  1. 3liminator

    3liminator Well-Known Member

    217
    244
    0
    Design and implement a class called Bulb that represents a light bulb that can be turned on and off. Create a driver class called Lights, whose main method instantiates and turns on some Bulb objects. Is this right? Need help.

    public class Bulb {

    private boolean On = true;
    public Bulb (boolean b) {
    On = b;
    }
    public void setStatus(boolean b) {
    On = b;
    }
    public boolean getStatus() {
    return On;
    }
    public String toString(){
    return "Bulb Status" +On;
    }
    }



    public class Lights {
    public static void main(String[] args) {
    Bulb b1 = new Bulb(true);
    Bulb b2 = new Bulb(true);
    Bulb b3 = new Bulb(true);

    b1.setStatus (true);
    b2.setStatus (true);
    b3.setStatus (true);

    System.out.println(b1);
    System.out.println(b2);
    System.out.println(b3);
    }
    }
     
  2. I don't see the purpose as to why you need to setStatus(true) if the light bulb is already on?

    can you turn a light bulb on if it's already on?

    when you initialize the variable private boolean On = true; you just told it to be on already

    then you call the constructor and tell it to be on again

    then you call setStatus and tell it to be on again