Use the following file BookRunnerjava Code the file Bookjava

Use the following file:

BookRunner.java

Code the file Book.java:

Write a class Book. A Book has a title and genre (the kind of book it is. Implement the Comparable interface. Books are ordered by genre in alphabetical order. If two Books have the same genre, they are ordered alphabetically be title. Provide Javadoc When you implement a method from an interface, you can add @override above the method and Javadoc will use the Javadoc provided in the interface implementation

Solution

BookRunner.java

import java.util.ArrayList;
import java.util.Collections;
/**
* Tester for Book class
*/
public class BookRunner
{
public static void main(String[] args)
{
ArrayList<Book> inventory = new ArrayList<Book>();
inventory.add(new Book(\"Hamlet\", \"play\"));
inventory.add(new Book(\"Beekeeper\'s Apprentice\", \"mystery\"));
inventory.add(new Book(\"Big Java\", \"non-fiction\"));
inventory.add(new Book(\"Beekeeper\'s Apprentice\", \"mystery\"));
inventory.add(new Book(\"Bad Business\", \"mystery\"));
inventory.add(new Book(\"Violet the Pilot\",\"juvenile fiction\" ));

Collections.sort(inventory);
for (Book b: inventory)
{
System.out.printf(\"%-18s%s%n\",b.getGenre(), b.getTitle());
}

}
}

Book.java


public class Book implements Comparable<Book>{
   private String genre;
   private String title;
   public Book(String t, String g){
       title = t;
       genre=g;
   }
   public String getGenre() {
       return genre;
   }
   public void setGenre(String genre) {
       this.genre = genre;
   }
   public String getTitle() {
       return title;
   }
   public void setTitle(String title) {
       this.title = title;
   }
public int compareTo(Book b)
{
if(getGenre().compareTo(b.getGenre())== 0){
   return getTitle().compareTo(b.getTitle());
}
else{
   return getGenre().compareTo(b.getGenre());
}
}

}

OUtput:

juvenile fiction Violet the Pilot
mystery Bad Business
mystery Beekeeper\'s Apprentice
mystery Beekeeper\'s Apprentice
non-fiction Big Java
play Hamlet

Use the following file: BookRunner.java Code the file Book.java: Write a class Book. A Book has a title and genre (the kind of book it is. Implement the Compara
Use the following file: BookRunner.java Code the file Book.java: Write a class Book. A Book has a title and genre (the kind of book it is. Implement the Compara

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site