(35 Pts.) 1. Respond to the following short answer questions.
(15 Pts.) 2. Consider the following conceptual representation for a linked list. head references the first node in the list with each node pointing to the next as shown.
|5 |===>|15|===>|-8|===>|3 |===>|6 |===|| head
Now assume the following node class:
class Node { int element; Node next; Node (Node n, int e) { next=n; element=e; } }
(15 Pts.) 3. Here is the class Tank:
public class Tank { public static int capacity=5000; protected int content_; public Tank() public Tank(int init_cont) throws TankOverFlowException, IllegalArgumentException public int content () public void add(int amount) throws TankOverFlowException, IllegalArgumentException public void remove(int amount) throws TankUnderFlowException, IllegalArgumentException
a) Write the following method for an application that must distribute the contents of two tanks evenly between them. For example, if tank1's content is 100 and tank2's is 50, when this method is done, they each will contain 75. If sum of the contents is an odd number, one tank will end up with 1 gallon more than the other when this method is done (for example, if tank1 contains 101 gallons and tank2 contains 50, tank1 should end up with 76 and tank2 with 75).
Keep in mind that content_ is protected and that add and remove throw exceptions.
static void makeEqual (Tank tank1,Tank tank2) { }b) State in English the postcondition for the makeEqual method. Be concise and complete.
(15 Pts.) 4. Assume that the following array variable L and the
size variable exist and the method test2 has access to
them. What answer will this method return if we call it with
test2(0)?
--------------------------------------------
L | 134 | 322 | 88 | 343 | 11 | size=5
--------------------------------------------
0 1 2 3 4
int test2 (int i) {
if (i==size) return -1;
int temp = test2(i+1);
if (temp > L[i])
return temp;
else
return L[i];
}
(20 Points) 5. Assume the following Stack and Queue class definitions; note that they are NOT intStack and stringQueue, they are also not abstract:
public class Stack { public Stack () public boolean empty() public boolean full() public int pop() public void push(int val) } public class Queue { public Queue () public boolean empty() public boolean full() public void enqueue(int val) public int dequeue() }
Complete the following method which is suppose to take elements of a queue (q) out one at a time and put them into a newly created stack (s) until there are no more elements in q. This method returns s when it is done. Don't worry about the stack becoming full.
public Stack move_all(Queue q) { Stack s = new Stack(); // design a loop to take each element from q and put in s return s; }