There are a variety of ways to create nested and inner classes. This lab will give you an opportunity to try them.
For today’s lab, you will use
Create a new Eclipse project and import the files from the /home/jimenezp/csc207/InnerClasses
directory.
Greeter.java
and Greetable.java
to make sure that you understand the purposes and components of each interface.GreetableExperiments.java
so that you can see the shape of the basic experiment.SampleGreetable1.java
and predict what the output of the experiment will be. Check your prediction. Write your results/observations/what did you learn?SampleGreetable2.java
. Update the experiment to use SampleGreetable2
. Predict the output. Then check your prediction. Write your results/observations/what did you learn?SampleGreetable3.java
. Update the experiment to use SampleGreetable3
. Predict the output. Then check your prediction. Write your results/observations/what did you learn?SampleGreetable4.java
. Update the experiment to use SampleGreetable4
. Predict the output. Then check your prediction. Write your results/observations/what did you learn?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?
SampleGreetable5.java
. Update the experiment to use SampleGreetable5
. Predict the output. Then check your prediction. Write your results/observations/what did you learn?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.
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.
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.