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

Write a Book class that encapsulates a Book. It has three private instance varia

ID: 3537189 • Letter: W

Question


Write a Book class that encapsulates a Book. It has three private instance variables: title, cost, and numberOfPages. The class has a read method, getTitle (), getCost(), getNumberOfPages(), setTitle (), setCost(), setNumberOfPages(), changeTitle (String), changeCost(double) Book add(Book toString() and equals(). The add method returns a Name instance with the returned instance first name being the parameter%u2019s first name, the returned instance middle name being the parameter%u2019s middle name, and the returned instance last name being this.lastName hyphenated with the parameter%u2019s last Name.

here is the demo.

public class BookDemo

{

public static void main(String[] args)

{

Book schoolBook = new Book( );

Book bedReadingBook = new Book( );

Book cookBook = new Book( );

Book newBook;

System.out.println("Enter school book information.");

schoolBook.readBookData( );

System.out.println("Enter bed reading book information.");

bedReadingBook.readBookData( );

System.out.println("Enter cookbook information.");

cookBook.readBookData( );

System.out.println("The cost of your school book is $"

+ schoolBook.getBookCost());

System.out.println("Your bed reading book is " +

bedReadingBook);

if(schoolBook.equals(bedReadingBook))

{

System.out.println("Your school book is the same as your

bed reading book!" );

}

else

{

System.out.println("Your school book is not the same as

your bed reading book!" );

}

newBook = bedReadingBook.add(cookBook);

System.out.println("Creating a new book from the bed reading%u201C

+ "book added to the cookbook is " + newBook);

}

}

Explanation / Answer

import java.util.Scanner;



class Book

{

private String title;

private double cost;

private int numberOfPages;


public Book()

{

title="";

cost=0.0;

numberOfPages=0;

}


public Book(String title,double cost,int numberOfPages)

{

this.title=title;

this.cost=cost;

this.numberOfPages=numberOfPages;

}


public void changeTitle(String title)

{

this.title=title;

}


public void changeCost(double cost)

{

this.cost=cost;


}


public void setNumberOfPages(int numberOfPages)

{

this.numberOfPages=numberOfPages;

}


public String getTitle()

{

return title;

}


public double getCost()

{

return cost;

}



public int getNoOfPages()

{

return numberOfPages;

}



public String toString()

{

return "title: "+title+" cost: "+cost+" nuber of pages: "+numberOfPages;



}


public boolean equals(Book obj)

{

if(!title.equalsIgnoreCase(obj.getTitle()) && obj.getCost()==cost && obj.numberOfPages==numberOfPages)

return true;

else

return false;

}


public Book add(Book obj)

{

Book b=new Book(obj.getTitle(),obj.getCost(),obj.getNoOfPages());

return b;


}


public void readBookData()

{

Scanner in =new Scanner(System.in);

System.out.print("Title: ");

changeTitle(in.nextLine());

System.out.print("Cost : ");

changeCost(in.nextDouble());

System.out.print("number of pages: ");

setNumberOfPages(in.nextInt());



}



}


public class BookDemo

{

public static void main(String[] args)

{

Book schoolBook = new Book( );

Book bedReadingBook = new Book( );

Book cookBook = new Book( );

Book newBook;

System.out.println("Enter school book information.");

schoolBook.readBookData( );

System.out.println("Enter bed reading book information.");

bedReadingBook.readBookData( );

System.out.println("Enter cookbook information.");

cookBook.readBookData( );

System.out.println("The cost of your school book is $"

+ schoolBook.getCost());

System.out.println("Your bed reading book is " +

bedReadingBook);

if(schoolBook.equals(bedReadingBook))

{

System.out.println("Your school book is the same as your bed reading book!" );


}

else

{

System.out.println("Your school book is not the same as your bed reading book!" );


}

newBook = bedReadingBook.add(cookBook);

System.out.println("Creating a new book from the bed reading book added to the cookbook is " + newBook);


}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote