Interface Example -- creating and using an interface

// This demonstrates an interface that includes both constants and signatures
//
// Many control panels have an on/off button as well as a way to increase or decrease the output.  
// If a device has a control panel you expect to be able to turn it on and off, to increase or decrease the 
// output, or to be able to read its current setting. This is true no matter how the device actually works or 
// what other behavior it might provide.  A car heater, car fan, household heater or air conditioner, or 
// computer speaker might have similar controls.

public interface controlPanel
{
     final boolean OFF = false;
     final boolean ON = true;

     void on( );                       // turns the device on

     void off( );                     // turns the device off

     boolean getStatus ( );     // returns device status

     int getSetting ( );           // returns setting amount

     void increase ( );            // increases the setting

     void increase (int amt);   // increases the setting by the specified amount

     void decrease ( );           // decreases the setting

     void decrease (int amt);  // decreases the setting by specified amount

     void resetValue( );          // resets the setting to the default
}

//===================================================================================

// Note that a class that implements an interface MUST provide implementation
// details for EVERY method listed in the interface specification

public class fan implements controlPanel
{
     private boolean powerSwitch;
     private int setting;

     public fan( ) 
     {
          resetValue( );
     }

     // turns the device on
     public void on( )
     {
          powerSwitch = ON;
     }

     // turns the device off
     public void off( )
     {
          powerSwitch = OFF;
     }

     // returns device status
     public boolean getStatus ( )
     {
          return powerSwitch;
     }

     // returns setting amount
     public int getSetting ( )
     {
          return setting;
     }

     // increases the setting
     public void increase ( )
     {
          setting++;
     }

     // increases the setting by the specified amount
     public void increase (int amt)
     {
          setting+=amt;
     }

     // decreases the setting
     public void decrease ( )
     {
          setting--;
     }

     // decreases the setting by specified amount
     public void decrease (int amt)
     {
          setting-=amt;
     }

     // resets the setting to the default
     public void resetValue( )
     {
          setting = 0;
          powerSwitch = ON;
     }
}

//===================================================================================

// Here is an example main routine that instantiates a fan object.

import java.io.*;
class Main
{
     public static void main(String[] args)
     {
          fan blower = new fan( );

          System.out.println("The blower has been turned " + (blower.getStatus( ) ? "on." : "off."));
          System.out.println("The fan speed is " + blower.getSetting( ));
          for ( int counter = 1; counter <= 5; counter++)
          {
               blower.increase( );
               System.out.println("Incremented fan speed is " + blower.getSetting( ));
          }
          blower.increase(5);
          System.out.println("The fan speed has been adjusted to " + blower.getSetting( ));
          blower.off( );
          System.out.println("The blower has been turned " + (blower.getStatus( ) ? "on." : "off."));
          blower.resetValue( );
          System.out.println("The fan speed is " + blower.getSetting( ));
     }
}

//===================================================================================
// Sample Output

The blower has been turned on.
The fan speed is 0
Incremented fan speed is 1
Incremented fan speed is 2
Incremented fan speed is 3
Incremented fan speed is 4
Incremented fan speed is 5
The fan speed has been adjusted to 10
The blower has been turned off.
The fan speed is 0