Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Some of this is correct but not all, having trouble with the return and year. Ec

ID: 3792440 • Letter: S

Question

Some of this is correct but not all, having trouble with the return and year. Eclipse tells me that year is primative type and I can not call it like this. The program as a whole reads in a user input and then searches a list to find matches. If one exists it goes through the steps of matching author name and date to not have repeats.

* This method compares an instance of this Book, with another instance of
* Book. It should not consider genres.
* <P>
* Algorithm:<br>
* ????
* </P>
*
* @param obj
*            The object to which we are comparing this instance of Book
* @return ?????????
*/
public int compareTo(Book book) {
  // TODO Add the code for compareTo() here
  
  int result = title.compareTo(book.title);{
   if (result == 0);
    result = authorName.compareTo(book.authorName);
    return result;
   if (result == 0);
    result = year.compareTo(year);
    return result;
   }
   

// TODO Make sure you replace this return statement
  return;

Explanation / Answer

//Changes are in bold to comapre year

/**
* The book class that compars the two book
* objects by year.
* */
//Book.java
import java.lang.Comparable;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Comparator;
public class Book implements Comparable<Book>
{
   public String title;
   public String author;
   public int year;

   public Book(String title, String author,int year) {
       this.title = title;
       this.author = author;
       this.year=year;
   }

   public String getTitle() {
       return this.title;
   }

   public String getAuthor() {
       return this.author;
   }
  
   public int getYear() {
       return this.year;
   }
  
//This code sinippet that compares the book by autor,title and year as well
   /**
   * The method compareTo that compares the book
   * by titlt,author and year
   * */
   public int compareTo(Book other)
   {
       int comparison = this.title.compareTo(other.title);
       if (comparison != 0)
       {
           return comparison;
       }

       comparison= this.author.compareTo(other.author);

       if(comparison!=0)
           return comparison;
       else
           //return 1 if year is same otherwise 0
           return this.year==other.year? 1:0;
   }

   //yearComparator that compares the year of Book objects
   public static final Comparator<Book> yearComparator = new Comparator<Book>()
           {
       public int compare(Book a, Book b)
       {
           //Convert the primitive type to wrapper class
           Integer inta=new Integer(a.year);
           Integer intb=new Integer(b.year);
           //then call compareTo method
           return inta.compareTo(intb);
       }
   };

}

//Answered as per your question

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote