Paincakes
Paincakes

Reputation: 147

No Suitable Constructor Found (Java)

I am trying to compile my program here, but I am running into a small error and the error is "no suitable constructor found for ComplexNumber(double)." Here is my code so far.

public class ComplexNumber extends ImaginaryNumber
{
    private double realCoefficient;

    public ComplexNumber ( )
    {
        super ( );
        this.realCoefficient = 1;
    }

    public ComplexNumber (double r, double i)
    {
        super (i);
        this.realCoefficient = r;
    }

    public ComplexNumber add (ComplexNumber another)
    {
        return new ComplexNumber (this.realCoefficient + another.realCoefficient); //the error at complile occurs here, right at new.
    }//More Codes

I have had this problem once, and that was because I did not have a parameterized constructor. However this time, I do have one. So I have no idea what is the problem this time.

Here is my code for the ImaginaryNumber

public class ImaginaryNumber implements ImaginaryInterface
{
//Declaring a variable.
protected double coefficient;

//Default constructor.
public ImaginaryNumber( )
{
    this.coefficient = 1;
}

//Parameterized constructor.
public ImaginaryNumber(double number)
{
    this.coefficient = number;
}

//Adding and returing an imaginary number.
public ImaginaryNumber add (ImaginaryNumber another)
{
    return new ImaginaryNumber(this.coefficient + another.coefficient);
}//More Codes

My ImaginaryNumber class works fine.

Upvotes: 1

Views: 1910

Answers (2)

rgettman
rgettman

Reputation: 178253

In the add method, you are attempting to pass exactly one parameter to a constructor, but you only have constructors that take 0 or 2 parameters.

It looks like you need to add the imaginary parts of the ComplexNumber anyway, so place that imaginary part addition in as the second parameter to the constructor.

Using the coefficient protected variable from Imaginary:

return new ComplexNumber (this.realCoefficient + another.realCoefficient,
                          this.coefficient + another.coefficient);

Upvotes: 2

Steve Knabe
Steve Knabe

Reputation: 120

Java is looking for a constructor of just:

public ComplexNumber(double d){
   //to-do
}

You will need to make a constructor that fits those arguments.

Upvotes: 2

Related Questions