X458: Polymorphism Practice 2

For the question below, assume the following implementation of the Measurable interface and Square and Circle classes:

public class Shape {

}

public class Circle extends Shape
{
   private double radius;


   public Circle(double newRadius)
   {
      radius = newRadius;
   } // end constructor

   public double getRadius() {
       return radius;
   }


   public double getCircumference()
   {
      return 2.0 * Math.PI * radius;
   } // end getCircumference

   public double getPerimeter() // Measurable interface
   {
      return getCircumference();
   } // end getPerimeter

   public double getArea()      // Measurable interface
   {
      return Math.PI * radius * radius;
   } // end getArea

} // end Circle

public class Square extends Shape  {

    private double side;

    public Square(double len) {
        side = len;
    }

    public double getSide() {
        return side;
    }

    public double getPerimeter() {
        return 4 * side;
    }

    public double getArea() {
        return side * side;
    }

}

For this method, you will be writing a method that takes in a variable of type Shape:

  • If the Shape is a Square, return the side of the square (by calling getSide())
  • if the Shape is a Circle, return the radius of the circle (by calling getRadius())

Two static final objects (one square and one circle) have been provided to you, which may help you solve this problem.

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.