Speedometers often use a spinning disk that has alternating black and white strips such as this one:

A light sensor measures the color, receiving a stream of zeroes (black) and ones (white), such as

0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 ...

Each sensor measurement is communicated by a separate call to the add method:

SpinningDiskDevice device = new SpinningDiskDevice();
device.add(0);
device.add(0);
device.add(0);
device.add(0);
device.add(1);
device.add(1);
device.add(1);
device.add(1);
device.add(1);
device.add(1);
device.add(0);
 ...

The number of transitions from zero to one or from one to zero in a given time interval yields the speed of the disk. Here are the transitions of the preceding example stream:

0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 ...

The getTransitions method yields the number of transitions that have been observed since the device was constructed. In the previous code example, we would expect device.getTransitions to return 2.

Complete the following file:

SpinningDiskDevice.java

public class SpinningDiskDevice { private int lastInput; private int transitions; /** Constructs a spinning disk device. */ public SpinningDiskDevice() { . . . } /** Processes a new input @param input 0 or 1 */ public void add(int input) { . . . } /** Gets the number of transitions observed since this device has been constructed. */ public int getTransitions() { . . . } }

Use the following file:

SpinningDiskDeviceTester.java

public class SpinningDiskDeviceTester
{
   public static void main(String[] args)
   {
      SpinningDiskDevice device = new SpinningDiskDevice();
      device.add(1);
      device.add(1);
      device.add(1);
      device.add(1);
      device.add(0);
      device.add(0);
      device.add(0);
      device.add(0);
      device.add(0);
      device.add(0);
      device.add(0);
      device.add(1);
      System.out.println(device.getTransitions());
      System.out.println("Expected: 2");
      device.add(1);
      device.add(0);
      device.add(0);
      System.out.println(device.getTransitions());
      System.out.println("Expected: 3");
      System.out.println(device.getTransitions());
      System.out.println("Expected: 3");
      device.add(1);
      System.out.println(device.getTransitions());
      System.out.println("Expected: 4");
   }
}