Question: Using the code: package shapetest; class Shape { private int x, y; // Coordinates of Shape // Constructor public Shape( int x, int y )

Using the code:

package shapetest; class Shape { private int x, y; // Coordinates of Shape // Constructor public Shape( int x, int y ) { this.x = x; this.y = y; } // Set X Coordinate public void setX( int x ) { this.x = x; } // Set Y Coordinate public void setY( int y ) { this.y = y; } // Get X Coordinate public int GetX() { return this.x; } // Get Y Coordinate public int GetY() { return this.y; } } class TwoDimensionalShape extends Shape { int dimension1; int dimension2; public TwoDimensionalShape( int x, int y, int d1, int d2 ) { super( x, y ); // Call constructor of superclass this.dimension1 = d1; this.dimension2 = d2; } public void setDimension1( int d1 ) { this.dimension1 = d1; } public void setDimension2( int d2 ) { this.dimension2 = d2; } public int getDimension1() { return this.dimension1; } public int getDimension2() { return this.dimension2; } } public class ShapeTest { public static void main(String[] args) { Circle c = new Circle( 1, 1, 5 ); Rectangle r = new Rectangle( 1, 1, 4, 3 ); System.out.println( "Area of the Circle is " + c.area()); System.out.println( "Area of the Rectangle is " + r.area()); } } 

We now want to define the two additional subclasses; Circle and Rectangle. Both of these subclasses extend the TwoDimensionalShape class.

For subclass Circle:

1. Define the class Circle, which inherits TwoDimensionalShape.

2. No new Instance variables should be defined for this class. (Remember that the Instance variables from both Shape and TwoDimensionalShape are inherited in our definition)

3. Define a Constructor, which accepts an x and y coordinate, along with the radius of the circle. This constructor should call the superclass constructor where the radius should be assigned to dimension1 of the superclass.

4. Define a method called area, which has no parameters and returns a double. This method returns the area of the circle ( PI * radius * radius ).

5. Define setter and getter methods for setting the radius.

For subclass Rectangle:

1. Define the class Rectangle, which inherits TwoDimensionalShape.

2. No new instance variables should be defined for this class. (Remember that the instance variables from both Shape and TwoDimensionalShape are inherited in our definition)

3. Define a Constructor, which accepts an x and y coordinate, along with the length and width of the rectangle. This constructor should call the superclass constructor where the length of the rectangle should be assigned to dimension1 and the width of the rectangle should be assigned to dimension2 of the superclass.

4. Define a method called area, which has no parameters and returns a double. This method returns the area of the rectangle (length x width)

5. Define setter and getter methods for setting both the length and width of the rectangle.

Expected Output after excuting the program:

Area of the Circle is 78.53981633974483

Area of the Rectangle is 12

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!