Use the following files: Product.java BookTester.java Complete the following fil
ID: 3573070 • Letter: U
Question
Use the following files:
Product.java
BookTester.java
Complete the following file:
Book.java
/**
* Subclass of the class named product
*/
public class Book extends Product
{
private String author;
/**
* Constructor takes variables from superclass and subclass
* @param theDescription decription of books
* @param thePrice price of books
* @param theAuthor names of authors
*/
public Book(String theDescription, double thePrice, String theAuthor)
{
super(theDescription, thePrice);
author = theAuthor;
}
/**
* Get names of authors
* @return the author
*/
public String getAuthor()
{
return author;
}
/**
* Check whether the books have the same author or not
* @param other the other book to compare with the book
* @return false
*/
public boolean sameAuthor(Book other)
{
if (author.equals(other.author)) // compare if two Strings are the same
{
if (this.author.equals(other.getAuthor()))
{
return true;
}
}
return false;
}
/**
* Get description of books
* @return the method getDescription
*/
public String getDescription()
{
return super.getDescription();
}
}
Explanation / Answer
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();
}
}
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;
}
}
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");
}
}
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.