Enhanced Complexnumber Class - C# Programming Exercise

In this exercise of C#, you need to improve the ComplexNumber class by overloading the + and - operators to allow the addition and subtraction of complex numbers. The + operator overload should allow adding two complex numbers, where the real parts of the numbers are added together, and the imaginary parts are also added. Similarly, the - operator overload should allow subtracting two complex numbers, subtracting both the real and imaginary parts.

This exercise is a great practice for learning how to work with operator overloading in C#, which is an advanced feature that allows custom operations between objects of a class. Additionally, it's an opportunity to improve your understanding of complex numbers and how to implement them in object-oriented programming.

 Category

OOP More On Classes

 Exercise

Enhanced Complexnumber Class

 Objective

Improve the "ComplexNumber" class, so that it overloads the operators + and - to add and subtract numbers.

 Write Your C# Exercise

// Importing the System namespace to handle basic functionalities and console output
using System;

public class ComplexNumber
{
    // Real and imaginary parts of the complex number
    private double real;
    private double imaginary;

    // Constructor to set the real and imaginary parts
    public ComplexNumber(double real, double imaginary)
    {
        this.real = real;
        this.imaginary = imaginary;
    }

    // Getter for real part
    public double Real
    {
        get { return real; }
    }

    // Getter for imaginary part
    public double Imaginary
    {
        get { return imaginary; }
    }

    // Method to return a string representation of the complex number
    public string ToString()
    {
        return $"({real}, {imaginary})";
    }

    // Method to calculate the magnitude of the complex number (|a + bi| = sqrt(a^2 + b^2))
    public double GetMagnitude()
    {
        return Math.Sqrt(real * real + imaginary * imaginary);
    }

    // Overloading the + operator to add two complex numbers
    public static ComplexNumber operator +(ComplexNumber c1, ComplexNumber c2)
    {
        double realSum = c1.real + c2.real;
        double imaginarySum = c1.imaginary + c2.imaginary;
        return new ComplexNumber(realSum, imaginarySum);
    }

    // Overloading the - operator to subtract two complex numbers
    public static ComplexNumber operator -(ComplexNumber c1, ComplexNumber c2)
    {
        double realDifference = c1.real - c2.real;
        double imaginaryDifference = c1.imaginary - c2.imaginary;
        return new ComplexNumber(realDifference, imaginaryDifference);
    }
}

// Auxiliary class with the Main function to test the functionality of the ComplexNumber class
public class Program
{
    public static void Main()
    {
        // Create two ComplexNumber objects
        ComplexNumber complex1 = new ComplexNumber(3, 4);
        ComplexNumber complex2 = new ComplexNumber(1, 2);

        // Display the original complex numbers
        Console.WriteLine("Complex Number 1: " + complex1.ToString());
        Console.WriteLine("Complex Number 2: " + complex2.ToString());

        // Adding the two complex numbers using the overloaded + operator
        ComplexNumber sum = complex1 + complex2;
        Console.WriteLine("Sum: " + sum.ToString());

        // Subtracting the two complex numbers using the overloaded - operator
        ComplexNumber difference = complex1 - complex2;
        Console.WriteLine("Difference: " + difference.ToString());
        
        // Displaying the magnitude of the complex numbers
        Console.WriteLine($"Magnitude of Complex Number 1: {complex1.GetMagnitude()}");
        Console.WriteLine($"Magnitude of Complex Number 2: {complex2.GetMagnitude()}");
    }
}

 Share this C# exercise

 More C# Programming Exercises of OOP More On Classes

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  3D point

    In this exercise of C#, you need to create a class called Point3D to represent a point in 3-D space, with coordinates X, Y, and Z. The class must include the f...

  •  Catalog + Menu

    In this exercise of C#, you need to improve the catalog program so that the Main method displays a menu allowing the user to enter new data of any kind, as wel...

  •  Array of objects: table

    In this exercise, you need to create a class named "Table". This class should have a constructor that accepts the width and height of the table. ...

  •  House

    In this exercise, you need to create a class called "House" with an attribute called "area". The class should have a constructor to set the value of this attri...

  •  Table + coffetable + array

    In this exercise, you need to create a project named "Tables2", based on the "Tables" project. In this new project, you should create a class called "CoffeeTable" tha...

  •  Encrypter & Decrypter

    In this exercise, you need to create a class called "Encrypter" to encrypt and decrypt text. The class will have an "Encrypt" method, whic...