015-001-005

Polymorphism: Shape Drawing System

Hard

Problem Description

polymorphism: Shape Drawing System

In this problem, you will create a program that implements Circle and Rectangle classes extending an abstract Shape class, builds a Shape array from shape information read via standard input, calls draw() and getArea() using polymorphism, and displays the results to standard output.

Learning Objective: Understand how to handle different object types uniformly using polymorphism

Overview

Polymorphism is a key OOP concept that enables different behaviors with the same interface. A parent class type variable can hold child class instances, and appropriate methods are called at runtime.

Specifications

Create abstract parent class Shape:

  • abstract method draw() (no implementation)
  • abstract method getArea(): returns area (no implementation)

Create child class Circle:

  • Extends Shape
  • Private field radius
  • constructor accepts radius
  • Implement draw(): outputs "Drawing Circle with radius: [radius]"
  • Implement getArea(): returns circle area (radius × radius × 3.14)

Create child class Rectangle:

  • Extends Shape
  • Private fields width and height
  • Constructor accepts width and height
  • Implement draw(): outputs "Drawing Rectangle [width]x[height]"
  • Implement getArea(): returns rectangle area (width × height)

In Main class:

  • Use Scanner to read N (number of shapes) and each shape's type and parameters from standard input
  • Create Circle or Rectangle objects from the input and store in a Shape array
  • Call draw() for each element
  • Output area for each element in format "Area: [area]"

Input Format

N
[type] [params]
...
  • Line 1: number of shapes N
  • Each shape line: Circle [radius] or Rectangle [width] [height]

Output Format

For each shape, output the draw() result followed by the area.

Sample input:

2
Circle 5
Rectangle 4 6

Sample output:

Drawing Circle with radius: 5
Area: 78.5
Drawing Rectangle 4x6
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