014-005-004
Utilizing Overridden Methods: Diverse Behavior
Hard
Problem Description
Utilizing Overridden Methods: Diverse Behavior
In this problem, you will create a program that overrides the getArea() method of the Shape class in Rectangle and Triangle to calculate each shape's area from dimensions read via standard input.
Learning Objective: Understand how to achieve different behavior with the same method name through overriding
Overview
Create a program that calculates the area of shapes. override the getArea() method of Shape class in Rectangle and Triangle.
Specifications
- Shape class: getArea() method (returns 0.0)
- Rectangle class: calculate area with width and height (width * height)
- Triangle class: calculate area with base and height (base * height / 2)
- Output each shape's area
Input Format
rectangle width
rectangle height
triangle base
triangle height
Output Format
Rectangle Area: (rectangle area)
Triangle Area: (triangle area)
Sample Input/Output
Input:
4
5
6
5
Output:
Rectangle Area: 20.0
Triangle Area: 15.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