It is a well-researched fact that men in a restroom generally prefer to maximize their distance from already occupied stalls, by occupying the middle of the longest sequence of unoccupied places. For example, consider the situation where ten stalls are empty.


_ _ _ _ _ _ _ _ _ _


The first visitor will occupy a middle position:


_ _ _ _ _ X _ _ _ _


The next visitor will be in the middle of the empty area at the left.


_ _ X _ _ X _ _ _ _


Write a program that reads the number of stalls and then prints out diagrams in the format given above when the stalls become filled, one at a time. Hint: Use an array of boolean values to indicate whether a stall is occupied.

Complete the following files:

Restroom.java

/** A class that shows how restroom stalls are occupied. */ public class Restroom { . . . /** Constructs a restroom with a given number of stalls. @param ns the number of stalls */ public Restroom(int ns) { . . . } /* Adds an occupant in the middle of the longest sequence of unoccupied places. */ public void addOccupant() { . . . } /* Gets a string describing the current stall occupation @return a string with _ for an empty stall and X for an occupied one */ public String getStalls() { . . . } }

RestroomTester2.java

Use the following files:

RestroomSimulation.java

/**
      Print diagrams of restroom stalls as they are occupied.
      The premise is that people generally prefer to maximize
      their distance from already occupied stalls, by occupying
      the middle of the longest sequence of unoccupied places.
*/

public class RestroomSimulation
{
   public static void main(String[] args)
   {
      int STALLS = 10;
      Restroom wc = new Restroom(STALLS);

      for (int i = 1; i <= STALLS; i++)
      {
         wc.addOccupant();
         System.out.println(wc.getStalls());
      }
   }
}

RestroomTester.java

public class RestroomTester
{
   public static void main(String[] args)
   {
      int STALLS = 12;
      Restroom wc = new Restroom(STALLS);
      wc.addOccupant();
      System.out.println(wc.getStalls());
      System.out.println("Expected: ______X_____");
      wc.addOccupant();
      System.out.println(wc.getStalls());
      System.out.println("Expected: ___X__X_____");
   }
}