Question: Overview: Tree Hierarchy (Shape) The figure below shows a hierarchy, where: Shape is the Superclass TwoDimensionalShape is a Subclass to Shape Circle and Rectangle are

Overview:

Tree Hierarchy (Shape) The figure below shows a hierarchy, where: Shape is the Superclass TwoDimensionalShape is a Subclass to Shape Circle and Rectangle are Subclasses to TwoDimensionalShape

Overview: Tree Hierarchy (Shape) The figure below shows a hierarchy, where: Shape

Task 1:

We want to define our Superclass Shape, which requires two instance variables x and y. We want to define setter and getter methods for both instance variables.

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

Task 2:

We now want to create the subclass TwoDimensionalShape, which inherits the Shape class.

Our class definition includes the definition, along with extends Shape (to inherit Shape) class TwoDimensionalShape extends Shape { }

This class requires two additional instance variables: dimension1 and dimension2.

We need to define a Constructor which accepts initializing parameters for the two instance variables for Shape and the two instance variables for TwoDimensionalShape. public TwoDimensionalShape( int x, int y, int d1, int d2 ) { super( x, y ); // Call constructor of superclass this.dimension1 = d1; this.dimension2 = d2; }

We then need to define the setter and getter methods for the new instance variables: 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; }

The complete TwoDimensionalShape class is defined below: 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; } }

Task 3:

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

Define the class Circle, which inherits TwoDimensionalShape. 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) 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. Define a method called area, which has no parameters and returns a double. This method returns the area of the circle ( PI * radius * radius ). Define setter and getter methods for setting the radius.

Task 4:

Define the class Rectangle, which inherits TwoDimensionalShape. 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) 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. Define a method called area, which has no parameters and returns a double. This method returns the area of the rectangle (length x width) Define setter and getter methods for setting both the length and width of the rectangle.

Test:

You may use the following test harness to test your subclasses 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()); } }

Which will result in the following output: Area of the Circle is 78.53981633974483 Area of the Rectangle is 12

Template:

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

Goal:

Submit the definition for both the Circle class and the Rectangle class.

Shape TwoDimensionalShape Circle Rectangle

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!