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 length of each segment is an indication of the speed: the slower the speed, the longer the segment. In your implementation, measure the length of the current segment. You need to reset the length when the inputs flip from ones to zeroes or zeroes to ones.

Complete the following file:

SpinningDiskDevice.java

public class SpinningDiskDevice { private int lastInput; private int segmentLength; /** Constructs a spinning disk device. */ public SpinningDiskDevice() { . . . } /** Processes a new input @param input 0 or 1 */ public void add(int input) { . . . } /** Gets the length of the current segment. */ public int getSegmentLength() { . . . } }

Use the following file:

SpinningDiskDeviceTester.java

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