Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is in the java language. It is a well-researched fact that men in a

ID: 3738028 • Letter: T

Question

This program is in the java language.

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 positiorn: The next visitor will be in the middle of the empty area at the left 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.

Explanation / Answer

import java.util.Scanner;
class StallLogic
{
public void printStalls(boolean [] b)
{
for(boolean s:b)
{
System.out.print((s?"X" : "_") + " ");
}
System.out.print(" ");
}

public boolean giveFlag(boolean [] b)
{
for(boolean s : b)
{
if(!s) return false;
}
return true;
}

public int getLongest(boolean [] b)
{
int length = 0, temp = 0;
int len = b.length;
for(int i = 0; i < len ; i++)
{
if (b[i] == false)
{
temp++;
}
else{
temp = 0;
}
if (length < temp)
length = temp;
}
return length;
}

public int checkIndex(boolean [] b)
{
int length = 0 , temp = 0, ind = 0;
int len = b.length;
for (int i = 0 ; i < len ; i++)
{
if(b[i] == false)
{
temp++;
}
else{
temp = 0;
}
if (length < temp)
{
ind = i -length;
length = temp;
}
}
return ind;
}

public void findStalls(boolean [] b)
{
int loc = checkIndex(b);
int len = getLongest(b);
int ind = loc + len / 2;
b[ind] = true;
}
}

public class checkStall
{
public static void main(String [] args)
{
StallLogic stallin = new StallLogic();
System.out.print("Enter number of stalls");
Scanner in = new Scanner(System.in);
int i = in.nextInt();
boolean [] stalls = new boolean [i];
while(!stallin.giveFlag(stalls))
{
stallin.findStalls(stalls);
stallin.printStalls(stalls);
}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote