Question
Hey coding peeps, I'm working on an example for CSC3 and having a really hard time implementing a comparator class. Below I've pasted the code
Hey coding peeps, I'm working on an example for CSC3 and having a really hard time implementing a comparator class. Below I've pasted the code and you'll see some commented out comparator classes I've tried to implement. This isn't graded or anything just and example I'd like to be able to do. THANKS!
package book;
import java.util.*; import java.util.ArrayList;
public class Book implements Comparable
public Book(String author, String title, double price) { this.author = author; this.title = title; this.price = price; // this.releaseDate=releaseDate; }
public String toString() { return "Book(" + title + "," + author + ", $" + price + ")"; } public int compareTo(Book s){ return this.author.compareTo(s.author); } /* * I'd like for this to be a compartoer class which will compare the titles of books */ public int compareAuthor(Book s){ return this.title.compareTo(s.title); } /* * I'd like for this comparor class to compare the prices of book (double) */ class BookComparator2 implements Comparator
//Your Code for default comparison, // Alphabetic comparison by author //class testBook { public static void main(String[] args) { Book b1 = new Book("dad", "lorem", 7.99); Book b2 = new Book("goog", "ipsum", 2.33); Book b3 = new Book("frank", "aalor", 5.99); Book b4 = new Book("steve", "amet", 123.00); ArrayList
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started