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

I have a book class which is as follows class Book{ private int numberOfPages; p

ID: 3640679 • Letter: I

Question

I have a book class which is as follows

class Book{ private int numberOfPages; private boolean hardBack; private String title; private double price;
//constructors Book() { title = ""; numberOfPages = 0; hardBack = true; price = 0.0; }
Book(int n, boolean h, String t, double p) { numberOfPages = n; hardBack = h; title = t; price = p; }
int getPages() { return numberOfPages; }
void setPage(int n) { numberOfPages = n; }
boolean getBack() { return hardBack; }
void setBack(boolean h) { hardBack = h; }
String getTitle() { return title; }
void setTitle(String t) { title = t; } double getPrice() { return price; }
void setPrice( double p) { price = p; } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj == null || obj.getClass() != this.getClass()) { return false; } Book book = (Book) obj; return (book.getPages() == this.getPages() && book.getPrice() == this.getPrice() && book.getBack() == this.getBack() && book.getTitle() .equalsIgnoreCase(this.getTitle())); }
@Override public String toString() { return ("No Of Pages: " + this.getPages() + " " + "Price of Book: " + this.getPrice() + " " + "Hard Back: " + this.getBack() + " " + "Book Title: " + this.getTitle()); }

}
and Usebook which is

import java.util.*;public class UseBook { public static void main(String[] args) { Book b1 = new Book(90,true,"Lord of the Flies",8.99); Book b2 = new Book(287,true,"The Grand Design",15.40); Book b3 = new Book(300,false,"Farenheit 451",9.99); Book b4 = new Book(300,false,"Farenheit 451",9.99); System.out.println("Book 1: " + b1); System.out.println("Book 2: " + b2); System.out.println("Book 3: " + b3); if (b1.equals(b2)) { System.out.println("b1 and b2 are equal"); } else { System.out.println("b1 and b2 are not equal"); } if (b3.equals(b4)) { System.out.println("b3 and b4 are equal"); } else { System.out.println("b3 and b4 are not equal"); }
}}

I now have to create a new class called BookException. This class will be derived from java.lang.exception and will overrided the two constructors, as described in the class. create this class in its own file.

Modify the existing Book cass so that the Book constructor throws a BookExceptionif any of hte preconditions fail.

numebrOfPages is less than 1, title is null or has a zero length, price is negative

The Book constructor should specify the problem when instantiating the BookException. In addition, modify the mutator method for numebrOfPages ,title & price to throw a BookException if invalid data is specified.

In class UseBook attempt to instantiate the Book class with correct value and incorrect value(once for each attribute checked). wrap each attempt to construct a Book in its own try/ catch block. Print out the message set by the book constructor in Book Exception.

I am sorry but I really need help with this. I would appreciate it very much. Thanks in advance.

Explanation / Answer

Book Class package book; class Book { private int numberOfPages; private boolean hardBack; private String title; private double price; // constructors Book() { title = ""; numberOfPages = 0; hardBack = true; // price = 0.0; } Book(int n, boolean h, String t, double p) throws BookException { numberOfPages = n; hardBack = h; title = t; price = p; if (n < 1) { throw new BookException("No of Pages are less than one"); } if (t == null || t.length() == 0) { throw new BookException("Empty title"); } if (price < 0) { throw new BookException("Negative price value"); } } int getPages() { return numberOfPages; } void setPage(int n) throws BookException { if (n < 1) { throw new BookException("No of Pages are less than one"); } numberOfPages = n; } boolean getBack() { return hardBack; } void setBack(boolean h) { hardBack = h; } String getTitle() { return title; } void setTitle(String t) throws BookException { if (t == null || t.length() == 0) { throw new BookException("Empty title"); } title = t; } double getPrice() { return price; } void setPrice(double p) throws BookException { if (p < 0) { throw new BookException("Negative price value"); } price = p; } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj == null || obj.getClass() != this.getClass()) { return false; } Book book = (Book) obj; return (book.getPages() == this.getPages() && book.getPrice() == this.getPrice() && book.getBack() == this.getBack() && book.getTitle() .equalsIgnoreCase(this.getTitle())); } @Override public String toString() { return ("No Of Pages: " + this.getPages() + " " + "Price of Book: " + this.getPrice() + " " + "Hard Back: " + this.getBack() + " " + "Book Title: " + this.getTitle()); } } *************************************************************UseBook class package book; import java.util.*; public class UseBook { public static void main(String[] args) { Book b1, b2, b3, b4; try { b1 = new Book(90, true, "Lord of the Flies", 8.99); b2 = new Book(287, true, "The Grand Design", 15.40); b3 = new Book(300, false, "Farenheit 451", 9.99); b4 = new Book(300, false, "Farenheit 451", 9.99); System.out.println("Book 1: " + b1); System.out.println("Book 2: " + b2); System.out.println("Book 3: " + b3); if (b1.equals(b2)) { System.out.println("b1 and b2 are equal"); } else { System.out.println("b1 and b2 are not equal"); } if (b3.equals(b4)) { System.out.println("b3 and b4 are equal"); } else { System.out.println("b3 and b4 are not equal"); } } catch (BookException e) { System.out.println(e.getMessage()); } } } ********************************************************************** BookException Class package book; public class BookException extends Exception { static final long serialVersionUID = -3387516993124229948L; /** * Constructs a new exception with null as its detail message. * The cause is not initialized, and may subsequently be initialized by a * call to {@link #initCause}. */ public BookException() { super(); } /** * Constructs a new exception with the specified detail message. The cause * is not initialized, and may subsequently be initialized by a call to * {@link #initCause}. * * @param message * the detail message. The detail message is saved for later * retrieval by the {@link #getMessage()} method. */ public BookException(String message) { super(message); } } ****************************************************** Sample Output Please change the values in UseBook.java b1 = new Book(0, true, "Lord of the Flies", 8.99); No of Pages are less than one b2 = new Book(287, true, "", 15.40); Empty title b4 = new Book(300, false, "Farenheit 451", -9.99); Negative price value