Consider the following (bad) Java,
implementing the "C style" enum pattern:
public class Coins {
public static final int PENNY = 1;
public static final int NICKEL = 5;
public static final int DIME = 10;
public static final int QUARTER = 25;
}
- Give example code that illustrates a type safety problem with
Coins
.
- What code would you need to turn a nickel into a string?
Explain how this could go wrong at runtime.
- What code would you need to iterate through the coins?
- Would extensions to this particular enum be likely to require
recompilation of client code? Explain.
- Write a decent Java Enum for coins.
- Turn a nickle into a string.
- Iterate though the coins.