1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ioda
3 years ago
7

It is a well-researched fact that men in a restroom generally prefer to maximizetheir distance from already occupied stalls, by

occupying the middle of the longestsequence 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 stalls and then prints out diagrams in theformat given above when the stalls become filled, one at a time. Hint: Use an array ofboolean values to indicate whether a stall is occupied.
Computers and Technology
1 answer:
Artyom0805 [142]3 years ago
7 0

Answer:

The code below addresses the problem statement with appropriate comments for explanation.

Explanation:

public class Stall

{

public static void main(String[] args)

{

boolean[] arr = new boolean[10];

while(hasFreeSpots(arr))   // as long as there is a free slot

{

arr[position(arr)] = true;  

// position(arr) gets the position of slot a person would most likely enter at(view problem statement)

// setting that position true(position occupied) by arr[position(arr)]

System.out.println(stallView(arr));   // printing current state of the stall using 'X' and '_'

}

}

/**

* Find the empty position that is in the middle of the sub-array of largest group of empty spots

* @param arr an array of boolean values

* @return returns an an integer value. Optimal position to be occupied.

*/

public static int position(boolean[] arr)

{

int first = 0;   // first stores the starting position of "longest sequence of unoccupied places"

int last = 0;   // last stores the ending position of "longest sequence of unoccupied places"

int temp1 = 0;   // temp1 stores the starting position of "current sequence of unoccupied places"

int temp2 = 0; // temp2 stores the ending position of "current sequence of unoccupied places"

 

for(int i = 0; i < arr.length; i++)   // for each slot in the Stall

{    

if(!arr[i]) // if slot i is not occupied

{    

temp1 = i;   // store starting position of "current sequence of unoccupied places" in temp1

while(i < arr.length && !arr[i] )   // if i is a valid index and as long as this slot is not occupied increment i

{

i++;

}

// we break out of the loop either if i is invalid index(reached end of the array)

// or if the slot is occupied by a person already

temp2 = i;   // store ending position of "current sequence of unoccupied places" in temp2

if(Math.abs(first - last) < Math.abs(temp1 - temp2))

  // if current sequence is longer than longest sequence, update it

{

first = temp1;

last = temp2;    

}

}

}

return (first + last) / 2;   // return middle index of first and last

}

/**

* Checks if there are empty spots in an array, basically checks if there is a false boolean value

* thats representing an empty spot in an array

* @param arr the input array, of boolean values

* @return returns a boolean value, true if there are false elements inside the array

*/

public static boolean hasFreeSpots(boolean[] arr)

{

boolean rtn = true;   // initializing rtn to 'true'

for(int i = 0; i < arr.length; i++)   // for every slot in the Stall

{

if(!arr[i]){rtn = false;}   // sets rtn to false if a free spot found

}

return !rtn;   // returns true if a free spot found and returns false if no free spot found

}

/**

* X for true, _ for false. Representing occupied(X) and free stalls(_)

* @param arr a boolean array

* @return returns a string representation if the input

*/

public static String stallView(boolean[] arr)

{

String str = "";       // initializing str to be an empty String

for(int i = 0; i < arr.length; i++)   // for every slot in Stall

{

if(arr[i]){str += " X";}   // Add X to str if slot is occupied

else {str += " _";}           // prints _ to str if slot not occupied

}

return str;       // return str(pictorial representation of Stall with 'X', '_')

}

}

You might be interested in
Microsoft word is an example of utility software? <br><br>A.true <br>B.false​
monitta

Answer:

false

Explanation:

Ms word is only an application software

7 0
2 years ago
Read 2 more answers
Which of the following statements are true?
grin007 [14]

Answer:

I think the answer are two which are B and C

7 0
3 years ago
In implementing Secunity Lfe Cycle:_______
KIM [24]

Answer:

c

Explanation:

because its inportant



7 0
3 years ago
Use the arr field and mystery () method below.
kolezko [41]

Answer:

The sum of all positive even values in arr

Explanation:

We have an array named arr holding int values

Inside the method mystery:

Two variables s1 and s2 are initialized as 0

A for loop is created iterating through the arr array. Inside the loop:

num is set to the ith position of the arr (num will hold the each value in arr)

Then, we have an if statement that checks if num is greater than 0 (if it is positive number) and if num mod 2 is equal to 0 (if it is an even number). If these conditions are satisfied, num will be added to the s1 (cumulative sum). If num is less than 0 (if it is a negative number), num will be added to the s2 (cumulative sum).

When the loop is done, the value of s1 and s2 is printed.

As you can see, s1 holds the sum of positive even values in the arr

7 0
3 years ago
Jasmine took many pictures at a photo shoot. She wants to transfer these pictures from her camera to her laptop for image enhanc
hoa [83]
The answer would more likely be A
If it’s not correct I’m very sorry
5 0
2 years ago
Other questions:
  • what is the primary way to access applications in windows 8? a. control panel b. start menu c. desktop tiles d. context menu
    7·1 answer
  • Which best explains why magnets can push on or pull other magnets without touching them?
    9·1 answer
  • Explain the distinction between interpretation and compilation. What are the comparative advantages and disadvantages of the two
    11·1 answer
  • A communication medium which allows receivers to observe multiple cues, such as body language and tone of voice, and allows send
    15·1 answer
  • Advertising is organized around four distinct groups. The _____ group includes the photographers, the illustrators, video produc
    9·1 answer
  • I want to know why almost every single "expert answer verified" thing I come across is wrong. If it's wrong, why the h is it exp
    8·2 answers
  • Which of the follow will happen if you miss a monthly credit card payment?
    11·1 answer
  • HELP FAST PLS<br> Do you care more about avoiding fees/costs, accumulating perks, convenience, etc?
    14·2 answers
  • You are testing a printer you just installed, so you use the operator panel on the printer to print a test page. Later, you use
    13·1 answer
  • HELP ME ⚠️‼️⚠️‼️ DUE IN EXACTLY 27 MINUTES
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!