Use the following files: Product.java BookTester.java Code the file: Book.java I
ID: 3774147 • Letter: U
Question
Use the following files:
Product.java
BookTester.java
Code the file: Book.java
Implement a subclass of Product called Book. Book has a String instance variable, author, in addition to description and price. Provide the instance variable and methods public String getAuthoro public boolean sameAuthor( Book other) returns true if the books have the same author. Otherwise false public String getDescription0 returns the name of the book then a space and the the author name Provide Javadoc. Use @Override for the overridden getDescription methodExplanation / Answer
BookTester.java
import java.util.Arrays;
/**
* Write a description of class ProductTester here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class BookTester
{
public static void main(String[] args)
{
//be sure Book is a subclass of Product
Product product = new Book("Big Java", 79.0, "Cay Horstmann");
//instantiate three Books
Book cplusplus = new Book("Big C++", 85.0, "Cay Horstmann");
Book hfjs = new Book("Head First JavaScript", 50, "Eric Freeman");
Book bigJava = (Book)product; //Get a Book reference to the Book
System.out.println(bigJava.sameAuthor(cplusplus));
System.out.println("Expected: true");
System.out.println(bigJava.sameAuthor(hfjs));
System.out.println("Expected: false");
System.out.println(bigJava.getDescription());
System.out.println("Expected: Big Java Cay Horstmann");
System.out.println(hfjs.getDescription());
System.out.println("Expected: Head First JavaScript Eric Freeman");
System.out.println(bigJava.getPrice());
System.out.println("Expected: 79.0");
System.out.println(bigJava.getAuthor());
System.out.println("Expected: Cay Horstmann");
}
}
Product.java
public class Product
{
private double price;
private String description;
/**
* Constructs a Product with a price and a description
* @param thePrice the price of this Product
* @param theDescription - the description of this product
*/
public Product(String theDescription, double thePrice)
{
price = thePrice;
description = theDescription;
}
/**
* Gets the price
* @return the price of this Product object
*/
public double getPrice()
{
return price;
}
/**
* Gets the description
* @return the description of the Product object
*/
public String getDescription()
{
return description;
}
/**
* Reduces the price of this product by the give percentage
* @param percent the percentage to reduce the priice by
*/
public void reducePrice(double percent)
{
double reduction = price * percent / 100;
price = price - reduction;
}
/**
* Increases the price by the given percent
* @param percent the percent to increase the price by
*/
public void increasePrice(double percent)
{
double increase = price * percent / 100;
price = price + increase;
}
}
Book.java
public class Book extends Product{
private String author;
public Book(String theDescription,double thePrice,String a ){
super(theDescription,thePrice);
author = a;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public boolean sameAuthor(Book b){
return getAuthor().equalsIgnoreCase(b.getAuthor());
}
public String getDescription()
{
return super.getDescription()+" "+getAuthor();
}
}
Output:
true
Expected: true
false
Expected: false
Big Java Cay Horstmann
Expected: Big Java Cay Horstmann
Head First JavaScript Eric Freeman
Expected: Head First JavaScript Eric Freeman
79.0
Expected: 79.0
Cay Horstmann
Expected: Cay Horstmann
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.