Name______________________________This lab covers basic concepts in the use of interfaces. You should print a copy of this lab so you can record your answers to questions. Turn in the completed questions and any supporting printouts as required in the lab.
Interfaces are incredibly useful to ensure that certain classes will include specific methods. We will use two standard interfaces in this lab to ensure that certain objects can be compared (as in a less than, equal to, and greater relationship). Java includes interfaces for this purpose; you should lookup the Comparable and Comparator interfaces in the API documentation. You can also read about these in the text (pages 1019 and 1075).
According to the Comparable interface, if you need to modify an existing Coin class so that it realizes (implements) the Comparable interface, what methods do you need to implement? Give the method signatures, that is, the return types, the method names, and the method parameters.
Answer:
The compareTo method compares two parameters, the implicit parameter (this) and explicit parameter. The call
a.compareTo(b)
returns
Start a project for this lab. Add the Coin class. Add the necessary implements clause and implement the compareTo method of the Coin class so that it compares the numerical values of two coins. You will need to cast the Object parameter of this method to a Coin to accomplish this task.
The sort method of the Collections class can sort a list of objects of classes that implement the Comparable interface. Using this outline, write a test program in a class named TestSort that sorts a list of five Coins: one penny, two nickels, one dime, and one quarter.
What is the outcome of executing your test program?
Answer:
Change your compareTo method so it returns -1 where it used to return +1 and returns +1 where it used to return -1. Recompile and run the test program again. What is the outcome of executing your test program? Explain the changed output.
Answer:
Print the two java files you created. You will attach them to this report with a staple.
Copy the test program you created above to a new file. You can place it in a different folder, or change its name (this will require changing the name of the class as well to something like RectangleSort). Make changes so it sorts Rectangle objects. You may need to import the Rectangle class (java.awt.Rectangle). You can find it in the API. What compile error (related to sorting the rectangles) do you get? What is the reason for the error message?
Answer:
Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The Rectangle class is part of the standard library, and you cannot modify library classes. Fortunately, there is a second sort method that you can use to sort a list of objects of any class, even if the class doesn't implement the Comparable interface. This approach requires the use of a Comparator (java.util.Comparator). Comparators compare two objects passed as arguments.
Comparator comp = . . .; Collections.sort(list, comp);
Comparator is an interface. Therefore, comp must be constructed as an object of some class that implements the Comparator interface. The object so constructed is usually nothing more than an empty shell to hold a method or two. According to the API, what methods are required to implement this interface? Give the method signatures. (Hint: Look up the Comparator interface in the API documentation.)
Answer:
Create a class named RectangleAreaComparator (in its own file) whose compare method compares two rectangles. This class will start like this:
public class RectangleAreaComparator implements Comparator<Rectangle> {
Don't forget the imports! The compare method should return
The parameters to the compare method must be of type Rectangle. Fix the test program so it constructs a rectangle comparator, sorts the list, and prints the sorted list. What is the output of the test program?
Answer:
Suppose you sometimes wanted to sort Rectangles according to the length of their perimeters (instead of areas). Explain how you could do this in the same program (sometimes sorting by area, sometimes by perimeter):
Answer:
Based on the above solution, what advantage does Comparator have over Comparable?
Answer:
Print the two java files you created. You will attach them to this report with a staple.
You should have 4 printed files attached to this lab report. Be sure your name is on each page and that they are securely attached with a staple.