015-003-006

Polymorphism: Method Parameters

Medium

Problem Description

In this problem, you will create a program that implements a printArea() method accepting a parent class type parameter (Shape) and displays the area of Circle and Rectangle instances using polymorphism.

Polymorphism: Method Parameters

Learning Objective: Understand how to utilize polymorphism with parent class type parameters

Overview

You have Circle and Rectangle classes that inherit from Shape class. Create a printArea() method that accepts a Shape-type parameter and calculates/outputs the area of different shapes.

Specifications

  • Shape class: getArea() method (returns 0.0)
  • Circle class: constructor receives radius, getArea() returns circle area (π × radius²)
  • Rectangle class: Constructor receives width and height, getArea() returns area (width × height)
  • printArea(Shape shape) method: Outputs area in "Area: X.X" format
  • Read radius, width, and height with Scanner, then create Circle and Rectangle and call printArea()

Input Format

radius
width
height

Output Format

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

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