Problem 1 Implement the classes in the following class diagr

Problem 1: 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.

• 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.

• Write a method that returns the most expensive book in the array of objects (Books).

• The method signature is: public static Book max ( Book[] books)

• 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.

• Using Arrays.sort( Book[] books, new BookTitleComparator() ) to sort books by the title.

• Arrays.sort( books, new BookTitleComparator() );

• Create a method to show each element in the array with the following method signature:

• public static void showBooksArray ( books )

*Using Java*

Solution

public class Book implements Comparable<Book> {

   private int ISBN;
   private String title;
   private double cost;

   /**
   * @param iSBN
   * @param title
   * @param cost
   */
   public Book(int iSBN, String title, double cost) {
       ISBN = iSBN;
       this.title = title;
       this.cost = cost;
   }

   /**
   * @return the iSBN
   */
   public int getISBN() {
       return ISBN;
   }

   /**
   * @param iSBN
   * the iSBN to set
   */
   public void setISBN(int iSBN) {
       ISBN = iSBN;
   }

   /**
   * @return the title
   */
   public String getTitle() {
       return title;
   }

   /**
   * @param title
   * the title to set
   */
   public void setTitle(String title) {
       this.title = title;
   }

   /**
   * @return the cost
   */
   public double getCost() {
       return cost;
   }

   /**
   * @param cost
   * the cost to set
   */
   public void setCost(double cost) {
       this.cost = cost;
   }

   @Override
   public int compareTo(Book o) {
       // TODO Auto-generated method stub
       return (this.getCost() > o.getCost()) ? 1 : 0;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"Book [ISBN=\" + ISBN + \", title=\" + title + \", cost=\" + cost
               + \"]\";
   }

}

import java.util.Comparator;

public class BookTitleComparator implements Comparator<Book> {

   @Override
   public int compare(Book arg0, Book arg1) {
       // TODO Auto-generated method stub
       return arg0.getTitle().compareToIgnoreCase(arg1.getTitle());
   }

}

import java.util.Arrays;

public class BookTest {

   /**
   * @param args
   */
   public static void main(String[] args) {

       Book[] books = new Book[5];

       books[0] = new Book(3223, \"title6\", 2339.23);
       books[1] = new Book(3224, \"title2\", 2335.23);
       books[2] = new Book(3225, \"title7\", 2333.23);
       books[3] = new Book(3226, \"title4\", 2337.23);
       books[4] = new Book(3227, \"title5\", 2338.23);

       System.out.println(\"Before Sorting:\");
       showBooksArray(books);

       System.out.println(\"After Sorting:\");
       Arrays.sort(books);
       showBooksArray(books);

       System.out.println(\"After Sorting Using BookTitleComparator:\");
       Arrays.sort(books, new BookTitleComparator());
       showBooksArray(books);

       System.out.println(\"Costly book in the Array: \" + max(books));

   }

   /**
   * @param books
   */
   public static void showBooksArray(Book[] books) {

       for (int i = 0; i < books.length; i++) {
           System.out.println(books[i]);
       }

   }

   /**
   * @param books
   * @return
   */
   public static Book max(Book[] books) {

       Arrays.sort(books);
       return books[books.length - 1];

   }
}

OUTPUT:

Before Sorting:
Book [ISBN=3223, title=title6, cost=2339.23]
Book [ISBN=3224, title=title2, cost=2335.23]
Book [ISBN=3225, title=title7, cost=2333.23]
Book [ISBN=3226, title=title4, cost=2337.23]
Book [ISBN=3227, title=title5, cost=2338.23]
After Sorting:
Book [ISBN=3225, title=title7, cost=2333.23]
Book [ISBN=3224, title=title2, cost=2335.23]
Book [ISBN=3226, title=title4, cost=2337.23]
Book [ISBN=3227, title=title5, cost=2338.23]
Book [ISBN=3223, title=title6, cost=2339.23]
After Sorting Using BookTitleComparator:
Book [ISBN=3224, title=title2, cost=2335.23]
Book [ISBN=3226, title=title4, cost=2337.23]
Book [ISBN=3227, title=title5, cost=2338.23]
Book [ISBN=3223, title=title6, cost=2339.23]
Book [ISBN=3225, title=title7, cost=2333.23]
Costly book in the Array: Book [ISBN=3223, title=title6, cost=2339.23]

Problem 1: Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable in the class defi
Problem 1: Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable in the class defi
Problem 1: Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable in the class defi
Problem 1: Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable in the class defi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site