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
2 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]2 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
Which blog had legal content and is written mostly by lawyers
Svetradugi [14.3K]
Is it asking for a web site like .edu? or something like that?
6 0
3 years ago
Read 2 more answers
HELP ME ASAP
ruslelena [56]

it is title page and give me a ❤

3 0
3 years ago
In 2-5 paragraphs, describe the points that Kendra needs to consider when choosing a telecommunications technology to meet her n
never [62]

Answer:

For marketing requirements, various communication mediums like digital communication medium, as well as non digital traditional communication medium can be used. Some like Blogs, Article directories, forums etc. can be used.

However, the AI, ML, DL and the traditional statistical analysis from Data Science can also be used. You need to install on your blog the chat-bots, And through the Chat-bots we can make a data set, that is in fact automatically generated, and contains email, phone numbers, addresses etc, and this data base can be used to run the Amazon Sage-makers. and a lot of meaningful information can be achieved through the prepared data-set through the Sage-maker.

Thus, you can find out quite easily what is good.

Sage maker, Chat bots and Blog technology is quite cost effective as well.

Explanation:

The above details covers all the requirements.

7 0
2 years ago
Chất xơ có nhiều trong thực phẩm nào?
mezya [45]

Answer: rau củ quả đều có lượng chất xơ nhiều hơn so với các loại thực phẩn khác

Explanation:

3 0
2 years ago
Clicking a _____ name opens a drop-down list of commands and options.
UkoKoshka [18]
Clicking a menu opens a drop-down list of commands and options. According to Merriam dictionary, menu is defined as a list of things that you can choose from ; especially computers or a list shown on a computer from which you make choices to control what the computer does.
6 0
2 years ago
Other questions:
  • Outline a scenario in which you might be acting ethically but might still want to remain anonymous while using the Internet. How
    14·1 answer
  • It is essential that a security professional is able to resolve and respond to cyber law inquiries and incidents while avoiding
    14·1 answer
  • What process improves the life of a computer
    11·1 answer
  • A book of the Law was found in the Temple, which was being repaired, during what year of Josiah's reign.
    15·2 answers
  • Which quality of service (QoS) mechanism provided by the network does real-time transport protocol (RTP) rely on to guarantee a
    13·1 answer
  • The purpose of​ a/an _________ system is to capture best practice solutions and program them into a set of rules in a software p
    13·1 answer
  • Reading a news release about a product is an example of <br> research.
    9·1 answer
  • 4. In computers, an integer can be represented by more than 8-bit, what is the largest positive integer that
    9·1 answer
  • The hexadecimal eqquivalent of (80)10 is
    11·1 answer
  • To obtain your class E learner license, youll need too _
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!