0% found this document useful (0 votes)
4 views4 pages

Java Book Class and Comparators

Uploaded by

apiiulia70
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Java Book Class and Comparators

Uploaded by

apiiulia70
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Programming Patterns

Lab 1
import [Link];

public class Book implements Comparable<Book> {


protected String title;
protected String author;
protected int yearPublished;

public Book(String title, String author, int yearPublished) {


[Link] = title;
[Link] = author;
[Link] = yearPublished;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getYearPublished() {
return yearPublished;
}
public void setTitle(String title) {
[Link] = title;
}
public void setAuthor(String author) {
[Link] = author;
}
public void setYearPublished(int yearPublished) {
[Link] = yearPublished;
}
@Override
public String toString() {
return "Book:\n" +
"Title = " + title + '\n' +
"Author = " + author + '\n' +
"Year Published = " + yearPublished + '\n';
}
@Override
public int compareTo(Book o) {
return [Link]([Link]);
}
public int authorComparator(Book o) {
return [Link]([Link]);
}
public int yearComparator(Book o) {
return [Link]([Link], [Link]);
}
}
public interface BookComparable {
int compareTo(Book o);
}
import [Link];
public class AuthorComparator implements Comparator<Book> {
@Override
public int compare(Book b1, Book b2) {
return [Link]().compareTo([Link]());
}
}
import [Link];
public class YearComparator implements Comparator<Book> {
@Override
public int compare(Book b1, Book b2) {
return [Link]([Link](), [Link]());
}
}
import [Link].*
public class Main {
public static void main(String[] args) {
List<Book> books = new ArrayList<>();
[Link](new Book("Pride and Prejudice", "Jane Austen", 1813));
[Link](new Book("To Kill A Mockingbird", "Harper Lee", 1960));
[Link](new Book("1984", "George Orwell", 1949));

// Print all books


[Link]("All Books:");
[Link]([Link]::println);

// Find the most recent book (loop version)


Book newest = [Link](0);
for (Book b : books) {
if ([Link]() > [Link]()) {
newest = b;
}
}
[Link]("\nMost recently published book:");
[Link](newest);

// PART 2: Comparators

[Link]("\nSorted by title (natural order):");


[Link](books); // uses Comparable<Book>
[Link]([Link]::println);
[Link]("\nSorted by author:");
[Link](books, new AuthorComparator());
[Link]([Link]::println);

[Link]("\nSorted by year published (ascending):");


[Link](books, new YearComparator());
[Link]([Link]::println);

// PART 3: Lambdas

[Link]("\nSorted by title (lambda):");


[Link]([Link](Book::getTitle));
[Link]([Link]::println);
[Link]("\nSorted by author (lambda):");
[Link]([Link](Book::getAuthor));
[Link]([Link]::println);

[Link]("\nSorted by year published (descending, lambda):");


[Link]([Link](Book::getYearPublished).reversed());
[Link]([Link]::println);

// Find most recent book using stream


Book newestStream = [Link]()
.max([Link](Book::getYearPublished))
.get();
[Link]("\nMost recently published book (stream):");
[Link](newestStream);
}
}

You might also like