Recap: Storage Lockers
Put wrapper classes, generic classes, generic methods and bounded types to work together in one program
Mission: Storage Lockers
Tommy rents out storage lockers. Some hold crates labelled with names, others hold crates labelled with their weight. He wants one kind of shelf that works for both.
- Write a generic class
Shelf<T extends Comparable<T>>that keeps its items in anArrayList<T>. Give itadd(T item),size(), andsmallest()andlargest(), which find the item withcompareTo - Write a generic method
report(String label, Shelf<T> shelf)that gives back<label>: N items, smallest X, largest Y, or<label>: emptywhen the shelf has nothing on it
In main, store the names in a String array named names and the weights in an Integer array named weights. Put every name on a Shelf<String> and every weight on a Shelf<Integer>. Print the report for Names, then for Weights.
Example for names = { "Tommy", "Ken", "Lance" } and weights = { 12, 5, 30 }
Names: 3 items, smallest Ken, largest Tommy
Weights: 3 items, smallest 5, largest 30
Example for names = { "Phil" } and weights = { }
Names: 1 items, smallest Phil, largest Phil
Weights: empty