0

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.

1
  • You need to have a constructor without any parameters. public Complex() Commented Oct 3, 2019 at 16:50

1 Answer 1

1

Why not

public Complex add(Complex other){
    Complex result = new Complex(other.real, other.imag);
    return result;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply. But I still wondering how to utilize this in the main method. When I create like a example in main method (let's say, 3 for real part, and 2i for imagine part, how do I make a new complex number if it requires to insert rational number format?
Looking at your code, it seems that you have to create Rational values first, from which you can create Complex Objects.
That's the part where I'm struggling. If I need to create Rational value in the complex number class, should I create it in the class, or in the constructor?
Sorry, no idea, why don't you try it and see?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.