Реализованный код попарно проверяет каждый параметр котов и работает со счётчиками, для наглядности (без них в иной вариации тоже не проходит), в случае когда они равны то мы переходим к сравнению по их хэш-кодам, которые уникально определяются по заданной формуле..
package com.javarush.task.task05.task0502;
/*
Реализовать метод fight
*/
import java.util.Objects;
public class Cat {
public Integer age;
public Integer weight;
public Integer strength;
public Cat() {
this.weight = 0;
this.strength = 0;
this.age = 0;
}
public boolean fight(Cat anotherCat) {
Integer fightingScoreCat1 = 0;
Integer fightingScoreCat2 = 0;
if (this.strength.compareTo(anotherCat.strength) > 0)
fightingScoreCat1++;
else
fightingScoreCat2++;
if (this.age.compareTo(anotherCat.age) > 0)
fightingScoreCat1++;
else
fightingScoreCat2++;
if (this.weight.compareTo(anotherCat.weight) > 0)
fightingScoreCat1++;
else
fightingScoreCat2++;
if (fightingScoreCat1.compareTo(fightingScoreCat2) > 0)
return true;
else
return this.hashCode() > anotherCat.hashCode();
}
@Override
public int hashCode() {
return this.weight + this.age * 10 + this.strength * 100;
}
public static void main(String[] args) {
Cat cat1 = new Cat();
Cat cat2 = new Cat();
cat1.age = 1;
cat1.weight = 0;
cat1.strength = 1;
cat2.age = 1;
cat2.weight = 1;
cat2.strength = 0;
cat1.fight(cat2);
cat2.fight(cat1);
}
}