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...
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
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?");...
Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided...
Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided MPQ class. The functions from MPQ that are virtual function (remove min(), is empty(), min(), and insert()) must be implemented in the child classes. The functions remove min() and min() should throw an exception if the minimum priority queue is empty. For the UnsortedMPQ class, you will use a vector to implement the minimum priority queue functions. The insert() function should be O(1) and...
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 {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT