Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use the provided template as a starting point for the class Library and the provided template for BookComparator. import java.util.ArrayList; public class Library { private

image text in transcribed

Use the provided template as a starting point for the class Library and the provided template for BookComparator.

import java.util.ArrayList; public class Library { private ArrayList library = new ArrayList(); public Book getBook(int i) { return library.get(i); } public int getSize() { return library.size(); } public void addBook (Book b) { // Your code here } public void sort() { // Your code here } public void printLibrary() { // Your code here } }

import java.util.Comparator; public class BookComparator implements Comparator { // Implement the comparator method for books. } 

Section 2: the interface java.util.Comparator 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 compare To 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. 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 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions

Question

Roll out international HRM practices for franchisees.

Answered: 1 week ago