Class declarations should instantiate the Canonical Form pattern.
1. Implement the following classes following canonical form. Use JUnit to test each one. (Hint: All but iv and vi are intended to be value classes.)
i. A clock simulator has a units field (which can only be milliseconds, seconds, minutes, hours, or days) and an elapsedTime field.
ii. A task has a start time, duration, description, and an execute method.
iii. A worker has a first name, last name, id number, date of birth, and a Boolean male flag. Workers can execute tasks.
iv. A workflow has a clock, a sequence of tasks, a sequence of workers who execute the tasks, and an execute method. The worker at position i executes the task at position i. This updates the elapsedTime of the clock.
v. A rational number has a numerator and denominator. (Have your constructor throw an exception if the denominator isn't positive. Also, reduce the numerator and denominator by dividing both by their greatest common divisor.) A rational number has arithmetic methods add, mul, sub, and div.
vi. A rational series is a list of rational numbers. It has methods for adding and multiplying all of its members.
vii. An observation has a time and an observed value.
2. Criticize the following design:
3. Give a realistic example (in the form of running code) where an instance of Hashtable<Key, Value> gives the wrong or unexpected result if the Key type does not override the hashCode and an example where the wrong result is produced if the Key type does override hashCode. Explain what goes wrong if hashCode simply returns a constant value.
4. It's not true that if A extends B, then Generic<A> extends Generic<B>. For example, suppose the following declarations have been made:
class
private Filling filling;
public Filling getFilling() { return
filling; }
public void setFilling(Filling filling)
{
this.filling = filling;
}
}
class Meat { }
class Ham extends Meat { }
If it were true, then we could substitute ham sandwiches for meat sandwiches:
Sandwich<Meat> meatSandwich = new
Why is this a problem?