package fr.codegym.task.task05.task0502;
/*
Implémenter la méthode combattre
*/
public class Chat {
public int age;
public int poids;
public int force;
public Chat() {
}
public boolean combattre(Chat autreChat) {
//écris ton code ici
Chat chat1 = new Chat();
Chat chat2 = new Chat();
chat1.age = 5;
chat1.poids = 6;
chat1.force = 10;
chat2.age = 3;
chat2.poids = 4;
chat2.force = 5;
//chat1.combattre(chat2);
if (chat1.force > chat2.force)
return true;
if (chat2.force < chat1.force)
return false;
return false;
}
public static void main(String[] args) {
}
}
I give my tongue to...the cat (french expression)
Résolues
Commentaires (11)
- Populaires
- Nouveau
- Anciennes
Tu dois être connecté(e) pour laisser un commentaire
Gellert Varga
2 juillet 2021, 20:26
3.)
The creation of the objects:
Chat chat1 = new Chat();
and the assigning values to the fields of the objects:
chat1.age = 5;
all these should be written in the main() method, not in the combratte() instance method.
+1
BenOitB
4 juillet 2021, 09:09
Thx for all your answers but here, regarding the 3rd point,
The requirement specify clearly that objects and their attributes must be in the combattre() method. I passed the exercice as i follow your advices and Lupe's answer too.
Here is my functionnal code :
+1
Guadalupe Gagnon
4 juillet 2021, 15:14utile
just fyi, lines 18-26 don't do anything as you don't reference either object created on these lines. You can remove them and the code works exactly the same.
+2
Gellert Varga
4 juillet 2021, 22:23utile
BenOitB:
OK. I'm glad you managed to solve the problem well!:)
And I must apologize if I have given you partly stupid advices...
But it is possible that the English version has different requirements.
We did not have to put the objects in the 'fight' method.
I put them in main(). (And it is unusual for me to put them in an instance method.)
Just for interest, I paste the requirements of the English version here:
>> Requirements:
•
The Cat class must have a constructor with no parameters.
•
The Cat class must contain public fields age, weight, and strength.
•
The Cat class must have a fight method.
•
In the fight method, implement a mechanism for cats to fight based on their weight, age and strength.
•
The method should return the same value every time we fight the same cat.
•
If cat1 beats cat2, then cat2 must lose to cat1. <<
+2
BenOitB
5 juillet 2021, 08:28
Hi Guadalupe, actually with a clear head today, it's understood ! as it meets the following requirement :
"The Cat class must contain public fields age, weight, and strength."
There is all i need in the public class Chat
Thx ^^
+1
BenOitB
5 juillet 2021, 08:44
Hi Gellert,
I would write my objects in the main() too !
It wasn't stupid at all, don't worry ;). Actually i just understand that everything i needed was written at lines 8-10.
it's just a question of "references" if i may say. And i understood better your explanations and the Lupe's too.
The requirements in english version are the same than in french.
To me, the "mechanism" thing was to create objects with very specific attributes (weight, age and strength) in the method and then finish with a condition. So it was a misundertanding coming from me !
Then when i saw the CodeGym solution afterwards, with a ternary condition, i saw that i wasn't there yet xD
+1
Gellert Varga
5 juillet 2021, 09:02
Ok, thanks for the feedback:)
+1
Guadalupe Gagnon
5 juillet 2021, 13:16
You're doing an excellent job so far BenOitB! Keep up the pace and you will be a proficient coder in no time.
0
Gellert Varga
2 juillet 2021, 20:13
1.)
The public boolean combattre(Chat autreChat) method is a "generic" code.
Every Chat object you create will have its own combratte() method like this!
You understand: if you create chat1 chat2 chat3 . . . chat48 objects, each of our 48 objects will have its own combratte() method! That's why we call such a method an instance method. Every instance (=object) will have one instance of this method.
So you can't write 'chat1' variable name in this method, because it's not even sure if an object with such a name will ever be created. Maybe an object called chatDelta or chat1984 will be created... Therefore, you can only write code in the instance method with a general formulation.
You can refer to 'this' in an instance method. This means the object whose method we are talking about. The one that called the method:
If you write this code in main():
chat1.combratte(chat39); // = we call the combratte method of the chat1 object;
then 'this' keyword inside the method will mean chat1 object,
and the chat39 sent as an argument will be named variable autreChat inside the method.
If you write this code in main():
chat19.combratte(chat39);
then 'this' keyword inside the method will mean chat19 object.
So, inside the boolean instance method, it's incorrect to write this:
chat1.force<chat39.force;
but you can write for example this code:
this.force<autreChat.force;
2.)
line 29: if (chat1.force > chat2.force) // in the given case, in your code: 10 > 5 = true
line 31: if (chat2.force < chat1.force) // in the given case, in your code: 5 < 10 = true
so, you make the exactly same checking in these two lines. Just with other words:)
Instead, you could use the if-else structure, for example this way:
if (condition) return true;
else return false;
and you don't need the third return statement (line 34).
If you still have questions: Reply button!:)
+1
BenOitB
4 juillet 2021, 09:09
2) FACEPALM XD. Of course !! thank you ^^
0
Guadalupe Gagnon
2 juillet 2021, 15:23
See my comment on the English version of the task
+1