Recap - Shape Calculator
Part of the Object Oriented Programming section of Coddy's C# journey — lesson 30 of 70.
Challenge
EasyLet'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 calledIShapein theShapesnamespace. Every shape must be able to calculate its area and describe itself. Declare two methods:CalculateArea()returning adouble, andGetDescription()returning astring.Circle.cs: Create aCircleclass in theShapesnamespace that implementsIShape. A circle has aRadiusproperty (double) set through its constructor. The area formula isMath.PI * Radius * Radius. The description should return"Circle with radius {Radius}".Rectangle.cs: Create aRectangleclass in theShapesnamespace that implementsIShape. A rectangle hasWidthandHeightproperties (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 ofIShapereferences. 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.00Notice 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
1Fundamentals of OOP
External FilesNamespaces & DirectivesIntro to Classes & ObjectsThe 'this' KeywordMethods and ParametersFields vs PropertiesConstructorsObject InitializersRecap - Simple Calculator4Inheritance
Basic Inheritance (:) SyntaxThe 'base' KeywordVirtual & Override KeywordsSealed ClassesThe 'object' Base ClassRecap - Employee Hierarchy7Advanced Features
Operator OverloadingIndexers (this[])ToString() OverrideExtension MethodsRecap - Custom List2Properties & Static Members
Auto-Implemented PropertiesRead/Write-Only PropertiesStatic Fields & MethodsStatic ClassesExpression-Bodied Members5Polymorphism & Interfaces
Compile vs Runtime PolyInterface vs Abstract ClassMultiple InterfacesExplicit InterfacesUpcasting & DowncastingRecap - Shape Calculator