016-003-009

Shape Area Display

Easy

Problem Description

Shape Area Display

In this problem: You will create Circle and Rectangle classes that extend an abstract class Shape, and use polymorphism to display each shape's area.

Learning Objective: Understand design patterns combining abstract class inheritance with polymorphism

Overview

The abstract class Shape defines an abstract method getArea() and a concrete method printArea(). Circle and Rectangle each implement getArea(), and printArea() displays the area.

Specifications

  1. Abstract class Shape has a name field, abstract getArea(), and concrete printArea()
  2. Circle takes radius (double) in constructor, area = radius * radius * 3.14
  3. Rectangle takes width (double) and height (double), area = width * height
  4. printArea() outputs in format: name + ": " + getArea()
  5. In Main, store Circle(5.0) and Rectangle(4.0, 3.0) in Shape[] array, call printArea() on each

Output Format

Circle: 78.5
Rectangle: 12.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