Question: The ShapeTest class contains incomplete codes in the main method. You need to complete those missing lines of codes to test the polymorphic behaviour by
The ShapeTest class contains incomplete codes in the main method. You need to complete those missing lines of codes to test the polymorphic behaviour by calling the calculateArea method appropriately.
// an interface interface Shape { public double calculateArea();
}
//Rectangle class
class Rectangle implements Shape {
// fields/variables
double length;
double width;
//constructor
public Rectangle(double length, double width)
{ setLength(length); setWidth(width); }
//getters and setters
public double getLength()
{ return length; }
public void setLength(double length)
{ if(length > 0) this.length = length; }
public double getWidth()
{ return width; }
public void setWidth(double width)
{ if(width > 0) this.width = width; }
// returning the area of rectangle public double calculateArea()
{ return length * width; }
}// end of Rectangle class
//Circle class
class Circle implements Shape
{ // fields/variables double radius; //constructor public Circle(double radius) { setRadius(radius); }
//getters and setters
public double getRadius()
{ return radius; }
public void setRadius(double radius)
{ if(radius > 0) this.radius = radius; else this.radius = 0.0; }
// returning area of circle public double calculateArea()
{ return Math.PI * radius * radius; } }
// to test the objects of Rectangle and Circle
public class ShapeTest { public static void main(String [] args)
{ Shape [ ] diagrams = new Shape [3];
// Create three shape objects and test the polymorphic behavior by calling calculateArea() method //Missing lines of codes have to be completed} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
