1) Suppose we have a preexisting Rational class with methods add, subtract, mult
ID: 3726684 • Letter: 1
Question
1) Suppose we have a preexisting Rational class with methods add, subtract, multiply and divide. Now we want to build a Complex class with instance variables b and c of type Rational. We can think of this a representing a complex number b + c i, where i is the square root of -1. b is called the real part of the complex number and the value c is called the imaginary part.
a ) What is the OOP term for the association between Rational and Complex when we build a Complex object using Rational objects in this manner? _______________________________________ (11 letters begins with ‘c’ )
b) To multiply two complex numbers we multiply individual parts as follows:
(b1 + c1i)(b2 + c2i) = b1b2 + (c1c2)i2 + (b1c2 + b2c1)i = b1b2 – c1c2 + (b1c2 + b2c1) i
That’s to say, b1b2 – c1c2 is the real part of the result and b1c2 + b2c1 is the imaginary part. Your job is to complete the Complex times method below. That method returns a new Complex object equal to product of this and other. To compute the result , call Rational methods plus, minus and times (e.g. call b1.times(b2) to get b1 times b2.) You have access to a Complex constructor that takes two parameters for initializing b and c, respectively. That’s all you need; you won’t be using *, + or – anywhere in your code. (It’s possible, though not required, to complete this question by adding a single return statement to code below)
Complex times(Complex other)
{
Rational b1 = this.b, c1 = this.c, b2 = other.b, c2 = other.c;
}
Explanation / Answer
a) Word is : Composition
b)
Complex times(Complex other)
{
Rational b1 = this.b, c1 = this.c, b2 = other.b, c2 = other.c;
Return (Complex result(sub(b1.times(b2), c1.times (C2)), add(b1.times(C2),b2.times(C1))));
}
Here result is the object of Complex class. Here complex class constructor will be called which will have two arguments b1b2-c1c2 as real part and b1c2+b2c1 as complex part.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.