JMU JMU - Department of Computer Science
Help Tools
Sample Questions for Exam 1


  1. Explain:
    1. The differences and similarities between extending a class and decorating an object.
    2. The advantages of the iterator pattern.
    3. The advantages of the factory-method pattern.
  2. Give an example of the iterator pattern in Java. Specifically, identify a class that plays the role of the Aggregate and a class that plays the role of the Iterator.
  3. Give an example of the strategy pattern in Java. Specifically, identify a method (and the class it belongs to) that is passed an object that plays the role of the Strategy.
  4. Complete the total() method in the following class using the iterator pattern.
    import java.util.*;
    
    public class Revenues
    {
        private ArrayList<String>     items;
    
        public Revenues()
        {
            items = new ArrayList<String>();
        }
    
    
        /**
         * Add an item value to the collection of revenues
         *
         * @param item   A dollar value (including the leading $ and commas)
         */
        public addItem(String item)
        {
            items.add(item);
        }
    
    
        /**
         * Calculates the total of all (valid) items in this collection
         *
         * @return   The total revenue
         */
        public double total()
        {
        
    
    
    
    
    
    
    
        }
    }
    
  5. Modify the Revenues class above in such a way that only one instance can be created. Use the Singleton Pattern.
  6. Prof. Bernstein developed an email system called JMUmble. In this system, arriving messages are handled by a PostOffice object. Depending on how the system is configured at runtime, one or more objects might need to know when a message arrives. He has already implemented several such classes: ScreenFlasher (which makes the entire screen flash -- you always know when a message has arrived), PopularityTimer (which starts a clock that show the amount of time since the most recent message arrived), and Mumbler (which uses speech generation to read the name of the person that sent the message -- this is where the system got its name). Use the observer pattern to develop a class model of this system (in UML). You do not need to include the attributes of each class, only the operations/methods. Include comments that describe each operation/method.
  7. In Java, a Map object (e.g., a HashMap) can contain only one value for each key. Hence, in order to keep a mapping of, say, students to grades, one has to be creative. One approach is to create a ManyValuedMap class that does not provide all of the functionality of a Map but delegates to an object that implements Map. For example:
    import java.util.*;
    
    
    public class ManyValuedMap<K,V>
    {
        private HashMap<K,ArrayList<V>>       delegate;
    
        public ManyValuedMap()
        {
            delegate = new HashMap<K,ArrayList<V>>();
        }
    
        // Returns all of the values associated with a key (or null)
        public ArrayList<V> get(K key)
        
        // Adds the value to the ArrayList associated with the given key
        public V put(K key, V value)
    
        // Removes the value from the ArrayList associated with the given key
        public V remove(K key, V value)
    }
        

    Implement this class.

  8. As an alternative to the implementation of the ManyValuedMap above, one could extend the HashMap class as follows:
    import java.util.*;
    
    
    public class ManyValuedMap<K,V> extends HashMap<K,ArrayList<V>>
    {
        // Searches through all of the values in all of the keys
        public boolean containsValue(V value)
    
        // Returns the first value for this key
        public V get(K key)
    
        // Returns all of the values associated with a key (or null)
        public Iterator<V> getValues(K key)
        
        // Adds the value to the ArrayList associated with the given key
        public V put(K key, V value)
    
        // Throws UnsupportedOperationException
        public void putAll(Map<? extends K,? extends V> m) 
        
        // Removes the value from the ArrayList associated with the given key
        public V remove(K key, V value)
    
        // Returns a Collection of all of the values associated with all keys
        public Collection<V> values()
    }
        

    Implement this class.

  9. Rather than extendingd the HashMap class as above, one could decorate a Map as follows:
    import java.util.*;
    
    
    public class ManyValuedMap<K,V> implements Map<K,V>
    {
        private  Map<K,ArrayList<V>>       decorated;
    
        public ManyValuedMap(Map<K,ArrayList<V>> decorated)
        {
            this.decorated = decorated;
        }
    
        // All of the methods required in the Map interface should
        // delegate to decorated as needed
    
    
        // Returns all of the values associated with a key (or null)
        public Iterator<V> getValues(K key)
        {
           Collection<V>    values;
           
           values = decorated.get(key);
           
           if (values != null) return values.iterator();
           else                return null;       
        }
    }
        

    Implement this class.

Copyright 2020