0
import java.util.*;

public class Digimon1{

private String name;
private int hitPoints;
private double attackSpeed;

public Digimon1(String name, int hitPoints, double attackSpeed){
  this.name = name;
  this.hitPoints = hitPoints;
  this.attackSpeed = attackSpeed;

}
public String getName(){
   return this.name;
}
public double getAttackSpeed(){
   return this.attackSpeed;
}



public boolean equals(Object a){
   Digimon1 o = (Digimon1) a;
   if (this.name == o.getName() && this.attackSpeed == o.getAttackSpeed()){
      return true;
}else{
   return false;
     }
}
public static void main(String[] args){
   List <Digimon1> digimon = new ArrayList<>();
   Digimon1 gatamon = new Digimon1("Gatamon", 500, 12.9);
   digimon.add(gatamon);

   Digimon1 agumon = new Digimon1("agumon", 53, 9.8);//agumon
   digimon.add(gatamon);

  Digimon1 agumon1 = new Digimon1("agumon", 53, 9.8);//agumon1
  digimon.add(gatamon);

  System.out.print(agumon.equals(agumon1));



}


 }

So my codes equals method should output a "true, false, true" when 2 digimons have the same name and attackspeeed. But what i understand is that my equals can return if my object is true or or false. how do i return three boolean statments?

  • 6
    You don't. the equals method, or any overriden version of it, can only return a single boolean version. That doesn't mean you can't create your own equals that returns an array of booleans. I would recommend giving it another name, though, if only to avoid confusion – Stultuske Feb 21 '20 at 13:55
  • Use a function which is not `equals` – al3c Feb 21 '20 at 13:55
  • Your understanding is correct. `equals` should return _one_ `boolean` value. Why should you output `true, false, true`? – Sweeper Feb 21 '20 at 13:56
  • I would do it this way. Create 3 methods like `equalsName(), equalsSpeed(), equalsHits()` and you could call them in your overridden `equals` method like `equalsName() && equalsSpeed() && equalsHits()`, if you want you can individually call them too. – SomeDude Feb 21 '20 at 13:59
  • And you need to check object type in equals before accessing fields, `if (a instanceof Digimon1)` – rpc1 Feb 21 '20 at 14:04
  • 2
    What use is a return value of `[true, false, true]`? It is unspecified which field has the `false` value, and even if it was documented, it is rigid and fragile, making future changes nearly impossible to keep backward compatible. – Andreas Feb 21 '20 at 14:05
  • Caution: `this.name == o.getName()` will eventually fail. See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. – VGR Feb 21 '20 at 16:11

3 Answers3

1

If I understand you in a correct way and you just really want to see 3 boolean as a result. This is the easiest way to achieve that.

You can create bool array,

 boolean array[];
 array = new boolean[3];
  // then you can set the result to the array like this,

     if(this.name == o.getName())
            array[0] = true; 
    if(this.attackSpeed == o.getAttackSpeed())
            array[1] = true; 
//...

    return array;

and so on. after control statements. I think you can write whole version then you can return your array

Ugur Tufekci
  • 350
  • 2
  • 13
  • 2
    Well, that ain't Java code. --- `boolean array[3];` fails to compile. --- `this.name == o.getName()` [How do I compare strings in Java?](https://stackoverflow.com/q/513832/5221149) – Andreas Feb 21 '20 at 14:08
  • I just use his code and I tried to show him how he should think this problem I didn't even check how he try to compare cause he didn't ask anything about comparing but its my bad :) @Andreas – Ugur Tufekci Feb 21 '20 at 14:13
  • 1
    `array[0] = this.name.equals(o.getName()); ` – rpc1 Feb 21 '20 at 14:15
0

The other answers have already pointed out that you can't and also shouldn't think of using Object.equals for that. One solution is to use a custom method returning a boolean array, but you could also look into EnumSets:

enum DigimonEquality {
    NAME, HP, ATTACK;
}

public class Digimon {

    private String name;
    private int hitPoints;
    private double attackSpeed;

    public Digimon(String name, int hitPoints, double attackSpeed) {
        super();
        this.name = name;
        this.hitPoints = hitPoints;
        this.attackSpeed = attackSpeed;
    }

    public EnumSet<DigimonEquality> checkEquality(Digimon that) {
        EnumSet<DigimonEquality> result = EnumSet.noneOf(DigimonEquality.class);
        if (this.name.equals(that.name))
            result.add(DigimonEquality.NAME);
        // etc.
        return result;
    }

    public static void main(String[] args) {
        List<Digimon> digimon = new ArrayList<>();
        Digimon gatamon = new Digimon("Gatamon", 500, 12.9);
        digimon.add(gatamon);

        Digimon agumon = new Digimon("agumon", 53, 9.8);// agumon
        digimon.add(gatamon);

        Digimon agumon1 = new Digimon("agumon", 53, 9.8);// agumon1
        digimon.add(gatamon);

        EnumSet<DigimonEquality> equality = agumon.checkEquality(agumon1);
        System.out.println(
                Arrays.stream(DigimonEquality.values()).map(equality::contains).collect(Collectors.toList()));

    }

}

A custom method also can only accept Digimon s.t. you don't need to check/cast the type, as you would in the equals method.

EDIT: because OP is struggling with the equals implementation and maybe people new to Java read this, I want to add that you should ofc also override hashCode when you override equals

sfiss
  • 2,119
  • 13
  • 19
-1

You can implement Object#equals(Object obj), but you can't change count of arguments or it signature. Implement own method, that verify multiple objects, it can be a right way.

sq179
  • 1
  • 1
    *"you can't change count of arguments or it signature"* --- Sure you can, it's called method overloading. – Andreas Feb 21 '20 at 14:06
  • @Andreas, i'm talking about `Object#equals`. – sq179 Feb 21 '20 at 14:15
  • But question is just asking about *an* `equals` method, not necessarily the `Object#equals` method. The question code didn't even add `@Override`. So saying that you cannot change the signature is misleading, because you *can*. – Andreas Feb 21 '20 at 14:17
  • @Andreas, take a look on title of question: *Java* equals method. – sq179 Feb 21 '20 at 14:27
  • Yes, the question is about Java, not C, C#, Python, JavaScript, or any other programming language. What's your point? – Andreas Feb 21 '20 at 14:35
  • Or about function, that commonly developed by *Java* ? – sq179 Feb 21 '20 at 14:35