Question

Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

Create a BoxFigure class that inherits RectangleFigure class

BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in length and width passed, if the height is >= 0 set the height to passed height else height should be 0.- getHeight method should return the instance field height- value-returning area method – 2 * (getLenght() * getWidth() + getLength() * height + getWidth() * height)- value-returning volume method – will return super class area * height;- print method – will display Length, Width, Height, Surface Area and Volume

Polymorphism Class should contain:- Create two reference variables (named rec and shapeRef) of RectangleFigure class. - Create one reference variable (called box) of BoxFigure class. - Create object of rec calling parameterize constructor of RactangleFigure class passing in values into the constructor.- Create object of box calling parameterize constructor of BoxFigure class passing in values into the constructor.- Assign rec to shapeRef reference- Display Rectangle, using shapeRef reference name call the print method - Assign box to shapeRef reference- Display Box, using shapeRef reference name call the print method.

public class RectangleFigure_Polymorphism{

private double length;

private double width;

public RectangleFigure_Polymorphism() {

length = 0;

width = 0;

}

public RectangleFigure_Polymorphism(double l, double w) {

setDimension(l, w);

}

public void setDimension(double l, double w) {

if(l >= 0)

length = l;

else

length = 0;

if(w > 0)

width = w;

else

width = 0;

}

public double getLength() {

return length;

}

public double getWidth() {

return width;

} public double area() {

return length * width;

} public double perimeter() {

return 2 * (length + width);

}

public void print() {

System.out.print("Length = " + length+ "; Width = " + width+ "\n"+ "Area = " + area());

}

}//end of class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello dear....

Here is the perfectly working java program for the given task.  Also attached image of output and code for comfort of reading. Hope you will like it.

Polymorphism.java

public class Polymorphism{
        public static void main(String[] args) {
                //create reference varibles
                RectangleFigure rec, shapeRef;
                BoxFigure box;
                //create objects of classes
                rec = new RectangleFigure(12.5, 6.0);
                box = new BoxFigure(8.5, 5.0, 6.1);

                shapeRef = rec;
                shapeRef.print();
                shapeRef = box;
                shapeRef.print();

        }
}

public class RectangleFigure{
        private double length;
        private double width;
        public RectangleFigure() {
                length = 0;
                width = 0;
        }
        public RectangleFigure(double l, double w) {
                setDimension(l, w);
        }
        public void setDimension(double l, double w) {
                if(l >= 0)
                        length = l;
                else
                        length = 0;
                if(w > 0)
                        width = w;
                else
                        width = 0;
        }
        public double getLength() {
                return length;
        }
        public double getWidth() {
                return width;
        }
        public double area() {
                return length * width;
        }
        public double perimeter() {
                return 2 * (length + width);
        }
        public void print() {
                System.out.print("\nLength = " + length+ "; Width = " + width+ "\n"+ "Area = " + area());
        }
}//end of class

//BoxFigure class inherits Rectangle class
public class BoxFigure extends RectangleFigure{
        //private variables
        private double height;
        //default constructor
        public BoxFigure(){
                super();
                height = 0;
        }
        //overloaded parameterizes constructor
        public BoxFigure(double l, double w, double h){
                super(l, w);
                if(h >= 0)
                        height = h;
                else
                        height = 0;
        }
        //setDimention method to set length, width, height
        public void setDimension(double l, double w, double h) {
                super.setDimension(l,w);

                if(h >= 0)
                        height = l;
                else
                        height = 0;
        }
        //returns height
        public double getHeight() {
                return height;
        }
        //returns surfaceArea box
        public double surfaceArea() {
                return 2 * ( getLength()*getWidth() + getLength()*height + getWidth()*height );
        }
        //returns volue of box
        public double volume() {
                return area() * height;
        }
        //print method to print information
        public void print() {
                System.out.print("\nLength = " + getLength()+ "; Width = " + getWidth()+ "; Height = " + height+ "\n"+ "Surface Area = " + surfaceArea() + "\n"+ "Volume = " + volume());
        }

}

Output:

= Length 12.5; Width = 6.0 Area 75.0 Length 8.5; Width 5.0; Height = 6.1 Surface Area = 249.7 Volume = 259.25 =

Code Images:

public class Polymorphism{ public static void main(String[] args) { //create reference varibles RectangleFigure rec, shapeRef

public void print() { System.out.print(\nLength = - length+ ; Width = + width+ \n+ Area = + area(); } }//end of cl

Hope you like it.

If you have any doubts ask me in comment section.
If you like my work, please give me a like and feedback. That helps me a lot. Thank you.
All the best.

Add a comment
Know the answer?
Add Answer to:
Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...

    Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle {       private double length;    private double width;       public Rectangle(double l, double w)    {        length = l;        width = w;    }       public double getLength()    {   ...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

  • In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

    In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • In the code (C++) below, if you input 2 as length and 4 as width, the...

    In the code (C++) below, if you input 2 as length and 4 as width, the output is: Enter the rectangle's length: 2 Enter the rectangle's width: 4 Length: 2 | Width: 4 | Area: 8 | Perimeter:12 We worked on this code during class, but there were some things that I did not understand. 1) how does compiler read"l" as length, "w" as width, etc.? I thought you had to update it first, like "w=width." 2) i thought you...

  • Consider the following declaration for a class that will be used to represent rectangles. public class...

    Consider the following declaration for a class that will be used to represent rectangles. public class Rectangle { private double height; private double width; public Rectangle() { height = 2.0; width = 1.0; } public Rectangle(double w, double h) { height = h; width = w; } public double getHeight() { return height; } public double getWidth() { return width; } public void setHeight(double h) { height = h; } public void setWidth(double w) { width = w; } //Other...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT