010-004-008

Implement Area Calculation Methods with Overloading

Hard

Problem Description

Implement Area Calculation Methods with Overloading

In this problem, you will create a program that reads four values from standard input — a square's side length, a rectangle's width and height, and a circle's radius — then implements three overloaded methods: calculateArea(int side) for a square, calculateArea(int width, int height) for a rectangle, and calculateArea(double radius) for a circle.

Input:

  • Line 1: side (int) — side length of the square
  • Line 2: width (int) — width of the rectangle
  • Line 3: height (int) — height of the rectangle
  • Line 4: radius (double) — radius of the circle

Output:

Square: <square_area>
Rectangle: <rectangle_area>
Circle: <circle_area (2 decimal places)>

Learning Objectives:

  • Understand how method overloading works
  • See how the compiler selects the correct method based on argument type and count
  • Design a clean API by giving a unified name to related operations

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