// https://piazza.com/class#fall2012/cs46a/911
Part A. Write the following methods in a class Arrays1
.
public boolean isUpThenDown(int[] values)
Returns true
if the array is strictly increasing up to an
index, then strictly decreasing from that index to the end
public int upsAndDowns(int[] values)
Returns the number of strictly increasing and strictly decreasing segments in the array.
Draft: Implement the isUpThenDown
method.
Part B. In the Arrays2
class, add methods
public int[] copyWithNegativesFirst(int[] values)
Returns a new array with the same values in which the negative numbers come before the nonnegative numbers, without otherwise changing the order
public void negativesFirst(int[] values)
Reorders the elements in values
so that the negative
numbers come before the nonnegative numbers, without otherwise changing the
order. You must do this in-place. You may not allocate another
array, call another method, put the numbers in a string, etc. You may only
declare ≤ 4 variables.
A loop index for (int i = 0...)
counts as a variable!
public void negativesFirst(ArrayList<Integer> values)
As before, but now you may only declare 2 variables.
Draft: Just implement copyWithNegativesFirst
.
Part C. It is a well-known fact that in a men's restroom most people will
choose the middle of the longest empty subsequence of stalls. If there is more
than one such position, the one closest to the door is chosen. Implement a
class MensRoom
with these methods:
public MensRoom(int totalNumberOfStalls)
public String toString()
Returns a string where an empty stall is an underscore _ and a nonempty stall is an uppercase X.
public int add()
Adds an occupant with the choice described above, where the door is assumed next to index 0, and returns the index of the stall. If the restroom is fully occupied, returns -1.
public boolean remove(int i)
Removes the person at stall i
. Returns
true
if the removal was successful (i
was a
valid index and there was an occupant), false
otherwise.
Draft: The constructor, toString
, and add
for one
person.