Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please Help. New to Java. Micks Wicks makes candles in various sizes. Create a class for the business named Candlethat contains data fields for color,

Please Help. New to Java.

Micks Wicks makes candles in various sizes. Create a class for the business named Candlethat 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 heightis 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 parents setHeight() method to set the price of a ScentedCandleobject 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.

//Candle.java public class Candle { //instance variabels of class private String color; private int height; protected double price; //constructor of class to set color name, height public Candle(String color,int height) { setColor(color); setHeight(height); } //set color of candle public void setColor(String color) { this.color=color; } //set height public void setHeight(int height) { this.height=height; price=height*2; } //returns height of candle public int getHeight() { return height; } //returns color public String getColor() { return color; } //returns price public double getPrice() { return price; } }

//ScentedCandle.java public class ScentedCandle extends Candle { //additioanl instance field, scent private String scent; /* *Contsructor that calls the base class *Candle with color and height */ public ScentedCandle(String color, int height, String scent) { super(color,height); //set scent this.scent=scent; } //Override the setHeight method to set a new height of base class @Override public void setHeight(int height) { //cal super class setHeight method //to set ehight super.setHeight(height); //set price to 3 per inch price=getHeight()*3; } //set scent name public void setScent(String scent) { this.scent=scent; } //returns scent public String getScent() { return scent; } }

//DemoCandles.java public class DemoCandles { public static void main(String[]args) { //create instance of class Candle Candle candle=new Candle("white",5); System.out.println("Candle object"); System.out.println("Color:"+candle.getColor()); System.out.println("Height:"+candle.getHeight()); System.out.println("Price:"+candle.getPrice()); //create instance of class ScentedCandle ScentedCandle scentedCandle=new ScentedCandle("white",5,"jasmine"); System.out.println("ScentedCandle object"); System.out.println("Color:"+scentCandle.getColor()); System.out.println("Height:"+scentCandle.getHeight()); System.out.println("Price:"+scentCandle.getPrice()); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Horse Betting The Road To Absolute Horse Racing 2

Authors: NAKAGAWA,YUKIO

1st Edition

B0CFZN219G, 979-8856410593

More Books

Students also viewed these Databases questions

Question

LO5 Describe job analysis and the stages in the process.

Answered: 1 week ago