QUESTION 2: BoOK PROGRAM Design and write a java class that represents books. Us
ID: 3718860 • Letter: Q
Question
QUESTION 2: BoOK PROGRAM Design and write a java class that represents books. Use good Object Oriented Programming structure and style. Test your program by creating a separate class with a main program that creates, Your Book class must provide the following capabilities using good encapsulation techniques, 1. Books have a title (String), author (String), publication year (int), and total number of updates, and prints out some Book objects. good style, and good Object Oriented Programming pages (int). Books also have a current reading position (the page where you are in the book currently) 2. Books can be constructed in two ways A new book is created with title, author, publication year, and total number of a. pages (set the reading position to page 1) b. A new book is alternatively created with the all the information as above but also with a current reading position. Be sure the current reading position is not beyond the end of the book. (in both cases be sure the object is fully set up in a consistent way and ready to be used) 3. Provide a method called updatePage that allows users of the class to update the current reading position to a new page. Be sure the reading position does not go beyond the end of the book. Be sure you allow the reading position to go backwards (to show will reread from some point)Explanation / Answer
import java.util.*;
class Book
{
private String title,author;
private int pages,currentReadingPosition;
//constructors
public Book(String title,String author,int pages)
{
this.title = title;
this.author = author;
this.pages = pages;
currentReadingPosition = 1;
}
public Book(String title,String author,int pages,int currentReadingPosition)
{
this.title = title;
this.author = author;
this.pages = pages;
if(currentReadingPosition<=pages) // check current reading page should be less than total pages
this.currentReadingPosition = currentReadingPosition;
}
public void updatePage(int currentReadingPosition)
{
if(currentReadingPosition<=pages) // check current reading page should be less than total pages
this.currentReadingPosition = currentReadingPosition;
}
public String toString()
{
if(pages == currentReadingPosition)
return " "+title+" "+author+" Finished Reading";
else
return " "+title+" "+author+" "+currentReadingPosition+"/"+pages+" Pages Read";
}
}
class TestBook
{
public static void main (String[] args)
{
Book b1 = new Book("c++","Sumita Arora",350,327);
System.out.println(b1);
b1.updatePage(350); // update current page
System.out.println(b1);
Book b2 = new Book("Logic in Computer SCience","Micheal Huth",450,450);
System.out.println(b2);
Book b3 = new Book("Computer Science Illuminated","Nell Dail",566,469);
System.out.println(b3);
}
}
Output:
c++ Sumita Arora 327/350 Pages Read
c++ Sumita Arora Finished Reading
Logic in Computer SCience Micheal Huth Finished Reading
Computer Science Illuminated Nell Dail 469/566 Pages Read
Do ask if any doubt. Please upvote
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.