Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3819287 • Letter: O

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 Subclasses to TwoDimensionalShape

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:

Goal:

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

Shape TwoDimensionalShape Circle Rectangle

Explanation / Answer

Please find the definitions of Circle and REctangle Classes and ShapeTest's ouptput.

================

/*
* Circle class definition
*/
public class Circle extends TwoDimensionalShape {

   public Circle(int x, int y, int radius) {
       super(x, y, radius, 0);
   }

   /*
   * This methods returns the area of the circle.
   */
   public double area() {
       return (Math.PI * getRadius() * getRadius());
   }

   public int getRadius() {
       return super.dimension1;
   }

   public void setRadius(int radius) {
       super.setDimension1(radius);

   }
}

=====================

/*
* Rectangle definition
*/
public class Rectangle extends TwoDimensionalShape {

   public Rectangle(int x, int y, int length, int width) {
       super(x, y, length, width);
   }

   /*
   * This method returns the area of a rectangle.
   */
   public double area() {
       return (getLength() * getWidth());
   }

   public int getLength() {
       return super.dimension1;
   }

   public void setLength(int length) {
       super.setDimension1(length);

   }

   public int getWidth() {
       return super.dimension2;
   }

   public void setWidth(int width) {
       super.setDimension2(width);

   }
}

==========================

O/P
Area of the Circle is 78.53981633974483
Area of the Rectangle is 12.0