Recap - Custom Sorting
Part of the Object Oriented Programming section of Coddy's Java journey — lesson 46 of 87.
Challenge
EasyLet's build a book collection manager that brings together everything you've learned about special methods and sorting. You'll create a Book class with proper toString(), equals(), hashCode(), and natural ordering via Comparable, plus external comparators for flexible sorting options.
You'll organize your code across four files:
Book.java: Create a class representing a book with three private fields:title(String),author(String), andyear(int). Include a constructor and getters for all fields.Your Book should implement
Comparable<Book>with natural ordering byyearin ascending order (oldest first).Override
toString()to return:[title] by [author] ([year])Override
equals()so two books are equal when they have the sametitleANDauthor. OverridehashCode()usingObjects.hash()with the same fields.TitleComparator.java: Create a comparator that sorts books alphabetically by title in ascending order (A to Z).AuthorComparator.java: Create a comparator that sorts books alphabetically by author in ascending order (A to Z).Main.java: Bring everything together! You'll receive input for three books, each on a separate line in the format:title,author,yearCreate an
ArrayListof books and demonstrate all three sorting approaches:- Sort using the natural ordering (by year) and print each book
- Print an empty line
- Sort by title using
TitleComparatorand print each book - Print an empty line
- Sort by author using
AuthorComparatorand print each book
You will receive three lines of input, each containing book data in the format: title,author,year
For example: 1984,George Orwell,1949
Remember to import java.util.Objects in Book.java, java.util.Comparator in your comparator files, and java.util.ArrayList, java.util.Collections, and java.util.Scanner in Main.java.
Try it yourself
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read three books from input
String line1 = scanner.nextLine();
String line2 = scanner.nextLine();
String line3 = scanner.nextLine();
// TODO: Parse each line to extract title, author, and year
// Hint: Use split(",") to separate the values
// TODO: Create Book objects from the parsed data
// TODO: Create an ArrayList and add all three books
// TODO: Sort using natural ordering (by year) and print each book
// TODO: Print an empty line
// TODO: Sort by title using TitleComparator and print each book
// TODO: Print an empty line
// TODO: Sort by author using AuthorComparator and print each book
}
}All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System