Ask Question

Write an application TestBooks that asks the user for a number of books read during summer. The application repeatedly creates that many book objects, and then prints to the screen information about the "smallest" book, (i. e. the book with the smallest number of pages), as well as the average number of pages per book read. Hint: Have counter variables pageSum and numBooks for keeping track of the total number of pages and the number of books. Other variables you may want to consider having are pages and title, for creating a new book object every time the user enters the page and title data.

+5
Answers (1)
  1. 27 May, 15:34
    0
    The code to this question as follows:

    Code:

    import java. util.*; / /import package for objects

    public class Book / /defining class Book

    {

    private int pages; / /defining integer variable

    private String title; / /defining String variable

    Book (int pages, String title) / /defining parameterized constructor

    {

    this. pages = pages; / /holding value in private variable

    this. title = title; / /holding value in private variable

    }

    int getPages () / / defining method getPages

    {

    return pages; / /return a value

    }

    void setPages (int pages) / /defining method setPages, that accepts integer value

    {

    this. pages = pages; / /holding value

    }

    String getTitle () / / defining method getTitle

    {

    return title; / /return title

    }

    void setTitle (String title) / /defining method setTitle

    {

    this. title = title; / / holding value

    }

    public String toString () / /defining method toString

    {

    return "Book{"+"pages=" + pages + ", title='" + title + '/'' + '}'; / /return value

    }

    boolean equals (Object o) / /defining method equals

    //using conditional statement

    if (this = = o) / /check value

    return true; / /return value true

    if (o = = null

    int compareTo (Book o) / /defining method compareTo

    {

    return this. getPages () - o. getPages (); / /return getPages value

    }

    }

    Explanation:

    In the above code, first, a package is import for holding objects, in the next line a class book is declared, inside these class two private variable pages and title is declared, in which pages is integer variable and title is string variable, parameterized constructor and method is declared, all the calculation is done in that methods, which can be described as follows:

    In the constructor, the this keyword is used that holds private variable value. In the next step get and set method is used in which the set method is used to set the value by using parameters and the get method is used to return all the set values. At the last, the conditional statement is used that creates a book object and checks the condition, and defined method "compareTo" to return its value.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write an application TestBooks that asks the user for a number of books read during summer. The application repeatedly creates that many ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers