-3

how can I call a true or false value created with my "public boolean equals method" in a later method "public static void main(String[] args)"?

public Car(String color, double insurance)
{
    this.color = color;
    this.insurance = insurance;
}

public boolean equals(Car other)
{   

    if (this.color.equals(other.color) && this.insurance.equals(other.insurance))
    {
        return true;
    }
    else
    {
        return false;
    }
}

I get error: cannot invoke equals(double) on the primitive type double

6
  • 1
    (1) equals(Car other) belongs to Car class which means you can invoke them on Car instances like carInstance.euqlas() (2) If you want to override already existing equals method then you need to declare it using equals(Object other) not equals(Car other). Commented Apr 26, 2017 at 16:33
  • if (c1.equals(c2)) -- but your equals method is broken. It should take an Object parameter, not a Car parameter. Commented Apr 26, 2017 at 16:33
  • This code is very odd. I'm struggling to understand what you're going for. The whole body of equals should just be this.color.equals(other.color) && this.insurance.equals(other.insurance;, and you never even call equals. Also, writing if(true) is completely useless. Commented Apr 26, 2017 at 16:33
  • 1
    This question isn't really a duplicate of the one provided... The problem is that the user is calling .equals(..) on a double. The user should instead use this.insurance == other.insurance Commented Apr 26, 2017 at 16:36
  • 1
    @lucasvw There are at least three problems here, and they have mostly been covered in the comments now. I'll add you should use @Override to catch the method signature mismatch. Commented Apr 26, 2017 at 16:37

1 Answer 1

0

You need to call method here:

Car car1;

...
     if(equals(car1)){
...
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.