Problem Description (from Big Java: Late Objects; Chapter 6; Programming Project P6.3):

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.

Part 1: Problem-Solving Phase

Using the Design Recipe, write each of the following for this problem:

1.Contract
2.Purpose Statement
3.Examples, making sure to include counter-examples
4.Algorithm

Make sure to test your algorithm by hand with the examples to verify it before continuing to Part 2.

Part 2: Implementation Phase

Using Eclipse, write the Java program for the algorithm formulated in Part 1, and test your program with the examples from Part 1. Make sure to incorporate your Contract, Purpose Statement and Examples as one or more comment blocks, and your Algorithm as line comments in your Java source code.

Extra Credit

You will be awarded 10 additional points each for implementing the following methods, and using (calling) them in the program (main method):

  • printStalls(boolean[])
    method outputs the given boolean array, such that _ is output if an array element is false and X is output if an array element is true
  • nextStall(boolean[]) -> int
    method calculates and returns the index in the middle of the longest run of false elements in the given boolean array

Note that to earn the 10 additional points for each method, it must be designed and documented using the Design Recipe; this is described in the document Using the Design Recipe to Write Modular Computer Programs.

Sample Runs (user input in color)

Run 1 (invalid input type):

How many stalls in the bathroom? 13.6
Invalid number of stalls 13.6

Run 2 (invalid number of stalls):

How many stalls in the bathroom? 0
Invalid number of stalls 0

Run 3 (invalid number of stalls):

How many stalls in the bathroom? -3
Invalid number of stalls -3

Run 4 (odd number of stalls):

How many stalls in the bathroom? 11
_ _ _ _ _ X _ _ _ _ _
_ _ _ _ _ X _ _ X _ _
_ _ X _ _ X _ _ X _ _
_ _ X _ _ X _ _ X X _
_ _ X _ _ X X _ X X _
_ _ X X _ X X _ X X _
X _ X X _ X X _ X X _
X _ X X _ X X _ X X X
X _ X X _ X X X X X X
X _ X X X X X X X X X
X X X X X X X X X X X

Run 5 (even number of stalls):

How many stalls in the bathroom? 8
_ _ _ X _ _ _ _
_ _ _ X _ X _ _
_ X _ X _ X _ _
_ X _ X _ X X _
_ X _ X _ X X X
_ X _ X X X X X
_ X X X X X X X
X X X X X X X X
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.