Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code to be completed and done in Java It is a well-researched fact that men in a restroom generally prefer to maximize their distance from

Code to be completed and done in 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.

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()

{

. . .

}

}

RestroomSimulation:

/**

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:

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_____");

}

}

RestroomTester2:

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions