Question

Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comp
Book MyDate title: String author : Arraylists Authors o 1 MyDate from publishing: MyDate price: Double Bookf title String aut
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[]l 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 )
Book MyDate title: String author : Arraylists Authors o 1 MyDate from publishing: MyDate price: Double Bookf title String author Author, publiching: MyDate, price: Doublel set Titlel title: String): void + setAuthorf title: String): voild setDatel date: MyDate ): void getDate): MyDate + setPrice price: int): void getPrice 0:int e compareTo( book:Book):int toString):String Author name: String age : int book: ArrayList Book Authorl name:String, age: int) +getName0: String eweak void getAge0 : String getBookf title: String): Book +setBookf book : Book ): void compare (book1:Book, book2 Book): int Class MyDate should have member of type Date and provide methods and constructor to allow User to create object of myDate
1 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// MyDate.java

public class MyDate {
   private int day;
   private int month;
   private int year;

   public MyDate(int day, int month, int year) {
       this.day = day;
       this.month = month;
       this.year = year;
   }

   @Override
   public String toString() {
       return month + "/" + day + "/" + year;
   }

}
______________________

// Author.java

import java.util.ArrayList;

public class Author {
   private String name;
   private int age;
   private ArrayList<Book> book;

   public Author(String name, int age) {
       this.name = name;
       this.age = age;
       book=new ArrayList<Book>();
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public int getAge() {
       return age;
   }

   public void setAge(int age) {
       this.age = age;
   }

   public Book getBook(String title) {
       Book b = null;
       for (int i = 0; i < book.size(); i++) {
           if (book.get(i).equals(title))
               b = book.get(i);
       }
       return b;
   }

   public void setBook(Book book) {
       this.book.add(book);
   }

   @Override
   public String toString() {
       return "Author [name=" + name + ", age=" + age + ", book=" + book + "]";
   }

}
__________________________

// Book.java

import java.util.ArrayList;
import java.util.Date;

public class Book implements Comparable<Book> {
   private String title;
   private double price;
   private ArrayList<Author> author;
   private MyDate publishing;

   public Book(String title, Author author, MyDate publishing, double price) {
       this.author = new ArrayList<Author>();
       this.title = title;
       this.price = price;
       this.author.add(author);
       this.publishing = publishing;
   }

   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public double getPrice() {
       return price;
   }

   public void setPrice(double price) {
       this.price = price;
   }

   public ArrayList<Author> getAuthor() {
       return author;
   }

   public void setAuthor(ArrayList<Author> author) {
       this.author = author;
   }

   public MyDate getDate() {
       return publishing;
   }

   public void setDate(MyDate publishing) {
       this.publishing = publishing;
   }

   @Override
   public int compareTo(Book o) {
       if (getPrice() == o.getPrice())
           return 0;
       else if (getPrice() > o.getPrice())
           return 1;
       else
           return -1;

   }

   @Override
   public String toString() {
       return "Book [Title=" + title + ", Price=$" + price + ", Author="
               + author + ", Publishing=" + publishing + "]";
   }

}
______________________________

// BookTitleComparator.java

import java.util.Comparator;

public class BookTitleComparator implements Comparator<Book> {

   @Override
   public int compare(Book a, Book b) {

       return a.getTitle().compareTo(b.getTitle());

   }

}
_________________________

// Test.java

package org.students;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);
       Book books[] = new Book[10];
       books[0] = new Book("Introduction to java", new Author(
               "Yeshwanth Kanitkar", 23), new MyDate(31, 12, 2000), 43.50);

       books[1] = new Book("Introduction to c", new Author(
               "Yeshwanth Kanitkar", 23), new MyDate(21, 11, 2002), 23.50);
       books[2] = new Book("Wings of fire", new Author("Abdul kalam", 75),
               new MyDate(22, 02, 2006), 55.60);
       books[3] = new Book("Anci C", new Author("Balaguruswamy", 56),
               new MyDate(04, 03, 2001), 65.00);
       books[4] = new Book("Letting Go", new Author("Philip Roth", 49),
               new MyDate(03, 01, 2005), 16.50);
       books[5] = new Book("Fear of Flying", new Author("Erica Jong", 45),
               new MyDate(02, 11, 2007), 26.50);
       books[6] = new Book("Wide Sargasso Sea", new Author("Jean Rhys", 40),
               new MyDate(12, 9, 2009), 23.50);
       books[7] = new Book("When Breath Becomes Air", new Author(
               "Paul Kalanithi", 34), new MyDate(11, 12, 2007), 27.50);
       books[8] = new Book("Genisis", new Author("Wanick", 39), new MyDate(12,
               12, 2012), 34.50);
       books[9] = new Book("A Girl Like I", new Author("Anita Loos", 37),
               new MyDate(11, 02, 2010), 44.50);

       Collections.sort(Arrays.asList(books));
       showBooksArray(books);

       Book bmax = max(books);
       System.out.println("\nMost Expensive Book is :$" + bmax);

       Collections.sort(Arrays.asList(books), new BookTitleComparator());
       System.out.println("\nDisplaying the books info after sorting based on title:");
       showBooksArray(books);
   }

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

   }

   private static Book max(Book[] books) {
       double max = books[0].getPrice();
       int indx = 0;
       for (int i = 0; i < books.length; i++) {
           if (max < books[i].getPrice()) {
               max = books[i].getPrice();
               indx = i;
           }
       }
       return books[indx];
   }

}

____________________________

Output:

Book [Title=Letting Go, Price=$16.5, Author=[Author [name=Philip Roth, age=49, book=[]]], Publishing=1/3/2005]
Book [Title=Introduction to c, Price=$23.5, Author=[Author [name=Yeshwanth Kanitkar, age=23, book=[]]], Publishing=11/21/2002]
Book [Title=Wide Sargasso Sea, Price=$23.5, Author=[Author [name=Jean Rhys, age=40, book=[]]], Publishing=9/12/2009]
Book [Title=Fear of Flying, Price=$26.5, Author=[Author [name=Erica Jong, age=45, book=[]]], Publishing=11/2/2007]
Book [Title=When Breath Becomes Air, Price=$27.5, Author=[Author [name=Paul Kalanithi, age=34, book=[]]], Publishing=12/11/2007]
Book [Title=Genisis, Price=$34.5, Author=[Author [name=Wanick, age=39, book=[]]], Publishing=12/12/2012]
Book [Title=Introduction to java, Price=$43.5, Author=[Author [name=Yeshwanth Kanitkar, age=23, book=[]]], Publishing=12/31/2000]
Book [Title=A Girl Like I, Price=$44.5, Author=[Author [name=Anita Loos, age=37, book=[]]], Publishing=2/11/2010]
Book [Title=Wings of fire, Price=$55.6, Author=[Author [name=Abdul kalam, age=75, book=[]]], Publishing=2/22/2006]
Book [Title=Anci C, Price=$65.0, Author=[Author [name=Balaguruswamy, age=56, book=[]]], Publishing=3/4/2001]

Most Expensive Book is :$Book [Title=Anci C, Price=$65.0, Author=[Author [name=Balaguruswamy, age=56, book=[]]], Publishing=3/4/2001]

Displaying the books info after sorting based on title:
Book [Title=A Girl Like I, Price=$44.5, Author=[Author [name=Anita Loos, age=37, book=[]]], Publishing=2/11/2010]
Book [Title=Anci C, Price=$65.0, Author=[Author [name=Balaguruswamy, age=56, book=[]]], Publishing=3/4/2001]
Book [Title=Fear of Flying, Price=$26.5, Author=[Author [name=Erica Jong, age=45, book=[]]], Publishing=11/2/2007]
Book [Title=Genisis, Price=$34.5, Author=[Author [name=Wanick, age=39, book=[]]], Publishing=12/12/2012]
Book [Title=Introduction to c, Price=$23.5, Author=[Author [name=Yeshwanth Kanitkar, age=23, book=[]]], Publishing=11/21/2002]
Book [Title=Introduction to java, Price=$43.5, Author=[Author [name=Yeshwanth Kanitkar, age=23, book=[]]], Publishing=12/31/2000]
Book [Title=Letting Go, Price=$16.5, Author=[Author [name=Philip Roth, age=49, book=[]]], Publishing=1/3/2005]
Book [Title=When Breath Becomes Air, Price=$27.5, Author=[Author [name=Paul Kalanithi, age=34, book=[]]], Publishing=12/11/2007]
Book [Title=Wide Sargasso Sea, Price=$23.5, Author=[Author [name=Jean Rhys, age=40, book=[]]], Publishing=9/12/2009]
Book [Title=Wings of fire, Price=$55.6, Author=[Author [name=Abdul kalam, age=75, book=[]]], Publishing=2/22/2006]


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other);...

    JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other); } A Film class is defined as public class Film {      private String title;      private int yearOfRelease;           public Film(String title, int yearOfRelease) {           super();           this.title = title;           this.yearOfRelease = yearOfRelease;      }           public void display()      { System.out.println("Title " + title +". Release" + yearOfRelease);      } } Rewrite the Film class so that it...

  • What is a Java interface? It is a __________ ? base class derived class set of...

    What is a Java interface? It is a __________ ? base class derived class set of instance variables specification of required methods, including method names, signatures, and return values What is the required method of the Comparable interface? comparator compare compareTo spaceship What is the load factor for a HashMap collection? It is the maximum ___________ of the HashMap collection. capacity number of empty cells percentage of empty cells percentage of occupied cells Which choice defined a HashMap with integer...

  • in java Define a class named ComparableNames thats extends Person and implements comparable Define a class...

    in java Define a class named ComparableNames thats extends Person and implements comparable Define a class named ComparableNames that extends Person (defined below) and implements Comparable. Override the compare To method to compare the person on the basis of name. Write a test class to find the order of two ComparableNames. class Person String name; Person00 Person(String n) name=n: Hint: in class ComparableName, you only need a constructor with arg to initialize the data filed defined in the super class...

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

  • Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same...

    Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same way it is used in the GeneralizedSelectionSort. You should use the attached ComparableDemo to test your program. public class ComparableDemo { public static void main(String[] args) { Double[] d = new Double[10]; for (int i = 0; i < d.length; i++) d[i] = new Double(d.length - i); System.out.println("Before sorting:"); int i; for (i = 0; i < d.length; i++) System.out.print(d[i].doubleValue( ) + ", ");...

  • Create a data class named Automobile that implements the Comparable interface. Give the class data fields...

    Create a data class named Automobile that implements the Comparable interface. Give the class data fields for make, model, year, and price. Then add a constructor, all getters, a toString method that shows all attribute values, and implement Comparable by using the year as the criterion for comparing instances. Write a program named TestAutos that creates an ArrayList of five or six Automobiles. Use a for loop to display the elements in the ArrayList. Sort the Arraylist of autos by...

  • public class Item implements Comparable { private String name; private double price; /** * Constructor for...

    public class Item implements Comparable { private String name; private double price; /** * Constructor for objects of class Item * @param theName name of item * @param thePrice price of item */ public Item(String theName, double thePrice) { name = theName; price = thePrice; }    /** * gets the name * @return name name of item */ public String getName() { return name; }    /** * gets price of item * @return price price of item */...

  • Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The...

    Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The clone method must allow a deep copy on the students field. In addition, add the equals(Object o) and the toString() methods. Write a test program to invoke the compareTo, clone, equals, and toString methods in a meaningful way. Below is the Listing from the book that needs to be rewritten public class Course {    private String courseName;    private String[] students = new...

  • Can someone help me with the main class of my code. Here is the assignment notes....

    Can someone help me with the main class of my code. Here is the assignment notes. Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook. Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When...

  • In the USIntsArrayList Class, implement the USIntsArrayListInterface Interface (which is really just the UnSortedInts Class without...

    In the USIntsArrayList Class, implement the USIntsArrayListInterface Interface (which is really just the UnSortedInts Class without the logic. All you are doing in this class is providing the same functionality as UnSortedInts, but using an ArrayList to hold the data. Document appropriately Thank you, package arrayalgorithms; import java.util.ArrayList; /** * Title UnSorted Ints stored in an Array List * Description: Implement all the functionality of the UnSortedInts * class using an ArrayList * @author Khalil Tantouri */ public class USIntsArrayList...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT