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() { } }
Revenues
class above in such a way that
only one instance can be created. Use the Singleton Pattern.
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.
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.
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.
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