-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

Deebee
  • 1
  • 1
  • 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)`. – Pshemo Apr 26 '17 at 16:33
  • `if (c1.equals(c2))` -- but your equals method is broken. It should take an Object parameter, not a Car parameter. – Hovercraft Full Of Eels Apr 26 '17 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. – Carcigenicate Apr 26 '17 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` – lucasvw Apr 26 '17 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. – Elliott Frisch Apr 26 '17 at 16:37
  • @Deebee: then sorry to say but your teacher may be a bit confused on Java standards. – Hovercraft Full Of Eels Apr 26 '17 at 16:52

1 Answers1

0

You need to call method here:

Car car1;

...
     if(equals(car1)){
...
}
Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38