1. Define a class named Book that contains:
An int data field named pages that stores the number of pages in
the book.
A String data field named title that represents the title of the
book.
A constructor with parameters for initializing pages and
title.
The getter and setter methods for all data fields.
A toString method that returns book information, including the
book title and pages.
The equals method that returns true if two books have the same
title and the same number of pages.
The compareTo method that compares two books and returns -1 if
the first book has less pages than the second one, 1 if the first
book has more pages than the second one, and 0 if both books have
same number of pages.
Source code of the program and its working are given below.Comments are also given along with the code for better understanding.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Working of the program
Source code
Book class
//Book class definition public class Book { //instance variable declaration private int pages; String title; //Constructor to initialize instance variables public Book(int pages, String title) { this.pages = pages; this.title = title; } //getter method for getting values public int getPages() { return pages; } public String getTitle() { return title; } //setter method for setting values public void setPages(int pages) { this.pages = pages; } public void setTitle(String title) { this.title = title; } //overriding toString() method to print object information directly @Override public String toString() { return "\nBook title: "+title+"\nNumber of pages: "+pages; } //overriding equals() to check objects are equal or not @Override public boolean equals(Object obj) { //if the object compared with itself if(obj==this) { return true; } //if object is not the instance of Book if(!(obj instanceof Book)) { return false; } //typecast obj to Book for comparing their data members Book b=(Book)obj; //compare the data members return Integer.compare(pages,((Book) obj).pages)==0 &&title.equals(((Book) obj).title); } //compareTo() method is used to compare pages public int compareTo(Book ob) { //if first book's pages less than second one's if(pages<ob.pages) return -1; //if first book's pages greater than second one's else if(pages>ob.pages) return 1; //if pages of both are equal else return 0; } }
Main class
public class Main { public static void main(String[] args) { //creating Book objects Book b1 = new Book(200, "Learning Java"); Book b2 = new Book(150, "python Introduction"); Book b3 = new Book(200, "Learning Java"); //printing their information System.out.println("Book information: "); System.out.println(b1); System.out.println(b2); System.out.println(b3); System.out.println(); //checking objects are equal or not System.out.println(b1.equals(b2)); System.out.println(b1.equals(b3)); //comparing pages of two books System.out.println(b1.compareTo(b2)); } }
Screen shot of the code
Screen shot of the code
Get Answers For Free
Most questions answered within 1 hours.