I have got this to pass all qualifications except the third, which is an arbitrary "implement fight method based on weight, age, and strength", which I have done, comparing each stat separately. And this code seems to work. Pardon all my //comments. Without any additional cues as to what I am doing wrong I am clueless. Thanks in advance for any help!!
package en.codegym.task.jdk13.task05.task0501;
// CG TASK : "Cat carnage (3)"
public class Solution {
public static void main(String[] args) {
Cat cat1 = new Cat("Al", 4, 8, 15); // COMPARING OBJECT STATES
Cat cat2 = new Cat("Bo", 9, 10, 30); // ~~~~~~~~~~~~~~~~~~~~~~~ // OUTPUT
Cat cat3 = new Cat("Ce", 12, 16, 20);
System.out.println(cat1.fight(cat2)); // F
System.out.println(cat2.fight(cat1)); // T
System.out.println(cat2.fight(cat3)); // WHY IS THIS NOT FALSE??? <<< Where logic failed // F
System.out.println(cat3.fight(cat2)); // -stronger = true See below... // T
System.out.println(cat3.fight(cat1)); // -older = false // T
System.out.println(cat1.fight(cat3)); // -fatter = false // F
}
public static class Cat {
private String name; // INSTANCE VARIABLES
private int age, weight, strength;
//private boolean stronger, older, fatter = false; // Used to compare each stat separately.
public Cat(String name, int age, int weight, int strength) {
this.name = name; this.age = age; this.weight = weight; this.strength = strength; // CONSTRUCTOR
}
public boolean fight(Cat anotherCat) {
int numOfBetterStats = 0; // Needed to quantify num of better stats.
if (this.strength > anotherCat.strength) {
numOfBetterStats++; //this.stronger = true;
}
if (this.age > anotherCat.age) {
numOfBetterStats++; //this.older = true;
}
if (this.weight > anotherCat.weight) {
numOfBetterStats++; //this.fatter = true;
}
boolean result = (numOfBetterStats > 1) ? true : false;
return result;
// if (this.stronger && this.fatter) return true; // <<< FLAWED LOGIC!!!
// else if (this.fatter && this.older) return true; // Fails if there's not 2 "true"s.
// else if (this.older && this.stronger) return true;
// else return false;
}
//----------------------------------------------------------------------------------------------
// public boolean fight(Cat anotherCat) {
// if (this.strength + this.age + this.weight == anotherCat.strength + anotherCat.age + anotherCat.weight) return false;
// else if (this.strength + this.age + this.weight > anotherCat.strength + anotherCat.age + anotherCat.weight) {
// //System.out.println("Win");
// return true;
// }
// else {
// //System.out.println("Lose");
// return false;
// }
// }
}
}