Question

I need help with java It is a well-researched fact that men in a restroom generally...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please let me know if you have any doubts or you want me to modify the code. And if you find this code useful then don't forget to rate my answer as thumps up. Thank you! :)


public class Restroom
{

    private boolean[] s;


    public Restroom(int ns)
    {
        s = new boolean[ns];
    }

    // Adds an occupant in the middle of the longest sequence of unoccupied places.

    public void addOccupant()
    {
        int lengthOfRun = 0;
        int runStart = 0;
        int maxRun = 0;
        int maxRunStart = 0;
        for (int i = 0; i < s.length + 1; i++)
        {
            if (i == s.length)
            {
                if (lengthOfRun > maxRun)
                {
                    maxRun = lengthOfRun;
                    maxRunStart = runStart;
                }
            } else if (lengthOfRun == 0 && !s[i])
            {
                runStart = i;
                lengthOfRun++;
            } else
            {
                if (!s[i])
                {
                    lengthOfRun++;
                } else
                {
                    if (lengthOfRun > maxRun)
                    {
                        maxRun = lengthOfRun;
                        maxRunStart = runStart;
                    }
                    lengthOfRun = 0;
                }
            }
        }
        s[maxRunStart + maxRun / 2] = true;
    }

    // Gets a string describing the current stall occupation

    public String getStalls()
    {
        String q = "";
        for (boolean a : s)
            q += a ? "X" : "_";
        return q;
    }

}

Restroom [-/IdeaProjects/Restroom] - src/Restroom.java [Restroom] Project ▼ *申旮卜 σ RestroornTesterjava . G Restroom Java if

Add a comment
Know the answer?
Add Answer to:
I need help with java It is a well-researched fact that men in a restroom generally...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAMMING It is a well-researched fact that men in a restroom generally prefer to maximize...

    JAVA PROGRAMMING 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...

  • help me with a question in java: class A { int i = 10; } class...

    help me with a question in java: class A { int i = 10; } class B extends A { int i = 20; } public class Boo { public static void main(String[] args) { A a = new B(); System.out.println(a.i); } }

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need...

    I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need to create a static method that takes an argument "n" and returns the n'th line of pascal's triangle. the main method, which will call the class, is already done and the class is set up. Please build this without modifying the main method and without modifying exactly what the static method returns. public class Main { public static void main(String[] args) { int n...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • (Reading & Writing Business Objects) I need to have my Classes be able to talk to...

    (Reading & Writing Business Objects) I need to have my Classes be able to talk to Files. How do I make it such that I can look in a File for an account number and I am able to pull up all the details? The file should be delimited by colons (":"). The Code for testing 'Select' that goes in main is: Account a1 = new Account(); a1.select(“90001”); a1.display(); Below is what it should look like for accounts 90000:3003:SAV:8855.90 &...

  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT