简体   繁体   中英

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.

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM