Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how to structure this piece of code out private Author author; private String isbn, title; private int pages; public int compareTo(Book another) The compareTo method

 how to structure this piece of code out…

private Author author;

private String isbn, title;

private int pages;


public int compareTo(Book another)

The compareTo method that compares the author first, title second, pages third, and lastly isbn of the book

Specified by:

compareTo in interface Comparable<Book>

Parameters:

another - The book object that is being compared to

Throws:

IllegalArgumentException - if the parameter is null



public int compareTo(Book another){
if(another == null){
throw new IllegalArgumentException();
}
if(this.author.compareTo(another.author) == 0){
if(this.title.compareTo(another.title) == 0) {
if (this.pages.compareTo(another.pages) == 0)
return this.isbn.compareTo(another.isbn);
}else{
return this.pages.compareTo(another.pages)
}else{
return this.title.compareTo(another.title);
}
}else{
return this.author.compareTo(another.author);
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building Java Programs A Back To Basics Approach

Authors: Stuart Reges, Marty Stepp

5th Edition

013547194X, 978-0135471944

More Books

Students also viewed these Programming questions