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
Many professional photographers take pictures of people. What skills would someone have to have to be successful in these types
Galina-37 [17]
Wedding photographer
you are taking pictures of large groups of all kinds of people from 2 different sides of a family 

could you add me as best answer if this is correct?

7 0
3 years ago
Match the order in which you should develop a plan:
Advocard [28]

Develop

Present

Approve

Brainliiest please?

4 0
2 years ago
Why we need each section of prologprogram?
evablogger [386]

Answer:

Prolog program are used in the artificial intelligence and the web development in a systematic manner process. As, it is also sometimes known as declarative language which basically consist of some facts and list. Prolog program are divided into the sections that are:

  • Domain sections
  • Clauses sections
  • Predicates sections
  • Goal sections

We need each section of the prolog program because all the sections introduced the systematic program and performed there particular functions so by using all these processing steps an efficient function are formed.

6 0
3 years ago
Explain any one method of creating a presentation.
Arisa [49]

Answer:

Step 1: Analyze your audience

The first step in preparing a presentation is to learn more about the audience to whom you'll be speaking. It's a good idea to obtain some information on the backgrounds, values, and interests of your audience so that you understand what the audience members might expect from your presentation.

Step 2: Select a topic

Next, if possible select a topic that is of interest to the audience and to you. It will be much easier to deliver a presentation that the audience finds relevant, and more enjoyable to research a topic that is of interest to you.

Step 3: Define the objective of the presentation

Once you have selected a topic, write the objective of the presentation in a single concise statement. The objective needs to specify exactly what you want your audience to learn from your presentation. Base the objective and the level of the content on the amount of time you have for the presentation and the background knowledge of the audience. Use this statement to help keep you focused as you research and develop the presentation.

Preparing the Content of Your Presentation

Step 4: Prepare the body of the presentation

After defining the objective of your presentation, determine how much information you can present in the amount of time allowed. Also, use your knowledge about the audience to prepare a presentation with the right level of detail. You don't want to plan a presentation that is too basic or too advanced.

The body of the presentation is where you present your ideas. To present your ideas convincingly, you will need to illustrate and support them. Strategies to help you do this include the following:

Present data and facts

Read quotes from experts

Relate personal experiences

Provide vivid descriptions

And remember, as you plan the body of your presentation it's important to provide variety. Listeners may quickly become bored by lots of facts or they may tire of hearing story after story.

Step 5: Prepare the introduction and conclusion

Once you've prepared the body of the presentation, decide how you will begin and end the talk. Make sure the introduction captures the attention of your audience and the conclusion summarizes and reiterates your important points. In other words, "Tell them what you're going to tell them. Tell them. Then, tell them what you told them."

During the opening of your presentation, it's important to attract the audience's attention and build their interest. If you don't, listeners will turn their attention elsewhere and you'll have a difficult time getting it back. Strategies that you can use include the following:

Make the introduction relevant to the listeners' goals, values, and needs

Ask questions to stimulate thinking

Share a personal experience

Begin with a joke or humorous story

Project a cartoon or colorful visual

Make a stimulating or inspirational statement

Give a unique demonstration

During the opening you want to clearly present your topic and the purpose of your presentation. Clearly articulating the topic and purpose will help the listeners focus on and easily follow your main ideas.

During the conclusion of your presentation, reinforce the main ideas you communicated. Remember that listeners won't remember your entire presentation, only the main ideas. By reinforcing and reviewing the main ideas, you help the audience remember them.

[top of page]

Practicing and Delivering

Step 6: Practice delivering the presentation

Most people spend hours preparing a presentation but very little time practicing it. When you practice your presentation, you can reduce the number of times you utter words and phrases like, "um," "well," and "you know." These habits can easily diminish a speaker's credibility. You can also fine-tune your content to be sure you make your most important points in the time alloted.

In addition to planning the content of your presentation, you need to give advanced thought to how you want to deliver it. Do you want to commit your presentation to memory, use cards to guide you, or read from a script? Or, you might want to use a combination of methods. To help you decide, read the advantages and disadvantages of the four delivery methods described below.

Speaking from Memory

A human brain.

4 0
3 years ago
The probability that the price of a commodity is increasing is 0.62 and the probability that the price is decreasing is 0.18 . W
liraira [26]

bahug ka itlog hduxuwlowv heusuowjdd

8 0
3 years ago
Other questions:
  • A company that hires only American Indians is practicing
    5·2 answers
  • 1. Mobile devices have a _________ viewport that displays a web page content that fits within a mobile screen.
    11·1 answer
  • Why is it important to use standard english when applying for a job
    13·2 answers
  • Users can make notes or highlight areas of a website and then use the ________ feature of Microsoft Edge to send the highlighted
    13·1 answer
  • 5.
    9·1 answer
  • ¿que significa “TTAQMMQMPDATLSPLNDTMGCCLFEQMMPQTEIUMDR” ?
    5·2 answers
  • Pro and Cons of Artificial Intelligence in Art <br><br> You must have 3 statements in each
    14·1 answer
  • What is the difference between electrical and electronic devices?
    5·2 answers
  • 8
    7·1 answer
  • Anyone knows the answer for 6.1.4 Happy Birthday! codehs
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!