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
Operands may be any of the following: (select all that apply) Group of answer choices constant or constant expression register n
attashe74 [19]

Answer:

constant or constant expression

register name

variable name (memory)

Explanation:

Literally, operand means data which an operation can be performed on. The operation could be an arithmetic or logical operation.

From the list of options, several operations can be performed on:

  • constants e.g. \pi = 3.14
  • registers and
  • variables e.g. (a + b)

<em>However, no operation can be performed on keywords and/or reserved words.</em>

3 0
3 years ago
QUESTION: Which is not an example of a video port or cable?
Vikki [24]

Answer:

Radio Corporation of America (RCA)

Explanation:

A corporation is not a video connector.

8 0
3 years ago
Selective colleges choose to have in-person meetings to learn more about the applicants. These meetings are called:
lubasha [3.4K]
The meeting is called collage interviews
4 0
3 years ago
Read 2 more answers
Which tasks can a user accomplish by using the Find and Replace feature of Word? Check all that apply.
sveticcg [70]

Answer:

B,C,E your welcome

Explanation: i just took the test

3 0
2 years ago
Inputting a range of numbers comprising a batch and then inputting each serially numbered document is characteristic of the cont
Fynjy0 [20]

Answer:

batch sequence check.

Explanation:

A batch sequence check can be defined as a strategic and systematic control plan which typically involves the process of inputting a range of numbers comprising a batch and then inputting each serially numbered document.

The steps for checking an event data within a batch using a batch sequence check include the following;

I. You'll enter a range of serial numbers of the document in a batch.

II. You'll enter each serially pre-numbered document one after the other.

III. The input documents are sorted by a computer into a serial (numerical) order, match the sequence number range against the input documents, and then reports any part of the document that is missing, a duplicate and out of range.

6 0
3 years ago
Other questions:
  • Which online text source would include a review of a new TV show?
    9·2 answers
  • What is the role of the constructor for an object?
    5·1 answer
  • How do you think computers have helped to improve documentation, support and services within the healthcare industry.
    7·1 answer
  • Peter has a website that promotes his local vacation rental site. He uses Google Ads to manage his advertising campaign. He’s cr
    13·1 answer
  • PLEASE HELP!! Kou converged his Word document to a PowerPoint document. When he received the PowerPoint, he was missing material
    12·1 answer
  • Assume that the int variables i and j have been declared, and that n has been declared and initialized.
    10·1 answer
  • What are cell phones used for?
    8·2 answers
  • Which of these is a valid use of the Reply All feature?
    11·1 answer
  • Which of the following candidates would most likely be hired as a graphic artist?
    15·2 answers
  • You are making a game! The player tries to shoot an object and can hit or miss it. The player starts with 100 points, with a hit
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!