$24
Learning Objectives
• Create your own method equals(Object o)
• Implement a parametrized interface
• Use the interface java.util.Comparator<T>
Section 1: the method equals
As you know, the class Object defines a method equals.
public class Object {
// ...
public boolean equals(Object obj) {
return this == other;
}
}
Since in Java, every class inherits from the class Object, every class inherits the method boolean equals(Object obj). This is true for the predefined classes, such as String and Integer, but also for any class that you will define.
The method equals is used to compare references variables. Consider two references, a and b, and the designated objects.
• If you need to compare the identity of two objects (do a and b designate the same object?), use "==" (i.e. a == b );
• If you need to compare the content of two objects (is the content of the two objects the same?), use the method "equals" (i.e. a.equals(b)).
In this first part, we are going to create a simple class Book. This class is quite minimal. It contains the following:
• An instance variable author of type String;
• An instance variable title of type String;
• An instance variable year of type int (the publication year of that book);
• A contructor Book(String author, String title, int year), which receives that new instance's author, title and publication year as paramters;
• A method toString(), which returns a String representation of that instance, using the format "author: title (year)";
• A method equals(Object o).
We want our implementation to be as robust as possible. In particular, the method equals should handle every particular case you can think about. Make sure your code passes all the provided JUnit tests.
Question 1.1:
Provide an implementation of the class Book. Use the provided template as a starting point.
Section 2: the interface java.util.Comparator<T>
In the previous laboratory, we have used the interface Comparable to create objects of type Post. A comparable object (implementing the Comparable interface) overrides the method compareTo and is capable of comparing itself with another object.
Unlike Comparable, a class implementing the interface Comparator is external to the element type we are comparing. Such a class must implement the methods compare and equals, to define and impose the sort order.
Sort methods such as Collections.sort or Arrays.sort take a instance of Comparator as a parameter.
For this exercise, we will implement an application for managing a library. The Library class will have a list of Book instances. We will be using the resizable-array implementation offered by the class ArrayList, which automatically increases the capacity as new elements are added to the list.
The idea is that the method sort of the class ArrayList will use the method compare of the comparator when sorting the elements of the list. Therefore, the method compare needs to handle elements that are of a type which is compatible with the type of the elements in the ArrayList.
Since Comparator is an interface, it needs a concrete implementation. Depending on the criterion for ordering, we can have multiple implementations through different classes and obtain different results when sorting.
Note that the class ArrayList is parameterized. In our case, we use ArrayList<Book>. Our ArrayList instance will hold instances of the class Book. Therefore, its method sort needs to compare instances of the Book class, and requires an instance of Comparator<Book> for this.
Thus, the class Library will have a list of Book instances stored in an instance of ArrayList. Using a Comparator, we want to be able to sort our library by authors first, then by title, then by year of publication (of course, in a real case we would want to have several Comparators and sort our library in different orders).
The class Library has the following methods:
• void addBook (Book b): adds a Book instance to the library;
• void sort(): sorts the books using a Comparator;
• void printLibrary(): prints all the book in their current order of the list.
Question 1.2:
Provide an implementation of the class Library as well as of BookComparator. Use the provided template as a starting point for the class Library and the provided template for BookComparator.