Mick’s Wicks makes candles in various sizes. Create a class for the business nam
ID: 3762547 • Letter: M
Question
Mick’s Wicks makes candles in various sizes. Create a class for the business named Candle that contains data fields for color, height, and price. Create get methods for all three fields. Create set methods for color and height, but not for price. Instead, when height is set, determine the price as $2 per inch. Create a child class named ScentedCandle that contains an additional data field named scent and methods to get and set it. In the child class, override the parent’s setHeight() method to set the price of a ScentedCandle object at $3 per inch. Write an application that instantiates an object of each type and displays the details.
Save the files as Candle.java, ScentedCandle.java, and DemoCandles.java.
Explanation / Answer
public class Candle {
public double height,price;
public String color;
public double getHeight()
{
return height;
}
public double getPrice()
{
return price;
}
public String getColor()
{
return color;
}
public void setColor(String c)
{
color=c;
}
public void setHeight(double h)
{
height=h;
price=height*2;
}
}
public class ScentedCandle extends Candle{
public int scent;
public int getScent()
{
return scent;
}
public void setScent(int s)
{
scent=s;
}
public void setHeight(double h)
{
super.height=h;
super.price=super.height*3;
}
}
public class DemoCandles {
public DemoCandles() {
}
public static void main(String args[])
{
Candle c=new Candle();
System.out.println("-----------------Test Set Color and Set Height--------------");
c.setColor("RED");
c.setHeight(5);
System.out.println(" Candle Color: "+c.getColor()+" Candle Height: "+c.getHeight()+" Candle Price: "+c.getPrice());
ScentedCandle sc=new ScentedCandle();
System.out.println("-----------------Test Set Height of Scented Candle--------------");
sc.setHeight(10);
System.out.println(" ScentedCandle Height: "+sc.height+" ScentedCandle Price: "+sc.price);
}
}
Output
--------------------Configuration: <Default>--------------------
-----------------Test Set Color and Set Height--------------
Candle Color: RED
Candle Height: 5.0
Candle Price: 10.0
-----------------Test Set Height of Scented Candle--------------
ScentedCandle Height: 10.0
ScentedCandle Price: 30.0
Process completed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.