Question: I need help with this Java question. Please provide output as well so I can test this on CodeLab. Book.java import java.util.Objects; public class Book
I need help with this Java question. Please provide output as well so I can test this on CodeLab.
Book.java
import java.util.Objects;
public class Book { private String title, author; private int publicationYear; private double price;
public Book(String title, String author, int publicationYear, double price) { this.title = Objects.requireNonNull(title); this.author = Objects.requireNonNull(author); this.publicationYear = publicationYear; this.price = price; } }
BookTest.java
// DO NOT MODIFY THIS FILE.
public class BookTest { public static void main(String[] args) { System.out.println(new Book("The Illiad", "Homer", -700, 15.99) instanceof Comparable); System.out.println(new Book("The Illiad", "Homer", -700, 15.99).compareTo(new Book("A Tale of Two Cities", "Charles Dickens", 1859, 19.95)) 0); // expected: true System.out.println(new Book("The Hitchhiker's Guide to the Galaxy", "Douglas Adams", 1979, 9.99).compareTo(new Book("The Hitchhiker's Guide to the Galaxy", "Douglas Adams", 1979, 12.99)) 0); // expected: true System.out.println(new Book("To Kill a Mockingbird", "Harper Lee", 1960, 12.99).compareTo(new Book("Animal Farm", "George Orwell", 1960, 12.99)) > 0); // expected: true System.out.println(new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925, 14.99).compareTo(new Book("The Grapes of Wrath", "John Steinbeck", 1939, 14.99))
} }
HW 1 (review) > Book compa re To Deadline: 02/05/23 11:59pm EST Instructions Below is a simple Book class. Every book has a title, and author, a publication year, and a price. Make the Book class implement the Comparable interface. Two books should be compared as follows: - First, they should be compared numerically by publication year. - If the publication years are equal, the books should be compared numerically by price. - If the prices are also equal, the books should be compared alphabetically by author. - If the authors are also equal, the books should be compared alphabetically by title. Paste your solution in Book.java, below. The other file, BookTest.java, contains code to test your solution. You may copy the testing code, but do not modify it. Additional Notes: Regarding your code's standard output, CodeLab will check for case errors but will ignore whitespace (tabs, spaces, newlines) altogether
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
