Homework: Queue I


  1. Implement, using only the Queue kernel methods, the static method declared as follows and answer the questions below:
    1. Why do you need the requires clause?
    2. Why is the first line of the ensures clause important (min is in entries(q))? Explain what the implementation could do if this line was not included in the postcondition.
  2. Implement the static method declared as follows:
    Note that because in Java a function can return only one value and it is not possible to return other values through the parameters, we are forced to return an array with the minimum and maximum integers of the given queue as elements.
  3. Consider the minAndMax operation again. Write a second implementation for this operation that uses the "Noah's Ark" algorithm. This algorithm takes entries from q in pairs, first comparing them to each other, then comparing the smaller of the pair to the minimum so far and the larger of the pair to the maximum so far.