Implement the classes in the following class diagram. The Book class implements
ID: 3860032 • Letter: I
Question
Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[] books ) from the java.util package to sort the array. The order of objects in the array is determined using compareTo(...) method. 2. Write a method that returns the most expensive book in the array of objects (Books) o The method signature is: public static Book max (Book] books) 3. Create another class BookTitleComparator that implements the Comparator interface in which overrides the compare( Book a, Book b) method. Use the implements Comparator in the class definition in order to compare objects of type Book. The compare(Book a, Book b) method should compare the titles of the two books. 4. Using Arrays.sort( Book[] books, new BookTitleComparator() to sort books by the title. o Arrays.sort( books, new BookTitleComparator()); 5. Create a method to show each element in the array with the following method signature: o public static void showBooksArray ( books )Explanation / Answer
import java.util.ArrayList;
import java.util.Arrays;
/**
* @author Aniket
*
*/
public class Book implements Comparable<Book> {
String title;
ArrayList<Author> author;
MyDate publishing;
double price;
/**
* @param title
* @param author
* @param publishing
* @param price
*/
public Book(String title, ArrayList<Author> author, MyDate publishing,
double price) {
this.title = title;
this.author = author;
this.publishing = publishing;
this.price = price;
}
/**
* @param title
* the title to set
*/
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
/**
* @param author
* the author to set
*/
public void setAuthor(Author author) {
this.author.add(author);
}
/**
* @return the author
*/
public ArrayList<Author> getAuthor() {
return author;
}
/**
* @return the publishing
*/
public MyDate getPublishing() {
return publishing;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param publishing
* the publishing to set
*/
public void setPublishing(MyDate publishing) {
this.publishing = publishing;
}
/**
* @param price
* the price to set
*/
public void setPrice(double price) {
this.price = price;
}
@Override
public int compareTo(Book o) {
// TODO Auto-generated method stub
if (this.getPrice() > o.getPrice())
return -1;
else
return 1;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Book [title=" + title + ", price=" + price + "]";
}
public static void main(String[] args) {
Book[] books = new Book[10];
// randomly assign some data to books array
for (int i = 0; i < books.length; i++) {
Book b = new Book("title" + (i + 1), null, null, 5000 * (i + 2));
books[i] = b;
}
// getting max price of books
System.out.println("Max Price of a book:" + max(books).getPrice());
// After sorting by title
System.out.println("After sorting by title");
Arrays.sort(books, new BookTitleComparator());
showBooksArray(books);
}
/**
* return maxprice book
*
* @param books
* @return
*/
public static Book max(Book[] books) {
Arrays.sort(books);
return books[0];
}
/**
* method to show the books
*
* @param books
*/
public static void showBooksArray(Book[] books) {
for (int i = 0; i < books.length; i++) {
System.out.println(books[i].toString());
}
}
}
import java.util.ArrayList;
public class Author {
String name;
int age;
ArrayList<Book> book;
/**
* @param name
* @param age
*/
public Author(String name, int age) {
this.name = name;
this.age = age;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @return the book
*/
public Book getBook(String title) {
for (int i = 0; i < this.book.size(); i++) {
Book b = this.book.get(i);
}
return null;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param age
* the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @param book
* the book to set
*/
public void setBook(Book book) {
this.book.add(book);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Author [name=" + name + ", age=" + age + ", book=" + book + "]";
}
}
public class MyDate {
int day,month,year;
/**
* @param day
* @param month
* @param year
*/
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
/**
* @return the day
*/
public int getDay() {
return day;
}
/**
* @return the month
*/
public int getMonth() {
return month;
}
/**
* @return the year
*/
public int getYear() {
return year;
}
/**
* @param day the day to set
*/
public void setDay(int day) {
this.day = day;
}
/**
* @param month the month to set
*/
public void setMonth(int month) {
this.month = month;
}
/**
* @param year the year to set
*/
public void setYear(int year) {
this.year = year;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "MyDate [day=" + day + ", month=" + month + ", year=" + year
+ "]";
}
}
import java.util.Comparator;
public class BookTitleComparator implements Comparator<Book> {
/*
* (non-Javadoc)
*
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
@Override
public int compare(Book o1, Book o2) {
// TODO Auto-generated method stub
if (o1.getTitle().equals(o2.getTitle()))
return 1;
else
return -1;
}
}
OUTPUT:
Max Price of a book:55000.0
After sorting by title
Book [title=title1, price=10000.0]
Book [title=title2, price=15000.0]
Book [title=title3, price=20000.0]
Book [title=title4, price=25000.0]
Book [title=title5, price=30000.0]
Book [title=title6, price=35000.0]
Book [title=title7, price=40000.0]
Book [title=title8, price=45000.0]
Book [title=title9, price=50000.0]
Book [title=title10, price=55000.0]