Enhanced ComplexNumber class C# Exercise - C# Programming Course

 Exercise

Enhanced ComplexNumber class

 Objetive

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

 Example Code

// 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()}");
    }
}

More C# Exercises of OOP More On Classes

 Array of objects: table
Create a class named "Table". It must have a constructor, indicating the width and height of the board. It will have a method "ShowData" which will wr...
 House
Create a class "House", with an attribute "area", a constructor that sets its value and a method "ShowData" to display "I am a house, my area is 200 m...
 Table + coffetable + array
Create a project named "Tables2", based on the "Tables" project. In it, create a class "CoffeeTable" that inherits from "Table". Its method "ShowDa...
 Encrypter & Decrypter
Create a class "Encrypter" to encrypt and decrypt text. It will have a "Encrypt" method, which will receive a string and return another string. It ...
 Complex numbers
A complex number has two parts: the real part and the imaginary part. In a number such as a+bi (2-3i, for example) the real part would be "a" (2) and ...
 Table + coffetable + leg
Extend the example of the tables and the coffee tables, to add a class "Leg" with a method "ShowData", which will write "I am a leg" and then it will ...
 Catalog
Create the classes diagram and then, using Visual Studio, a project and the corresponding classes for a catalog utility: It will be able to store i...
 Random number
Create a class RandomNumber, with three static methods: - GetFloat will return a number between 0 and 1 using the following algorithm: seed = (s...
 Text to HTML
Create a class "TextToHTML", which must be able to convert several texts entered by the user into a HTML sequence, like this one: Hola Soy yo Ya ...
 Class ScreenText
Create a class ScreenText, to display a certain text in specified screen coordinates. It must have a constructor which will receive X, Y and the strin...
 3D point
Create a class "Point3D", to represent a point in 3-D space, with coordinates X, Y and Z. It must contain the following methods: MoveTo, which will...
 Catalog + Menu
Improve the Catalog program, so that "Main" displays a menu to allow entering new data of any kind, as well as displaying all the data stored....

Juan A. Ripoll - Programming Tutorials and Courses © 2025 All rights reserved.  Legal Conditions.