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 Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If...
CODE IN JAVA Create a class Student includes name, age, mark and necessary methods. Using FileWriter,...
CODE IN JAVA Create a class Student includes name, age, mark and necessary methods. Using FileWriter, FileReader and BufferedReader to write a program that has functional menu: Menu ------------------------------------------------- Add a list of Students and save to File Loading list of Students from a File Display the list of Students descending by Name Display the list of Students descending by Mark Exit Your choice: _ + Save to File: input information of several students and write that information into a...
This Code Is Supposed To Be Performed In JAVA 1.) Create an abstract class DiscountPolicy. It...
This Code Is Supposed To Be Performed In JAVA 1.) Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost. Create a driver class that tests this class and provide the UML. 2.) In a separate program, define DiscountPolicy as an interface instead of the abstract class. Create a driver class that tests...
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
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...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
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?");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT