SWE 619 In Class Exercise Number 7
October 24, 2017


Given the following variable declarations, independently consider the given 6 sequences of Java instructions.
    String           string = "bat";
    Integer          x = 7;
    Object[]         objects;
    List             rawList;
    List < Object >  objectList;
    List < String >  stringList;
Identify any code that results in a compiler error or warning.
Identify any code that raises a runtime exception.
Once a compiler error is noted, you do not need to analyze the sequence further.
  1. objects = new String[1]; 
    objects[0] = string;     
    objects[0] = x;        
    
  2. objects = new Object[1];
    objects[0] = string;   
    objects[0] = x;     
    
  3. stringList = new ArrayList < String >();
    stringList.add(string) ;
    
  4. objectList = new ArrayList < String >();
    objectList.add(string) ;
    
  5. objectList = new ArrayList < Object >(); 
    objectList.add(string) ;      
    objectList.add(x) ;        
    
  6. rawList = new ArrayList();
    rawList.add(string) ;    
    rawList.add(x) ;       
    

    Consider the BoundedQueue class:
    public class BoundedQueue {
    
        protected Object rep[];
        protected int front = 0;
        protected int back = -1;
        protected int size = 0;
        protected int count = 0;
    
        public BoundedQueue(int size) {
            if (size > 0) {
                this.size = size;
                rep = new Object[size];
                back = size - 1;
        }  }
    
        public boolean isEmpty() { return (count == 0); }
    
        public boolean isFull() { return (count == size); }
    
        public int getCount() { return count; }
    
        public void put(Object e) {
            if (e != null && !isFull()) {
                back++;
                if (back >= size)
                    back = 0;
                rep[back] = e;
                count++;
        }  }
    
        public Object get() {
            Object result = null;
            if (!isEmpty()) {
                result = rep[front];
                rep[front] = null;
                front++;
                if (front >= size)
                    front = 0;
                count--;
            }
            return result;
        }
    }
    
    Generify!
    Can you add a putAll() method? A getAll() method?
    Think about the abstraction function and rep-invariant. Do these methods tell you anything about code smells?