Lab 5.1: Inner Classes -- Lab Exercises

As usual submit a zipped directory your .java files to the dropbox.

Problem 13.3:

Complete problem 13.3 from the textbook. Here is boilerplate for Enumeration and NameCollection. Hint: see pp. 740-2 (pp. 723-5 in previous version). If you find the concept of Enumerators difficult, consider that you might want to iterate through a NameCollection at two different rates. You could use two different enumerators--both affilitated with the same NameCollection object--to do this. The next few paragraphs show how this can work.

Suppose a local variable, names, is defined

        String[] names = new String[] {"James", "Sarah", "Erica", "Jennifer", "William"};

Now let's use two different enumerators to work our way through the same collection:

NameCollection bob = new NameCollection(names);  // use the same variable from NameCollection.java
Enumeration pointer1 = bob.getEnumeration();
Enumeration pointer2 = bob.getEnumeration();
// Initially, both enumerators point to the first element of the bob NameCollection, "James".
String name = pointer1.getNext();  // name now has "James" and pointer1 now refers to "Sarah".
                       // pointer2 still refers to "James".
name = pointer1.getNext();// name now has "Sarah" and pointer1 now refers to "Erica".
                       // pointer2 still refers to "James".
					   

Note that your inner class in NameCollection should not be called Enumeration--that's the name of an interface. Instead you can call your inner class anything you want, but it must implement Enumeration. For sake of reference, let's call your inner class FooEnum.

The key concept is that your Enumeration class (FooEnum) is an inner class to NameCollection. Therefore, each FooEnum object can keep track of its "current position" within its NameCollection, independent of any other FooEnums (even those referring to the same NameCollection object, such as is shown above.)

Place your main() method in a driver class named NameDriver.

Problem 13.9

Complete problem 13.9 from the textbook. Here is boilerplate for Employee and HourlyEmployee. See pg. 745 (pg. 727 in the previous edition). (Hint, you will need to make Date a static inner class.) Here is a basic Date class that you can adapt.

Place your main() in a driver class named EmployeeDriver