Lab: 26 - Inner Classes

Assigned
Monday, 30 October 2023
Summary
We begin our explorations of Java’s anonymous inner classes, classes without explicit names (hence, “anonymous”) that are declared within other classes (hence, “inner”).

There are a variety of ways to create nested and inner classes. This lab will give you an opportunity to try them.

Setting Up

For today’s lab, you will use

Create a new Eclipse project and import the files from the /home/jimenezp/csc207/InnerClasses directory.

Experimentation: Exploring the basis

  1. Read the descriptions of Greeter.java and Greetable.java to make sure that you understand the purposes and components of each interface.
  2. Read through GreetableExperiments.java so that you can see the shape of the basic experiment.
  3. Look at SampleGreetable1.java and predict what the output of the experiment will be. Check your prediction. Write your results/observations/what did you learn?
  4. Look at SampleGreetable2.java. Update the experiment to use SampleGreetable2. Predict the output. Then check your prediction. Write your results/observations/what did you learn?
  5. Look at SampleGreetable3.java. Update the experiment to use SampleGreetable3. Predict the output. Then check your prediction. Write your results/observations/what did you learn?
  6. Look at SampleGreetable4.java. Update the experiment to use SampleGreetable4. Predict the output. Then check your prediction. Write your results/observations/what did you learn?
  7. Change the body of GreetableExperiments.main to read
    Greetable g = new SampleGreetable4();
    experiment(g);
    experiment(g);
    

    What do you expect the new output to be? Check your prediction. Write your results/observations/what did you learn?

  8. Look at SampleGreetable5.java. Update the experiment to use SampleGreetable5. Predict the output. Then check your prediction. Write your results/observations/what did you learn?
  9. (1 point) Named Greetables: design and implement a class that implements the Greetable interface. Your class should have a field of type String called name that gets filled in by the constructor. The objects that your greeter method returns should incorporate the name in their output, but each should give independent/different output.
  10. (1 point) Updating Greeter Output: Create a new class, based on SampleGreetable4, in which the Greeter returned by greeter has an internal field that gets printed and updated each time the greet method is called. For example, calling for the second time greet(pen) on the third Greeter out of five Greeters should output something like:
    Greeting 2 from greeter 3 of 5.
    

Static Nested Class and Anonymous Comparators

Review your priority queues lab. Redo the Practical application section: “To Do” Queue.

Static nested class version

(1 point) Instead of creating two classes that implement the Comparator interface for a “To Do” Queue, you should create two static nested classes. Review the compare method, if comparing two tasks gives the same priority or completion time, it should compare them alphabetically by taskDescription.

To test your “To Do” queue, write a tester program that creates 5+ tasks, randomly assign a priority [1-10], a completion time [10 – 240], and a description (the description will be taken from an array of 10 predefined tasks). Finally, print out the two resulting priority queues in the console by removing the highest priority item from the queues.

Anonymous version

(1 point) Instead of creating static nested classes that implement the Comparator interface, use anonymous inner class to build a comparator for a “To Do” Queue. Create an additional field for your Task Class that will be used in your comparator (e.g, number of days until the task is do, or any other of your preference).

To test your “To Do” queue, you will modify the tester program to create a third priority queue that should organize the Tasks by a third criteria. Create a few tasks and print out the new priority queue to test your code.

Note: Since this comparator is only defined in this class as part of creating a collection, it cannot be used in any other programs. But since there is so little involved in creating this class - it is only one method - this form is very concise. For objects such as comparators or iterators, this can be a very useful technique for code that will not be reused frequently.

Lab Submission

Submit the source code for tasks 9 and 10, the static nested class version and anonymous version for the “To Do” queue, and your lab-report with answers to the posted questions and outcome of your programs.

Remember: Do not use magic numbers, write your code anticipating errors and print user-friendly error messages, all your public methods should be well documented (use Javadoc comments). Please include comments or organize your code and report in a way that will help the grader to find the answer to the exercises or posted questions.