What I also do not understand is that you make a cat1 and a cat2 object.
you fill the object with data: strength, age, weight.
and then a complete another object (anotherCat) can use the data of cat2.
I hope you can help me..
Still do not get it..
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
rajesh
10 January 2019, 11:48
package com.codegym.task.task05.task0502;
/*
Implement the fight method
*/
public class Cat {
public int age;
public int weight;
public int strength;
public int point = 0;
public Cat() {
this.age = age;
this.weight = weight;
this.strength = strength;
}
public boolean fight(Cat anotherCat) {
//write your code here
if(this.age > anotherCat.age){
this.point--;
anotherCat.point++;
}
if (this.weight > anotherCat.weight){
this.point++;
anotherCat.point--;
}
if (this.strength > anotherCat.strength ){
this.point++;
anotherCat.point--;
}
if (this.point>anotherCat.point){
return true;
}else {
return false;
}
}
public static void main(String[] args) {
Cat cat1 = new Cat();
cat1.strength = 10;
cat1.weight = 15;
cat1.age = 15;
Cat cat2 = new Cat();
cat2.strength = 5;
cat2.weight = 16;
cat2.age = 14;
System.out.println(cat1.fight(cat2));
System.out.println(cat2.fight(cat1));
}
}
+1
Sabine Meijran-Prins
6 January 2019, 20:00
Thank you Syed, this was very helpfull...
0
vikr@nt
31 January 2019, 01:27
Ok Sabine, though you understood but in simple words here again. In anotherCat variable, reference of cat2 is being passed i.e. it's attributes, so that they can be compared with the current cat object i.e. cat1
Also, why cant you do this and compare i and j based on the values added . i and j are local variables in the method fight
int i=this.weight+this.strength+this.age;
int j=anotherCat.age+anotherCat.weight+anotherCat.strength;
Additionally I'd like to understand the significance of this code please
public Cat() {
this.age = age;
this.weight = weight;
this.strength = strength;
}
0
Syed Tayyab ul Mazhar
6 January 2019, 11:40useful
This Cat anotherCat doesn't create an object. anotherCat is just a variable of type Cat, and it can hold the reference of any object of Cat class.
similary when we do cat1.fight(cat2), this is what happens :
If you understand now, then please give a response. +3