Question

In: Computer Science

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.
2. 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.
This program should make use of the compareTo method from Part 1 and a for-loop.

Solutions

Expert Solution

Book.java

public class Book{
    private int pages;
    private String title;
    Book(int pages, String title){
        this.pages=pages;
        this.title=title;
    }
    public int getPages(){
        return this.pages;
    }
    public void setPages(int pages){
        this.pages=pages;
    }
    public String getTitle(){
        return this.title;
    }
    public void setTitle(int pages){
        this.title=title;
    }
    public String toString(){
        return "Title : "+this.title+", Number of Pages : "+this.pages;
    }
    public boolean equals(Book test){
        return (this.title==test.title)&&(this.pages==test.pages);
    }
    public int compareTo(Book test){
        if(test.pages==this.pages)
            return 0;
        else if(this.pages<test.pages)
            return -1;
        else
            return 1;
    }
}

TestBooks.java

import java.util.*;

class TestBooks{
    public static void main(String[] args){
        //declare variables to store total pages, number of books and average pages
        int pageSum, numBooks, averagePages;
        //scanner object to read from console
        Scanner sc = new Scanner(System.in);
        //prompt user to enter number of books
        System.out.print("Enter number of books to read in summer: ");
        //read number of books from user
        numBooks=sc.nextInt();
        //take care of new line entry after int entry
        sc.nextLine();
        //create array of books
        Book[] books = new Book[numBooks];
        //initialize pageSum to 0
        pageSum=0;
        //loop through the array to create books
        for(int i=0;i<numBooks;i++){
            //prompt user to enter title
            System.out.print("Enter title for Book " + (i+1) + ": ");
            String title = sc.nextLine();
            //prompt user to enter tnumber of pages
            System.out.print("Enter number of pages for Book " + (i+1) + ": ");
            int pages=sc.nextInt();
            //take care of new line entry after int entry
            sc.nextLine();
            //create new book object
            books[i]=new Book(pages, title);
            //add pages to our pageSum variable
            pageSum+=pages;
        }
        //calculate avergae
        averagePages=pageSum/numBooks;
        //calculate smallest book
        Book smallestBook = books[0];
        //compare with all other books
        for(int i=1;i<numBooks;i++){
            if(books[i].compareTo(smallestBook)==-1){
                smallestBook=books[i];
            }
        }
        //print smallest book 
        System.out.println("Smallest book is:  " + smallestBook.toString());
        //print number of pages 
        System.out.println("Average number of pages in the book = " + averagePages);
    }   
}

Console Input/Output Screenshot

Let me know in the comments if you have any doubts.
Do leave a thumbs up if this was helpful.


Related Solutions

In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named BankAccount that contains: A private int data field named id for the...
Design a class named BankAccount that contains: A private int data field named id for the account. A private double data field named balance for the account. A constructor that creates an account with the specified id and initial balance. A getBalance() method that shows the balance. A method named withdraw that withdraws a specified amount from the account. Create a subclass of the BankAccount class named ChequingAccount. An overdraftlimit to be 1000 for ChequingAccount . Test your ChequingAccount class...
In c++ Design a class named Account that contains: a.An int data field named id for...
In c++ Design a class named Account that contains: a.An int data field named id for the account. b.A double data field named balancefor the account. c.A double data field named annualInterestRate that stores the current interestrate. d.A no-arg constructor that creates a default account with id 0, balance 0, andannualInterestRate 0. e.The set and get functions for id,balance, and annualInterestRate. f.A functionearnedAmount()that calculates and returns the amount of dollars earned after one year. g.A function named printAccountInfo() that print...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
Private int data field named id for the account A private double data field named balance...
Private int data field named id for the account A private double data field named balance for the account Private double data field named annualInterestRate that stores the current interest rate Assume all accounts have the same rate Private date data field named date Created that stores the date when the account was created No arg constructor that creates an account with the specified id and initial balance The get and set methods for id balance and annual Interest Rate...
Design a class named ArraySort and it contains the following method: Public int search(int target): if...
Design a class named ArraySort and it contains the following method: Public int search(int target): if the target is found in the array, return the number of its showing up. If the target is not found in the array, return -1. If the array is empty, return -1; Public int maximum():Return the maximum number in the array if the array is nonempty, otherwise return -1; Public int minimum(): Return the minimum number in the array if the array is nonempty,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT