Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA: Kindle readers have to store information about each book that users are re

ID: 3738023 • Letter: J

Question

JAVA: Kindle readers have to store information about each book that users are reading. The two main items of information are the number of pages in the book, and the current page number the reader has reached. Write a class to create objects that store this information, and work with this simple driver class. Your class must have:

Two integer data items for storing the page information.

A constructor that receives the number of pages in the book, and sets the current page number to 1.

A toString method for printing the object.

A method to increase the current page number. If no argument is sent it increments the current page number. If an integer argument is sent it increases the current page number by that amount. (Hint: Review "Overloading" in the notes about Methods.) In both cases the method must check if the increase will take the current page number past the end of the book, in which case an error message should be issued, and the current page number set to the last page. (Hint: Use the toString method to help print the error message.)

Here's what a sample run should look like (with the keyboard input shown in italics) ...

Explanation / Answer

import java.util.Scanner;

class book_page//book object

int total_pgs;//total pages in the book

int curr_pg_num;//current page

book_page(int total_pgs)//contructor takes one argument i.e aonly total pages in the book

{

this.total_pgs=total_pgs;

this.curr_pg_num=1;

}

public void inc_pg(int num)//increase pg number by input parameter value

{

if(curr_pg_num+num>total_pgs)

{

System.out.println("Turning "+num+" pages would take you past the last page.");

curr_pg_num=total_pgs;

}

else

curr_pg_num+=num;

System.out.print(toString());

}

public void inc_pg()//overloading the function to take no arguments and increase it by 1

{

if(curr_pg_num+1>total_pgs)//checking if the increment will take past the total page or not

{

System.out.println("Turning "+1+" page would take you past the last page.");

this.curr_pg_num=total_pgs;

}

else

this.curr_pg_num+=1;

System.out.print(toString());

}

public String toString()// to print the object

{

return "Page "+curr_pg_num+" of "+total_pgs;

}

}

class Driver

{

public static void main(String args[])

{

System.out.println("Enter the book total page");

Scanner sc=new Scanner(System.in);

book_page new_book=new book_page(Integer.parseInt(sc.nextLine()));// input for the total no. of pages the book have

while(true)//loop till we reach the page

{

if(new_book.curr_pg_num==new_book.total_pgs)// if we are at last page break

break;

System.out.println(" Enter the no. of book pages to increment");

String inc=sc.nextLine();//input

if(inc.length()==0)//if the input string is empty then call no arguments function

{

new_book.inc_pg();

}

else

{

new_book.inc_pg(Integer.parseInt(inc));//call the argument function

}

}

}

}