In: Computer Science
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output
------------------------------------------------------------------------
1. The child class will be
named ScentedCandle
2. The data field for the ScentedCandle class is:
scent
3. It will also have getter and
setter methods
4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint: price = height * PER_INCH)
CODE PROVIDED TO START WITH
Class Candle
public class Candle
{
private String color;
private int height;
protected double price;
public String getColor( )
{
return color;
}
public int getHeight( )
{
return height;
}
public double getPrice( )
{
return price;
}
public void setColor(String clr)
{
color = clr;
}
public void setHeight(int ht)
{
final double PER_INCH = 2;
height = ht;
price = height * PER_INCH;
}
class Candle {
private String color;
private int height;
protected double price;
public String getColor() {
return color;
}
public int getHeight() {
return height;
}
public double getPrice() {
return price;
}
public void setColor(String clr) {
color = clr;
}
public void setHeight(int ht) {
final double PER_INCH = 2;
height = ht;
price = height * PER_INCH;
}
}
class ScentedCandle extends Candle{
private int scent;
//setters and getters for scent
public int getScent() {
return scent;
}
public void setScent(int aScent) {
scent = aScent;
}
@Override
//overriding the setHeight()
public void setHeight(int ht) {
final double PER_INCH = 3;
//setting the height using super keyword
super.setHeight(ht);
//finding the price $3 per inch
price = getHeight() * PER_INCH;
}
}
public class TestCandle {
public static void main(String[] args) {
ScentedCandle sc = new ScentedCandle();
sc.setHeight(10);
sc.setScent(2);
System.out.println("Price: $"+sc.getPrice());
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
Please Like and Support me as it helps me a lot