package com.codegym.task.task05.task0502;
/*
Implement the fight method
*/
public class Cat {
public int age;
public int weight;
public int strength;
public Cat() {
}
public boolean fight(Cat anotherCat) {
//write your code here
int cp = 0;
if ((age > 1) && (age < 6))
cp = cp + 2;
else if (age <= 1)
cp-=2;
else if (age > 6)
cp-=1;
if (weight < 4)
cp = cp + 0;
else if ((weight < 20) && (weight >= 4))
cp = cp + 3;
else if (weight > 20)
cp++;
if (strength < 40)
cp = cp + 20;
else if ((strength >= 40) && (strength <= 99))
cp+=50;
else if (strength >= 100)
cp+=100;
if (cat1.cp > cat2.cp)
return true;
else
return false;
}
public static void main(String[] args) {
Cat cat1 = new Cat();
cat1.age = 8;
cat1.weight = 15;
cat1.strength = 75;
Cat cat2 = new Cat(8,20,63);
cat2.age = 8;
cat2.weight = 20;
cat2.strength = 63;
cat1.fight(cat2);
cat2.fight(cat1);
}
}
This is the part I am struggling with a bit. I am having a hard time wrapping my head around when to use "this.some_name_here", and where and how calling certain objects in relation to other areas. This code is failing by saying it cannot find symbol 'cat1' on line 49. I have no idea what that means. As far as I can tell I have set the structure up right, but I may have a gap in my knowledge somewhere here. I will write what I think is happening and maybe somebody can point out where I am incorrect.
1) A public class is created called Cat.
2) In that class, there are three instance variable set ( age, weight, and strength), but they haven't been initiated.
3) within the class there is a public Cat() method. (I have no idea why there is a Cat() method here? Not sure what I should be doing with it?) (*note* could it be possible to use the Cat() method like cat1.Cat(8,15,75) to fill in the Cat class fields instead of writing the lines like cat1.age = 15?
4) There is a public boolean fight() method that accepts the parameters (Cat anotherCat)
5) in that method I made a few if statements that add cp points based on age weight and strength (I know this is a bit beyond the scope of the exercise, but I was having fun with it.)
6)At the end of the fight() method I attempted to determine if the cat1 cp was higher than the cat2 cp. ( I am fairly certain this is at least one of the places I have messed up. I am not sure if cp is ok to be a local variable within the method, or if I need to add it to the Cat class. I am also not sure if I am even referencing it correctly if it is ok as a local variable.
package com.codegym.task.task05.task0502;
/*
Implement the fight method
*/
public class Cat {
public int age;
public int weight;
public int strength;
public Cat() {
}
public boolean fight(Cat anotherCat) {
//write your code here
int cp = 0;
if ((age > 1) && (age < 6))
cp = cp + 2;
else if (age <= 1)
cp-=2;
else if (age > 6)
cp-=1;
if (weight < 4)
cp = cp + 0;
else if ((weight < 20) && (weight >= 4))
cp = cp + 3;
else if (weight > 20)
cp++;
if (strength < 40)
cp = cp + 20;
else if ((strength >= 40) && (strength <= 99))
cp+=50;
else if (strength >= 100)
cp+=100;
if (cat1.cp > cat2.cp)
return true;
else
return false;
}
public static void main(String[] args) {
Cat cat1 = new Cat();
cat1.age = 8;
cat1.weight = 15;
cat1.strength = 75;
Cat cat2 = new Cat(8,20,63);
cat2.age = 8;
cat2.weight = 20;
cat2.strength = 63;
cat1.fight(cat2);
cat2.fight(cat1);
}
}