Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

Follow all directions in the COP3337 Class Rules for Submitting Programs.

Please use the file > export > to zip option in NetBeans. The zip file should be named FirstNameLastNameA2.zip.

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:

//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

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_2

Step: 3

blur-text-image_3

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

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions

Question

Describe the concept of diversity.

Answered: 1 week ago

Question

Question What is a Roth 403 (b) plan?

Answered: 1 week ago