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
C# Exercise Example
// 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()}");
}
}
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#.
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...
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...
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. ...
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...
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...
In this exercise, you need to create a class called "Encrypter" to encrypt and decrypt text. The class will have an "Encrypt" method, whic...