Skip to content

Instantly share code, notes, and snippets.

@ocus
Last active August 29, 2015 14:23
Show Gist options
  • Save ocus/8d2c99b35c1168b57977 to your computer and use it in GitHub Desktop.
Save ocus/8d2c99b35c1168b57977 to your computer and use it in GitHub Desktop.
Exercise

The light show !

You are given the task to create the Java classes corresponding to light bulbs (of different characteristics) that can be individually plugged-in on one of the 4 sockets of a control panel allowing the light on or off at will.

The produced code must allow executing the "script" as follows:

static public void main(String[] args) {
    // Technicians are working hard to mount this...
    LightBulb a1 = new LightBulb("yellow");
    LightBulb a3 = new LightBulb("red");
    ControlPanel pdc = new ControlPanel();
    pdc.display();
    pdc.plugInLightBulb(a1, 1);
    pdc.plugInLightBulb(a3, 3);
    pdc.display();

    // The mad lighting designer starts his show !
    for (int i = 0; i < 10; i++) {
        System.out.println("Loop: " + i);
        pdc.display();
        pdc.turnOnLightBulb(1);
        pdc.turnOnLightBulb(2);
        pdc.display();
        pdc.turnOffLightBulb(1);
        pdc.turnOffLightBulb(2);
    }
    
    // The show was crazy !
}

We will make sure that the light bulbs burn-out after they were lit 7 times.

Tasks

  • Write the code for the classes LightBulb and ControlPanel in Java.
  • Write a class EternalLightBulb for light bulbs that never burn-out. Use this new light bulb type with the panel. You cannot modify the ControlPanel class.
  • Identify and explain the conception/coding errors induced by the fact of not having thought of all kinds of bulbs from the start.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment