What I am trying to do is to have different calculation methods for complex number. I have already created all the calculations for the rational number, and it is required to create complex number by using the rational number class that I used before. Here is how I created the rational part (incomplete version)
public class Rational {
int numerator;
int denominator;
// Task 3: add the missing fields
public Rational(int numerator, int denominator){
this.numerator = numerator;
this.denominator = denominator;
// Task 4: complete the constructor
}
public Rational add(Rational other){
Rational result = new Rational(0,0);
result.denominator = denominator * other.denominator;
result.numerator = (numerator * other.denominator) + (denominator * other.numerator);
return result;
}
......
......
......
public Rational divide(Rational other){
Rational result = new Rational(0,0);
result.denominator = denominator * other.numerator;
result.numerator = numerator * other.denominator;
return result;
// Task 4: complete the method
}
Now my task is to create exactly the same thing as this one, but just in complex number version. Here's what I've created:
public class Complex {
Rational real;
Rational imag;
// Task 6: add the missing fields
public Complex(Rational real, Rational imag){
this.real = real;
this.imag = imag;
// Task 7: complete the constructor
}
public Complex add(Complex other){
Complex result = new Complex(); ///Here is the problem!!!
result.real = real.add(other.real);
result.imag = imag.add(other.imag);
return result;
// Task 7: complete the method
}
when I want to create new complex number to operate the complex addition, I don't know what should I as as the parameters since it has to be based on the rational class. Any help would be appreciated.
public Complex()