015-001-002

Basic Polymorphism: Shape Area

Hard

Problem Description

Basic polymorphism: Shape Area

In this problem, you will create a program that defines Rectangle and Circle classes inheriting from Shape, accepts a shape type and dimensions as input, and uses polymorphism to call the same calcArea method on each shape object to display the corresponding area to standard output.

Learning Objective: Understand using common interface through polymorphism

Create Rectangle and Circle classes inheriting from Shape, and store each object in a Shape-typed variable to invoke the same calcArea method, achieving different area calculations depending on the concrete shape type.

Input

Line 1: Shape type (rect or circle)
Line 2: For rect: width and height (space separated), for circle: radius

Output

Type: [type]
Area: [area (2 decimal places)]

Example

Input:

rect
4 5

Output:

Type: Rectangle
Area: 20.00

Test Cases

※ Output examples follow programming industry standards

Normal case
Input:
circle
7
Expected Output:
Type: Circle
Area:153.94
Normal case
Input:
circle
3
Expected Output:
Type: Circle
Area:28.27

Your Solution

Current Mode: My Code
Shape.java🔒
Rectangle.java🔒
Circle.java🔒
Main.java🔒
4/6 ファイル216B
⚠️警告
  • No main method found
import java.util.Scanner;

abstract class Shape {
}
0 B / 5 MB

You have 10 free executions remaining