In: Computer Science
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.
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.