public class ArrayShapes3
{
   public static void main(String[] args)
   {
     Shape[] shapes = new Shape[4];            // array of 5 shapes
     shapes[0] = new Circ(1, 2, 3);            // creating 2 circles
     shapes[1] = new Circ(1, 3, 5);

     shapes[2] = new Rect(3, 5, 6, 2);         // creating 2 rectangles
     shapes[3] = new Rect(5, 7, 2, 2);

     Rect rectangle = new Rect(3, 5, 6, 2);    // creating one more rect 
     Circ circle = circFromRect(rectangle);    //  ... and converting it 
     System.out.println(circle.toString()+"\n");    //  into a circle
    
     Circ aCircle = circFromShape(shapes[2]);  // converting shape[2] into
     System.out.println(aCircle.toString()+"\n");   // a circle

     Shape aShape = shapeFromShape(shapes[1], "rectangle"); // conv. shape[1]
     System.out.println(aShape.toString() + "\n");          // into rectangle

     shapes[3] = shapeFromShape(shapes[2], "circle");       // conv. shape[2]
     System.out.println(shapes[4].toString() + "\n");       // into circle

     Circ aCircle = (Circ)shapes[3];           // this is legal
     Shape s2 = (Shape)aCircle;                // this is illegal
   }

// this method converts a rectangle into a circle of (approx.) the same area
   public static Circ circFromRect(Rect rect)
   {
      double area = rect.getArea();
      int radius = (int)(Math.round(Math.sqrt(area / Math.PI)));
      Circ circ = new Circ(rect.getX(), rect.getY(), radius);
      return circ;
   }

// this method converts any shape into a circle of (approx.) the same area
   public static Circ circFromShape(Shape aShape)
   {
      double area = aShape.getArea();
      int radius = (int)(Math.round(Math.sqrt(area / Math.PI)));
      Circ circ = new Circ(aShape.getX(), aShape.getY(), radius);
      return circ;
   }

// this method converts a shape into another shape of the same area and
// of type specified in the second parameter
   public static Shape shapeFromShape(Shape aShape, String type)
   {
      double area = aShape.getArea();
      Shape outShape = aShape;
      if (type.equals("circle"))
      {
        int radius = (int)(Math.round(Math.sqrt(area / Math.PI)));
        outShape = new Circ(aShape.getX(), aShape.getY(), radius);
      }
      else if (type.equals("rectangle"))
      {
        int width = (int)(Math.round(Math.sqrt(area)));
        int height = width;
        outShape = new Rect(aShape.getX(), aShape.getY(), width, height);
      }
      return outShape;
   }
}
