Programming II Complex: overloading operator, computed properties Week04_Lab11 This exercise you will be exploring more
Posted: Mon Jun 06, 2022 6:00 pm
www.int www.int www.double www double www Complex + «computed property>> Modulus $+ «factory property>> Zero <<constructor>> Complex ( : int, real = 0 imaginary = 0 : int) : string + ToString() $+ <<operator>> + (1hs $+ <<operator>> - (1hs $+ «<operator»> == (1hs $+ «<operator>> != (1hs: Complex, rhs: Complex) : Complex Complex, rhs: Complex) : Complex Complex, rhs: Complex): bool Complex, rhs: Complex): bool Methods Description of class members Fields: There are no fields in this type Programming II Complex: overloading operator, computed properties Week04_Lab11 Properties: The first two properties (Real and Imaginary) are auto-implemented and the rest (Modulus, Argument and Zero) are not. These properties are calculated on the fly i.e. whenever they are required they are calculated. Real - this int represents the real part of this type. This is an auto-implemented property, the getter is public and the setter is absent. Auto-implemented property is what we have been using so far. You may not attach code in either the getter or the setter parts. Imaginary - this int represents the imaginary part of this type. This is an auto-implemented property, the getter is public and the setter is absent. Modulus - this double represents the complex modulus or length of this object. It is calculated as the square root of the sum of the square of the Real and Imaginary parts of this type [√Real² + Imaginary³]. The getter is public Both Modulus and Argument are computed property and Zero is a factory property. and the setter is absent. Argument - this double represents the complex argument or the angle it make with the horizontal axis. It is calculated as the inverse tan of the ratio of the Real to the Imaginary part of this type [tan-¹(Imaginary/Real)]. The getter is public and the setter is absent. Zero - this static property returns a new complex object with both the real and the imaginary parts equal to 0. The getter is public and of course there is no setter. Constructor: Complex (int real, int imaginary) - This is constructor assigns the arguments to the appropriate field Methods ToString() - This is a public method overrides the corresponding method in the object class to return a stringify form of the object. For this you return the Real and the Imaginary properties as an ordered pair.
Complex: overloading operator, computed properties Week04_Lab11 The operator that you want to overload The return type First operand Second operand public static Complex operator +(Complex lhs, Complex rhs) Signals that you are overloading an operator +- You will overload the plus operator to add the two numbers. Copy the code below into your type declaration to overload the + operator. public static Complex operator (Complex bs, Complex cbs.) { int real lbs Real + chs Real; = int imaginary = lbs Imaginary + cbs.Imaginary: return new orlex(real, imaginary); } -- You will also overload the minus operator. Examine the code above and then try to implement this operator. Some operators have to be overloaded in pairs. This is one of them. === You will also overload the equal-equal operator. Examine the code above and then try to implement this operator. What should the return type of the method be? So you will also have to overload the == and the != operator at the same time wwwww Programming II Operators: Programming II Complex: overloading operator, computed properties Week04_Lab11 Test Harness Insert the following code statements in your Program.cs file: Complex ce = new Semelex(-2, 3); Complex c1 = new Semelex(-2, 3); Complex c2= new Complex(1, -2); Console.WriteLine($"{co}"); Console.WriteLine(c1); Console.WriteLine(c2); Console.WriteLine($"{c1} + {c2} = {C1 + c2}"); Console.WriteLine($"{c1} - {c2} = {C1 - c2}"); Complex c3 = C1 + c2; Console.WriteLine($"{c3} in polar form is {c3 Modulus:f2}cis({c3. Argument:f2})"); Console.WriteLine($"{co} {(co == <£₂2 "="i "!=")} {<1}"); Console.WriteLine($"{co} {(ce == c2_2"=""!=")} {<2}"); Additional tasks 1. Try to overload the * operator. Multiplication of two complex numbers is defined by the following relation: <a, b> *<c, d> = <ac-bd, ad+bc>. Insert the proper code statements in your main to show the operation of this operator. 2. Try to overload the unary - operator. This operator simply changes the sign of the operand: -<a, b> = <-a, -b>. Again, insert the proper code statements in your main to show the operation of this operator.
Programming II Complex: overloading operator, computed properties Week04_Lab11 This exercise you will be exploring more about properties and be introduced to overloading operators. The Complex class We are going to model a type that will behave like a complex number in higher mathematics. We will try to reduce the complexity of this type by not implementing all of the normal behaviors. There are 11 members as shown in the class diagram below. Complex Class Properties + <property setter absent»> Real + "<property setter absent» Imaginary + <<computed property>> Argument Complex: overloading operator, computed properties Week04_Lab11 The operator that you want to overload The return type First operand Second operand public static Complex operator +(Complex lhs, Complex rhs) Signals that you are overloading an operator +- You will overload the plus operator to add the two numbers. Copy the code below into your type declaration to overload the + operator. public static Complex operator (Complex bs, Complex cbs.) { int real lbs Real + chs Real; = int imaginary = lbs Imaginary + cbs.Imaginary: return new orlex(real, imaginary); } -- You will also overload the minus operator. Examine the code above and then try to implement this operator. Some operators have to be overloaded in pairs. This is one of them. === You will also overload the equal-equal operator. Examine the code above and then try to implement this operator. What should the return type of the method be? So you will also have to overload the == and the != operator at the same time wwwww Programming II Operators: Programming II Complex: overloading operator, computed properties Week04_Lab11 Test Harness Insert the following code statements in your Program.cs file: Complex ce = new Semelex(-2, 3); Complex c1 = new Semelex(-2, 3); Complex c2= new Complex(1, -2); Console.WriteLine($"{co}"); Console.WriteLine(c1); Console.WriteLine(c2); Console.WriteLine($"{c1} + {c2} = {C1 + c2}"); Console.WriteLine($"{c1} - {c2} = {C1 - c2}"); Complex c3 = C1 + c2; Console.WriteLine($"{c3} in polar form is {c3 Modulus:f2}cis({c3. Argument:f2})"); Console.WriteLine($"{co} {(co == <£₂2 "="i "!=")} {<1}"); Console.WriteLine($"{co} {(ce == c2_2"=""!=")} {<2}"); Additional tasks 1. Try to overload the * operator. Multiplication of two complex numbers is defined by the following relation: <a, b> *<c, d> = <ac-bd, ad+bc>. Insert the proper code statements in your main to show the operation of this operator. 2. Try to overload the unary - operator. This operator simply changes the sign of the operand: -<a, b> = <-a, -b>. Again, insert the proper code statements in your main to show the operation of this operator.