A Java question. Implement a subclass of Product called Painting . A Painting ha
ID: 3809499 • Letter: A
Question
A Java question. Implement a subclass of Product called Painting. A Painting has a String instance variable, artist, in addition to description and price. These paintings are framed prints not originals.
Provide the instance variable and methods
1. public String getArtist()
2. public boolean sameAuthor( Painting other) returns true if the paintings have the same author. Otherwise false (You will have to use sameAuthor even though that does not make good sense for a painting.)
3. public String getDescription() returns the description of the Painting then a space and the Artist's name
Provide Javadoc. Use @Override for the overridden getDescription method
------------------------------------------------------------------------------------------------------------------------
The PaintingTester and the Product have be given as follow:
PaintingTester.java
Product.java
Explanation / Answer
// Product.java
public class Product {
private double price;
private String description;
public Product(double price, String description) {
this.price = price;
this.description = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void reducePrice(double percent)
{
double reduction = price * percent / 100;
price = price - reduction;
}
public void increasePrice(double percent)
{
double increase = price * percent / 100;
price = price + increase;
}
}
// end of class Product.java
// Painting.java
import java.util.*;
public class Painting extends Product{
private String author;
public ArrayList<Painting> paintingList = new ArrayList<Painting>();
public Painting(String description, double price, String author) {
super(price, description+" "+author);
this.author = author;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
/*public void addPainting(Painting other){
for (Painting a:paintingList){
if (a.getDescription().equals(other.getDescription())){
paintingList.add(other);
}
}
}*/
public boolean sameAuthor (Painting other){
boolean aut= false;
if (other.getAuthor().equals(this.getAuthor()))
aut=true;
return aut;
}
}
// End of class Painting.java
import java.util.*;
public class Painting extends Product{
private String author;
public ArrayList<Painting> paintingList = new ArrayList<Painting>();
public Painting(String description, double price, String author) {
super(price, description+" "+author);
this.author = author;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public boolean sameAuthor (Painting other){
boolean aut= false;
if (other.getAuthor().equals(this.getAuthor()))
aut=true;
return aut;
}
}
// PaintingTester.java
public class PaintingTester {
public static void main(String[] args) {
Product product = new Painting("The Starry Night",95.0, "Van Gogh");
Painting lilies = new Painting("The Water Lily Pond", 85.0, "Monet");
Painting bridge = new Painting("The Japanese Footbridge", 120.0, "Monet");
Painting girl = new Painting("Girl with a Hoop", 100, "Renoir");
Painting child = new Painting("Child with a Dove", 150.0, "Picasso");
Painting starry = (Painting)product; //Get a Painting reference to the Painting
System.out.println(lilies.sameAuthor(bridge));
System.out.println("Expected: true");
System.out.println(girl.sameAuthor(child));
System.out.println("Expected: false");
System.out.println(starry.getDescription());
System.out.println("Expected: The Starry Night Van Gogh");
System.out.println(girl.getDescription());
System.out.println("Expected: Girl with a Hoop Renoir");
System.out.println(lilies.getPrice());
System.out.println("Expected: 85.0");
System.out.println("Artist: " + starry.getAuthor());
System.out.println("Expected: Van Gogh");
}
}
// End of PaintingTester.java
// Sample Output
true
Expected: true
false
Expected: false
The Starry Night Van Gogh
Expected: The Starry Night Van Gogh
Girl with a Hoop Renoir
Expected: Girl with a Hoop Renoir
85.0
Expected: 85.0
Artist: Van Gogh
Expected: Van Gogh
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.