Question

In: Computer Science

In Java, using the code provided for Class Candle, create a child class that meets the...

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;
}

Solutions

Expert Solution

 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


Related Solutions

Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and...
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and turns them into words and sentences. The code should use the 0 to represent a space between words and 00 to represent a period at the end of a sentence. The 26 letters of the alphabet should be represented in reverse order. In other words, Z is 26 and A is one. In your final product, only the first letter of a sentence should...
Java: Translate the Student class from the code provided to UML, e.g., -----------------------------_ | Student |...
Java: Translate the Student class from the code provided to UML, e.g., -----------------------------_ | Student | ------------------------------ | -lastName: String | | -firstName: String | ----------------------------- Java Code: public class Student { private String lName; private String fName; private int age; private double gpa; public Student(String lname, String fname, int age, double gpa);    public String lname();    public String fname();    public int age(); public double gpa(); public String toString()      {          return lName+ " " + fName+...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                            &n
Code in Java Create a class named DictionaryWord as: DictionaryWord - word: String                                                              - meanings: String + DictionaryWord (String word, String meanings) + getWord(): String + setWord (String word): void + getMeanings(): String + setMeanings(String meanings): void Write a program with the following requirements: Creates 8 DictionaryWord objects with: Word and meanings as the table: word meanings bank robber Steals money from a bank burglar Breaks into a home to steal things forger Makes an illegal copy of something...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
Please perform reverse engineering to compose a UML class diagram for the provided java code /*...
Please perform reverse engineering to compose a UML class diagram for the provided java code /* Encapsulated family of Algorithms * Interface and its implementations */ public interface IBrakeBehavior { public void brake(); } public class BrakeWithABS implements IBrakeBehavior { public void brake() { System.out.println("Brake with ABS applied"); } } public class Brake implements IBrakeBehavior { public void brake() { System.out.println("Simple Brake applied"); } } /* Client that can use the algorithms above interchangeably */ public abstract class Car {...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a)...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a) a TextField b) a Button c) a Text d) a Label 3. create the following container and put them the above controls: a) a VBox controller b) a BorderPane controller c) a GridPane
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT