Project Book class. Tuesday April 3 Write a Book class that encapsulates a Book.
ID: 3699400 • Letter: P
Question
Project Book class. Tuesday April 3 Write a Book class that encapsulates a Book. Start with the Name class on my website as a template for the book class. It has four private instance variables: title, category, cost and pageNumber. See the chart below for the methods in the Book class. The add method returns a Book instance with the returned instance title being the this.title hyphen parameter's title. Instance category is the this.category hyphen parameter's category. The returned instance cost is this.cost added to the parameter's cost and the returned instance pageNumber is this pageNumber added to the parameter's pageNumber. I will provide a main program as a demo. The Book class mustExplanation / Answer
import java.util.Scanner;
class Book {
private String title; // Declaring the variables
private String category;
private double cost;
private int pageNumber;
public void readBookData() { //taking the the book data from user and initializing the variables
Scanner sc=new Scanner(System.in);
System.out.println("Please enter book title");
this.title=sc.next();
Scanner sc1=new Scanner(System.in);
System.out.println("Please enter book category");
this.category=sc1.next();
Scanner sc2=new Scanner(System.in);
while (true) { // Using the while loop to re-enter when a negative value is entered
sc2=new Scanner(System.in);
System.out.println("Please enter book cost");
this.cost=sc2.nextDouble();
if (this.cost>=0) {
break;
}
}
Scanner sc3=new Scanner(System.in);
while (true) { // Using the while loop to re-enter when a negative value is entered
sc3=new Scanner(System.in);
System.out.println("Please enter number of pages");
this.pageNumber=sc3.nextInt();
if (this.pageNumber>=0) {
break;
}
}
}
public String getTitle() {
return this.title;
}
public double getCost() {
return this.cost;
}
public String getCategory() {
return this.category;
}
public int getPageNumber() {
return this.pageNumber;
}
public void setTitle(String title) {
this.title=title;
}
public void setCategory(String category) {
this.category=category;
}
public void setCost(double cost) {
while(true) { // Using the while loop to exit when a negative value is entered
if(cost<0) {
System.out.println("Entered cost is negative.");
System.exit(0);
} else {
this.cost=cost;
break;
}
}
}
public void setPageNumber(int pageNumber) {
while(true) { // Using the while loop to exit when a negative value is entered
if(pageNumber<0) {
System.out.println("Entered number of pages is negative.");
System.exit(0);
} else {
this.pageNumber=pageNumber;
break;
}
}
}
public void setBook(Book bk) {
this.setBook(bk.getTitle(),bk.getCategory(),bk.getCost(),bk.getPageNumber());
}
public void setBook(String title, String category, double cost, int pageNumber) {
this.title=title;
this.category=category;
while(true) { // Using the while loop to exit when a negative value is entered
if(cost<0) {
System.out.println("Entered cost is negative.");
System.exit(0);
} else {
this.cost=cost;
break;
}
}
while(true) { // Using the while loop to exit when a negative value is entered
if(pageNumber<0) {
System.out.println("Entered number of pages is negative.");
System.exit(0);
} else {
this.pageNumber=pageNumber;
break;
}
}
}
public Book add(Book bk) {
Book bk2=new Book(); // Creating a new Book object
bk2.setTitle(bk.getTitle());
bk2.setCategory(bk.getCategory());
bk2.setCost(bk.getCost());
bk2.setPageNumber(bk.getPageNumber());
return bk2;
}
public String toString() {
return " Title "+this.title+" "+"Category "+this.category+" "+"Cost"+cost+" pages "+this.pageNumber;
}
public boolean equals(Book bk) {
if (this.title==bk.getTitle() && this.category==bk.getCategory() && this.cost==bk.getCost() && this.pageNumber==bk.getPageNumber()) {
return true;
} else {
return false;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.