Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this Java question. Please provide the final code and output if possible. Part 4: Book Comparators Suppose we have the following

I need help with this Java question. Please provide the final code and output if possible.

Part 4: Book Comparators

Suppose we have the following Name and Book classes, which you should not modify: class Name {

private String first, last;

public Name(String first, String last) {

this.first = first;

this.last = last;

}

public String getFirst() {

return first;

}

public String getLast() {

return last;

}

@Override

public String toString() {

return first + " " + last;

}

}

class Book {

private String title;

private Name author;

private double rating;

private int publicationYear;

public Book(String title, Name author, double rating, int publicationYear) {

this.title = title;

this.author = author;

this.rating = rating;

this.publicationYear = publicationYear;

}

public String getTitle() {

return title;

}

public Name getAuthor() {

return author;

}

public double getRating() {

return rating;

}

public int getPublicationYear() {

return publicationYear;

}

@Override public String toString() {

return title + " " + author + " " + rating + " " + publicationYear;

}

}

Suppose we have a Book[] named books. For each of the following sorting orders, write the line(s) of code needed to sort the elements of books from lowest to highest.

1.) Ordered by publication year.

2.) Ordered by rating and then by publication year.

3.) Ordered by author last name and then by author first name.

4.) Ordered by rating, then by publication year, then by author last name, and then by author first name.

5.) Ordered by rating, then by publication year, then by author last name, then by author first name, and then by title.

Hint: Use Arrays.sort. Send it the array and a Comparator. To create a Comparator, you can use Comparator.comparing and thenComparing. For example, to create a Comparator that compares books by their publication year, then by rating, and then by author last name, we can do this:

Comparator c =

Comparator.comparing(Book::getPublicationYear)

.thenComparing(Book::getRating)

.thenComparing(b -> b.getAuthor().getLast());

You do not have to use Comparator.comparing and thenComparing, but doing so typically makes your code more concise and readable.

Additionally, repeat at least one of the above five sortings, now assuming that books is a List instead of a Book[]. Use Collections.sort, or the sort instance method of books (found in the List interface).

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

More Books

Students also viewed these Databases questions

Question

create a semiotic sign system to communicate an idea.

Answered: 1 week ago