015-001-008

Polymorphism with Arrays

Medium

Problem Description

Polymorphism with Arrays

In this problem, you will create a program that reads a radius, width, and height from standard input, stores Circle and Rectangle instances in a Shape array, and uses polymorphism to call getArea() on each element in a loop, displaying each shape's calculated area to standard output.

Learning Objective: Understand how to handle different child class instances uniformly in parent class type arrays

Overview

Create a program that stores different Shape objects in an array and processes them uniformly in a loop.

Input Format

radius of circle (line 1)
width height of rectangle (line 2, space-separated)

Specifications

  • Define getArea() method in Shape class (returns 0.0)
  • Circle class extends Shape, calculates area from radius (pi*r^2)
  • Rectangle class extends Shape, calculates area from width*height
  • Read radius, width, height with Scanner; store in array in Main and output area in loop

Output Format

Area: [circle area]
Area: [rectangle area]

Example

Input:

5
4 6

Output:

Area: 78.53975
Area: 24.0

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up