CSC 241- Exam 1 (February 21, 1997) Name:


1. 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 

Now consider the following method from an application that uses multiple
tanks.  

//The following method is designed to remove the whole content of tank1 and
//add it to tank2.
void move_all (Tank tank1,Tank tank2)
{
  tank2.add(tank1.content());
  tank1.remove(tank1.content());
}



(5 pts) a) Do we need try/catch? if we do, which exceptions, do we need to catch?





(5 pts) b) State in English the precondition for the above move method.  Remember 
           that arbitrary addition to a tank may cause an overflow exception.  
           Your precondition must make the relationship between tank1 and tank2 
           contents and the capacity very clear.





(5 pts) c) State in English the postcondition for the move method,  assuming
           the precondition that you developed in a).






(20 pts) d) The move method does not address the question of whether there is 
            enough room left is tank2 to add all of tank1 content to it.  Write a
            new version that would only move the portion of tank1 content that would 
            actually fit into tank2.  So, all or just part of tank1 content gets 
            moved.  You don't have to worry about exceptions.












(35 Points) ----------- 2. Provide brief responses to each of the following:   a) What does encapsulation refer to in the OOD?   b) What are Milestones in system analysis?   c) What makes a class and an application program that uses that class       loosely coupled?   d) What characteristics make a class highly cohesive?    e) What does validation and verification refer to after design is completed?   f) What is a Java applet?   g) what does it mean when a variable is qualified as static?
(30 Points) ----------- Assume. following classes: public class Stack {   public Stack (int init_size)   public boolean empty()   public boolean full()    public int pop() // take from front   public void push(int val) //put in front } public class Queue {   public Queue (int init_size)   public boolean empty()   public boolean full()    public int enqueue() //put in back   public void dequeue(int val)//take from front    public int element_count() } 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. public Stack move_all(Queue q) {  Stack s = new Stack(q.element_count());  // design a loop and take each element from q and put in s return s; }