        /* Demonstration rig for the HeaterController class.
         * Create an arbitrary number of HeaterControllers and
         * randomly turn them on and off.
         * A GUI provides a visualisation via the Heater class.
         */
class HeaterControllerDemo {
        // Repeatedly select a random controller.
        // Change its state at random intervals.
    public void run(){
        while(true){
            // Select a random controller.
            final HeaterController c = randomController();
            // Change its state if possible.
            if(turnItOff()){
                if(c.isOn()){
                    c.switchOff();
                }
            }
            else{
                c.switchOn();
            }
            // Pause a little.
            delay();
        }
    }

        // Randomly decide whether to turn a controller off.
    private boolean turnItOff(){
        return Math.random() < 0.5;
    }

        // Randomly select a controller to adjust
    private HeaterController randomController(){
        final HeaterController[] controllers = getControllers();
        // Which controller to select.
        int which = (int) (Math.random()*controllers.length);
        return controllers[which];
    }

    private void delay(){
        try{
            // Sleep for 1 second.
            Thread.sleep(1000);
        }
        catch(InterruptedException e){
        }
    }

    private HeaterController[] getControllers(){
        return controllers;
    }

    // Create an arbitrary number of HeaterController objects
    // to be manipulated.
    // Initialise the individual controller objects.
    private final HeaterController[] controllers;
    {
        // How many controllers to create.
        final int numControllers = 5;
        controllers = new HeaterController[numControllers];
        for(int i = 0; i < numControllers; i++){
            controllers[i] = new HeaterController();
        }
    }
}
