Overloaded Method to Calculate Area

C# Methods: Overloaded Method to Calculate Area (Circle, Rectangle, Triangle)


Introduction of the Lesson

In C# programming, methods are essential building blocks that allow developers to organize logic into reusable units. One powerful feature of methods is method overloading, which enables multiple methods to share the same name but differ in parameters. This concept improves code readability, maintainability, and flexibility.

In this lesson, you will learn how to use method overloading to calculate the area of different geometric shapes—circle, rectangle, and triangle—using a single method name. The program will accept user input, making it interactive and practical for beginners. By the end of this lesson, you will have a strong understanding of how overloading works and how to apply it in real-world programming scenarios.


Lesson Objectives

Before diving into coding, it is important to understand the learning goals of this lesson. These objectives follow the Outcome-Based Education (OBE) framework to ensure structured learning progression.

This lesson aims to guide you from understanding the concept of method overloading to applying it in a working C# program. You will not only learn the theory but also practice implementing it and applying it to solve real problems.

Objectives:

  1. Understand
    Explain what method overloading is and how it works in C#.
  2. Learn
    Identify how different parameter lists allow multiple methods with the same name.
  3. Practice
    Implement overloaded methods to calculate the area of different shapes.
  4. Apply
    Develop a complete program that accepts user input and calculates areas dynamically.

Understanding Method Overloading

Method overloading allows you to define multiple methods with the same name but different parameter lists. The compiler determines which method to call based on:

  • Number of parameters
  • Type of parameters
  • Order of parameters

This approach avoids creating multiple method names such as AreaCircle, AreaRectangle, etc., and instead uses a unified method name like Area().


Beginner-Friendly C# Source Code

Below is a complete C# program demonstrating method overloading with user input.

using System;

namespace AreaCalculatorApp
{
    class Program
    {
        // Overloaded method for Circle
        static double Area(double radius)
        {
            return Math.PI * radius * radius;
        }

        // Overloaded method for Rectangle
        static double Area(double length, double width)
        {
            return length * width;
        }

        // Overloaded method for Triangle
        static double Area(double baseValue, double height, string shape)
        {
            return 0.5 * baseValue * height;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("AREA CALCULATOR USING METHOD OVERLOADING");
            Console.WriteLine("Choose shape: ");
            Console.WriteLine("1. Circle");
            Console.WriteLine("2. Rectangle");
            Console.WriteLine("3. Triangle");
            Console.Write("Enter choice: ");

            int choice = Convert.ToInt32(Console.ReadLine());

            switch (choice)
            {
                case 1:
                    Console.Write("Enter radius: ");
                    double radius = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("Area of Circle: " + Area(radius));
                    break;

                case 2:
                    Console.Write("Enter length: ");
                    double length = Convert.ToDouble(Console.ReadLine());
                    Console.Write("Enter width: ");
                    double width = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("Area of Rectangle: " + Area(length, width));
                    break;

                case 3:
                    Console.Write("Enter base: ");
                    double baseValue = Convert.ToDouble(Console.ReadLine());
                    Console.Write("Enter height: ");
                    double height = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("Area of Triangle: " + Area(baseValue, height, "triangle"));
                    break;

                default:
                    Console.WriteLine("Invalid choice!");
                    break;
            }

            Console.ReadLine();
        }
    }
}

Step-by-Step Instructions

  1. Open Visual Studio or any C# IDE
  2. Create a new Console Application
  3. Copy and paste the code above
  4. Run the program
  5. Choose a shape and enter the required values

Example Output

AREA CALCULATOR USING METHOD OVERLOADING
Choose shape:
1. Circle
2. Rectangle
3. Triangle
Enter choice: 1
Enter radius: 5
Area of Circle: 78.5398163397448

Lesson Summary

Method overloading in C# allows developers to create multiple methods with the same name but different parameter lists, improving code clarity and flexibility. In this lesson, you implemented overloaded methods to calculate the area of a circle, rectangle, and triangle using user input. This approach demonstrates how a single method name can handle multiple tasks efficiently. By mastering this concept, you can write cleaner and more scalable programs.


Multiple Choice Questions

  1. What is method overloading?
    A. Using multiple classes
    B. Using the same method name with different parameters
    C. Creating loops
    D. Declaring variables
  2. Which factor determines method overloading?
    A. Method name only
    B. Return type only
    C. Parameter list
    D. Variable name
  3. Which method call computes the area of a rectangle?
    A. Area(5)
    B. Area(5, 10)
    C. Area(5, 10, “triangle”)
    D. Area()
  4. Why is method overloading useful?
    A. Reduces code readability
    B. Avoids using methods
    C. Improves code organization
    D. Removes variables
  5. Which scenario best uses method overloading?
    A. Printing text
    B. Calculating different shapes with same method name
    C. Declaring constants
    D. Writing comments

Exercises, Assessment, and Lab Exam

To strengthen your understanding of method overloading, it is essential to engage in hands-on exercises. These activities are designed to progressively enhance your coding skills—from basic modification to full application development.

Exercises

  1. Add a new overloaded method to calculate the area of a square
  2. Modify the program to display results up to 2 decimal places
  3. Add input validation to prevent negative values

Assessment Task

  • Create a program that uses method overloading to compute:
    • Perimeter of shapes (circle, rectangle, triangle)
  • Include user input and menu selection

Lab Exam Task

  • Develop a complete Shape Calculator System:
    • Must include at least 5 overloaded methods
    • Should support area and perimeter calculations
    • Implement error handling
    • Use looping menu system

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

, , , , , , , , ,

Post navigation