Question: I need help with java It is a well-researched fact that men in a restroom generally prefer to maximize their distance from already occupied stalls,

I need help with java

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 the stalls from the user in the RestroomSimulation.java file and then prints out the 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. The user should enter a number between 5 and 30 for the number of stalls. The number should be validated to be within that range. The rest of the code will be written in the Restroom.java file.

There are two other test programs that you can use to test your program included in the zip file. They are the ones call Restroom Tester.java and RestroomTester2.java.

I need help with Class Restroom.

/** 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() { . . . } }

/** 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()); } } }

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_____"); } }.//HIDE public class RestroomTester2 { 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_____"); wc.addOccupant(); System.out.println(wc.getStalls()); System.out.println("Expected: ___X__X__X__"); wc.addOccupant(); System.out.println(wc.getStalls()); System.out.println("Expected: _X_X__X__X__"); wc.addOccupant(); System.out.println(wc.getStalls()); System.out.println("Expected: _X_X_XX__X__"); wc.addOccupant(); System.out.println(wc.getStalls()); System.out.println("Expected: _X_X_XX_XX__"); wc.addOccupant(); System.out.println(wc.getStalls()); System.out.println("Expected: _X_X_XX_XX_X"); wc.addOccupant(); System.out.println(wc.getStalls()); System.out.println("Expected: XX_X_XX_XX_X"); wc.addOccupant(); System.out.println(wc.getStalls()); System.out.println("Expected: XXXX_XX_XX_X"); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!