Menu
Coddy logo textTech

Recap - Shape Calculator

Part of the Object Oriented Programming section of Coddy's C# journey — lesson 30 of 70.

challenge icon

Challenge

Easy

Let's build a shape calculator that brings together everything you've learned about interfaces and polymorphism. You'll create a system where different shapes can be processed uniformly through a common interface, demonstrating the power of runtime polymorphism.

You'll organize your code across four files:

  • IShape.cs: Define an interface called IShape in the Shapes namespace. Every shape must be able to calculate its area and describe itself. Declare two methods: CalculateArea() returning a double, and GetDescription() returning a string.
  • Circle.cs: Create a Circle class in the Shapes namespace that implements IShape. A circle has a Radius property (double) set through its constructor. The area formula is Math.PI * Radius * Radius. The description should return "Circle with radius {Radius}".
  • Rectangle.cs: Create a Rectangle class in the Shapes namespace that implements IShape. A rectangle has Width and Height properties (both double) set through its constructor. The area is simply width times height. The description should return "Rectangle {Width}x{Height}".
  • Program.cs: In your main file, create a circle and a rectangle using input values, then store them in an array of IShape references. Loop through the array and for each shape, print its description followed by its area rounded to two decimal places. This demonstrates how you can process completely different shapes through the same interface!

You will receive three inputs:

  • Circle radius (double)
  • Rectangle width (double)
  • Rectangle height (double)

Print the output in this format for each shape:

{GetDescription() result}
Area: {CalculateArea() rounded to 2 decimal places}

For example, if the inputs are 5, 4, and 6, the output should be:

Circle with radius 5
Area: 78.54
Rectangle 4x6
Area: 24.00

Notice how your loop doesn't need to know whether it's dealing with a circle or rectangle - it simply calls the interface methods and lets polymorphism handle the rest. This is the elegance of programming to interfaces!

Try it yourself

using System;
using Shapes;

class Program
{
    public static void Main(string[] args)
    {
        // Read input values
        double circleRadius = Convert.ToDouble(Console.ReadLine());
        double rectWidth = Convert.ToDouble(Console.ReadLine());
        double rectHeight = Convert.ToDouble(Console.ReadLine());
        
        // TODO: Create a Circle object with the given radius
        
        // TODO: Create a Rectangle object with the given width and height
        
        // TODO: Create an array of IShape containing both shapes
        
        // TODO: Loop through the array and for each shape:
        // - Print the description using GetDescription()
        // - Print "Area: " followed by CalculateArea() rounded to 2 decimal places
    }
}

All lessons in Object Oriented Programming