Question

1. Define a class named Book that contains:  An int data field named pages that...

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.

Homework Answers

Answer #1
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

  • In Book class two instance variables pages and titles are declared
  • A parametrized constructor is used to initialize variables
  • getter and setter methods are defined to get and set values
  • toString() method of Object class is overridden to print object information directly
  • equals() method of Object class is overriden to check objects are equal or not
  • compareTo() method is defined to compare pages of two books
  • Inside Main class, Book objects are created and their information are printed
  • By caling equals() method, objects are checked equal or not
  • By calling compareTo() method,objects pages are compared.

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...
c++ Design a class named Account that contains (keep the data fields private): a) An int...
c++ Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate. f) A function named getMonthlyInterestRate() that...
For the following class Book: 1- Write a default constructor to give default values (title= Intro...
For the following class Book: 1- Write a default constructor to give default values (title= Intro to Programming and price = 20) to the class’s variables. 2 - Write a parameterized constructor to allow the user to initialize the class variables. 3- Write getter & setter for each variable of this class. 4- Then write a main code to create an instance of this class using parameterized constructor, ask user for inputs to initialize the variables of this instance and...
Define the EvenNumber class for representing an even number. The class contains: A data field value...
Define the EvenNumber class for representing an even number. The class contains: A data field value of the int type that represents the integer value stored in the object. A no-arg constructor that creates an EvenNumber object for the value 0. A constructor that constructs an EvenNumber object with the specified value. A function named getValue() to return an int value for this object. A function named getNext() to return an EvenNumber object that represents the next even number after...
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements....
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements. But in a queue, the elements are retrieved in a first-in first-out fashion. The class contains: An int[] data field named elements that stores the int values in the queue. A data field named size that stores the number of elements in the queue. A constructor that creates a Queue object with default capacity 8. The method enqueue(int v) that adds v into the...
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods....
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure...
Homework 3 Before attempting this project, be sure you have completed all of the reading assignments,...
Homework 3 Before attempting this project, be sure you have completed all of the reading assignments, hands-on labs, discussions, and assignments to date. Create a Java class named HeadPhone to represent a headphone set. The class contains:  Three constants named LOW, MEDIUM and HIGH with values of 1, 2 and 3 to denote the headphone volume.  A private int data field named volume that specifies the volume of the headphone. The default volume is MEDIUM.  A private...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with a Program class and write the following two methods (headers provided) as described below: - A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number...
Complete the implementation of the Card class. The two methods that you need to complete are...
Complete the implementation of the Card class. The two methods that you need to complete are compareTo and equals. CopareTo API: Compares this card to another card for order. This method imposes the following order on cards: Cards are first compared by rank. If the rank of this card is less than the rank of the other card then -1 is returned. If the rank of this card is greater than the rank of the other card then 1 is...