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
- Abstract class
Shapehas anamefield, abstractgetArea(), and concreteprintArea() Circletakesradius(double) in constructor, area =radius * radius * 3.14Rectangletakeswidth(double) andheight(double), area =width * heightprintArea()outputs in format:name + ": " + getArea()- In Main, store
Circle(5.0)andRectangle(4.0, 3.0)inShape[]array, callprintArea()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