Java Reference
In-Depth Information
package com.oozinoz.applications;
import com.oozinoz.fireworks.*;
public class ShowFireworkList
{
public static void main(String[] args)
{
FireworkList flist =
Promo.getPromotionalFireworks();
?? i = ??
while (i.hasNext())
{
Firework f = i.next();
System.out.println(
f.getName()
+ ", $" + f.getPrice()
+ ", " + f.getType());
}
}
}
Running this program prints out:
Brightful, $0.35, Sparkler
Frightful, $0.19, Firecracker
Heightful, $33.95, Rocket
CHALLENGE 28.1
Fill in the missing code in
ShowFireworkList
to instantiate the iterator.
When you create a type-specific collection, you may want to inherit behavior from an existing
collection, such as
ArrayList
. In this approach, you have to use different method names for
get()
and
iterator()
, as you can't change the return type of an overridden method. You
will also find that using utilities, such as the
sort()
and
min()
methods of the
Collections
class, will force clients back into using casts. If you don't want to give clients
this much flexibility, you can leave your type-safe class as a subclass of
Object
and add
utilities as your clients need them. For example, if a client needs to know the cheapest
firework in a list, you might add to
FireworkList
the following method:
public Firework cheapest()
{
Comparator c = new Comparator()
{
public int compare(Object o1, Object o2)
{
Firework f1 = (Firework) o1;
Firework f2 = (Firework) o2;
return (int)
(100 * (f1.getPrice() - f2.getPrice()));
}
};