Untitled
unknown
plain_text
19 days ago
2.3 kB
3
Indexable
Never
import java.util.Scanner; class Book { private int ISBN; private String bookTitle; private int numberOfPages; private int count; public Book(int ISBN, String bookTitle, int numberOfPages) { this.ISBN = ISBN; this.bookTitle = bookTitle; this.numberOfPages = numberOfPages; this.count = 0; } public Book() { this(0, "", 0); } public String toString() { return "ISBN: " + ISBN + ", Title: " + bookTitle + ", Pages: " + numberOfPages + ", Count: " + count; } public int compareTo(Book other) { if (this.numberOfPages > other.numberOfPages) { return 1; } else if (this.numberOfPages < other.numberOfPages) { return -1; } else { return 0; } } public int getCount() { return count; } public int getNumberOfPages() { return numberOfPages; } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Book[] books = new Book[5]; for (int i = 0; i < 5; i++) { System.out.println("Enter details for Book " + (i + 1) + ":"); System.out.print("ISBN: "); int isbn = scanner.nextInt(); scanner.nextLine(); // Consume the newline character System.out.print("Title: "); String title = scanner.nextLine(); System.out.print("Number of Pages: "); int pages = scanner.nextInt(); books[i] = new Book(isbn, title, pages); } // Display all books displayAll(books); // Compare two books based on their pages System.out.println("Comparison result: " + books[0].compareTo(books[1])); // Demonstrate isHeavier method System.out.println("Is book 1 heavier? " + isHeavier(books[0])); } public static void displayAll(Book[] books) { System.out.println("All Books:"); for (Book book : books) { System.out.println(book.toString()); } } public static boolean isHeavier(Book book) { return book.getNumberOfPages() > 500; } }