public boolean fight(Cat anotherCat) {
int stat1 = this.age + this.weight + this.strength;
int stat2 = anotherCat.age + anotherCat.weight + anotherCat.strength;
if (stat1 > stat2 ) {
return true;
}
else return false;
}
Can someone explain why this works please?
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Tyler Begrow
15 January 2020, 21:56
For me, the "this" part through me for a loop until I realized that this is referring to the object that is calling the method. I never would have gotten the answer without seeing this post as the answer lol. So thank you.
+1
Guadalupe Gagnon
8 November 2019, 18:02
The requirements say that if cat1 defeats cat2, then cat2 must not defeat cat1. This code accomplishes this by using all the stats combined together and the higher one will defeat the lower one.
0