In java, Implement a hierarchy of classes: Book, Novel, TextBook, and UsedNovel.
ID: 3786237 • Letter: I
Question
In java, Implement a hierarchy of classes: Book, Novel, TextBook, and UsedNovel.
A book is specified by its title and base price. It contains methods that allow users to get the values of these variables as well as altering its base price.
A Novel is a type of book that contains two additional information units: its type which could be romance, mystery, or adventure; and the percent markup on its base price. The actual cost of a Novel is its base price plus the percentage markup. For example, if the base price of a Novel was $20.00 and its markup percentage is 10%, then the actual cost of that Novel is $22.00. A Novel has methods that allow users to get the values of its title, type, base price, and markup percentage; calculate and return its actual cost; and change the markup percentage as desired.
A TextBook is a type of book that identifies the field it belongs to, like CompSc, Physics, Economics, and so on. Additionally, invariably, TextBooks have percentage discount associated with them. The actual cost of a TextBook is its base price minus the discount. For example, if the base price of a TextBook was $50.00 and percent discount was 20%, then its actual cost would be $40.00. A TextBook provides methods that allow users to get the values of its title, field it belongs to, base price, and discount percentage; calculate and return its actual cost; and change the discount percentage as desired.
A UsedNovel is a type of Novel that also indicates how old is that copy. If the copy is less than 5 years old, discount given to the customer is 5% of its actual cost, if it is more than or equal to 5 years but less than 10 years old, then the discount is 10%, and if it is greater or equal to 10 years old, the discount is 15%. The discount is applied after the price of the corresponding Novel is calculated. This class must provide methods to allow users to get the values of all instance variables declared or inherited, and return its cost price calculated as above.
By the way, to create the individual objects of each type, the user must specify the values of all required parameters; no default values are assumed.
It is mandatory that all these classes contain methods "toString" and "equals" that override these methods of Object class. Every class must be able to print out the values of all declared or inherited variables for its objects using the "toString" method. Two books are equal if they have the same title; two Novels are equal if they are the same book and are of the same type; two TextBooks are equal if they are the same book and belong to the same field; and two UsedNovels are equal if they are the same Novel and their actual costs are within a dollar of each other.
Explanation / Answer
//Book.java
public class Book {
private String title;
private double basePrice;
public Book() {
title="";
basePrice=0;
}
public Book(String title, double basePrice) {
this.title=title;
this.basePrice=basePrice;
}
public void setTitle(String title){
this.title=title;
}
public void setPrice(double basePrice){
this.basePrice=basePrice;
}
public String getTitle(){
return title;
}
public double getPrice(){
return basePrice;
}
@Override
public String toString() {
return String.format("Title : %s Base Price : $%f ", title,basePrice);
}
@Override
public boolean equals(Object obj) {
Book b=(Book)obj;
return title.equalsIgnoreCase(b.getTitle());
}
}
--------------------------------------------------------------------------------------------------------
//Novel.java
public class Novel extends Book{
private String type;
private double percent;
public Novel(String title, double basePrice,
String type, double percent) {
super(title, basePrice);
this.type=type;
this.percent=percent;
}
public void setPercent(double percent){
this.percent=percent;
}
public double actualCost()
{
double cost=0;
cost=getPrice()+(percent/100)*getPrice();
return cost;
}
@Override
public String toString() {
return super.toString()
+String.format("Type : %s Percent : %5.2f Cost : %f ", type,percent,actualCost());
}
@Override
public boolean equals(Object obj) {
Book b=(Book)obj;
return getTitle().equalsIgnoreCase(b.getTitle())
&&type.equalsIgnoreCase(b.getTitle());
}
}
--------------------------------------------------------------------------------------------------------
//TextBook.java
public class TextBook extends Book{
private double discount;
private String type;
public TextBook(String title, double basePrice,
String type, double discount) {
super(title, basePrice);
this.discount=discount;
this.type=type;
}
@Override
public String toString() {
return super.toString()
+String.format("Type : %s Discount : %f Cost : $%5.2f ", type,discount, getCost());
}
public double getCost(){
double cost=0;
cost=getPrice()-(discount/100)*getPrice();
return cost;
}
@Override
public boolean equals(Object obj) {
Book b=(Book)obj;
return getTitle().equalsIgnoreCase(b.getTitle())
&&type.equalsIgnoreCase(b.getTitle());
}
}
--------------------------------------------------------------------------------------------------------
//UsedNovel.java
public class UsedNovel extends Book{
private int yearsUsed;
private double discount;
public UsedNovel(String title, double basePrice,
int yearsUsed) {
super(title, basePrice);
this.yearsUsed=yearsUsed;
setDiscount();
}
private void setDiscount() {
if(yearsUsed<5)
discount=0.05;
else if(yearsUsed>=5 && yearsUsed<=10)
discount=0.10;
else if(yearsUsed>=10)
discount=0.15;
}
public double getCost(){
double cost=0;
cost=getPrice()-(discount/100)*getPrice();
return cost;
}
@Override
public String toString() {
return super.toString()
+String.format("Years used : %d Discount : %f Final price : %5.2f ",
yearsUsed,discount,getCost());
}
@Override
public boolean equals(Object obj) {
Novel novel=(Novel)obj;
return getTitle().equalsIgnoreCase(novel.getTitle())
&& (Math.abs(getCost()-novel.actualCost())<1.0);
}
}
--------------------------------------------------------------------------------------------------------
//Driver.java
public class Driver
{
public static void main(String[] args)
{
//Create an instance of Book class
Book book=new Book("My Experiments with Truth", 4);
System.out.println(book.toString());
//Create an instance of Novel class
Novel novel=new Novel("Alice in wonderlan", 10, "Fiction", 5);
System.out.println(novel.toString());
//Create an instance of TextBook class
TextBook tbook=new TextBook("How to java", 25, "programming", 10);
System.out.println(tbook.toString());
//Create an instance of UsedNovel class
UsedNovel unovel=new UsedNovel("Secret", 30, 2);
System.out.println(unovel.toString());
}
}
--------------------------------------------------------------------------------------------------------
Sample Output:
Title : My Experiments with Truth Base Price : $4.000000
Title : Alice in wonderlan Base Price : $10.000000
Type : Fiction Percent : 5.00 Cost : 10.500000
Title : How to java Base Price : $25.000000
Type : programming Discount : 10.000000 Cost : $22.50
Title : Secret Base Price : $30.000000
Years used : 2 Discount : 0.050000 Final price : 29.99
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.