CodeGym
Promotion
CodeGym University
Apprentissage
Programme
Missions
Études et quiz
Jeux
Aide
Calendrier motivateur
Communauté
Utilisateurs
Forum
Discussion
Articles
Témoignages
Activité
Avis
Abonnements
Thème clair
Démarrer la formation maintenant
  • Toutes les questions
BenOitB
Niveau 8
Lyon
  • 02.07.2021
  • 372vues
  • 11commentaires

I give my tongue to...the cat (french expression)

Question sur la mission Implémenter la méthode combattre
Syntaxe Java,  Niveau 5,  Leçon 5
Résolues

Implémente la méthode boolean combattre(Chat autreChat) :
implémente un mécanisme pour que les chats combattent en fonction de leur poids, leur âge et leur force.
À toi de décider dans quelle mesure chacun de ces facteurs influencera l'issue des combats.
La méthode doit déterminer si notre chat (this) a gagné le combat, c'est-à-dire que tu dois renvoyer true s'il a gagné et false dans le cas contraire.

La condition suivante doit être remplie :
si chat1.combattre(chat2) renvoie true,
alors chat2.combattre(chat1) doit renvoyer false

Impératifs :
  • La classe Chat doit avoir un constructeur sans paramètres.
  • La classe Chat doit contenir les champs public age, poids et force.
  • La classe Chat doit avoir une méthode appelée combattre.
  • Dans la méthode combattre, implémente un mécanisme pour que les chats combattent en fonction de leur poids, leur âge et leur force.
  • La méthode doit renvoyer la même valeur chaque fois que nous combattons le même chat.
  • Si chat1 bat chat2, alors chat2 doit perdre contre chat1.
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) { } }
0
Commentaires (11)
  • Populaires
  • Nouveau
  • Anciennes
Tu dois être connecté(e) pour laisser un commentaire
Gellert Varga
niveau Szekesfehervar, Szekesfehervar, Hungary
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
niveau Lyon, Lyon, France
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 :
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;

        if (this.force > autreChat.force) {
            return true;
        }
        else if (this.force > autreChat.force) {
            return false;
        }
            return false;
    }

    public static void main(String[] args) {

    }
}
+1
Guadalupe Gagnon
niveau Tampa, Tampa, United States
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
niveau Szekesfehervar, Szekesfehervar, Hungary
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
niveau Lyon, Lyon, France
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
niveau Lyon, Lyon, France
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
niveau Szekesfehervar, Szekesfehervar, Hungary
5 juillet 2021, 09:02
Ok, thanks for the feedback:)
+1
Guadalupe Gagnon
niveau Tampa, Tampa, United States
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
niveau Szekesfehervar, Szekesfehervar, Hungary
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
niveau Lyon, Lyon, France
4 juillet 2021, 09:09
2) FACEPALM XD. Of course !! thank you ^^
0
Guadalupe Gagnon
niveau Tampa, Tampa, United States
2 juillet 2021, 15:23
See my comment on the English version of the task
+1
Apprendre
  • Inscription
  • Cours de Java
  • Aide avec les missions
  • Tarification
  • Projets de jeu
  • Syntaxe Java
Communauté
  • Utilisateurs
  • Articles
  • Forum
  • Discussion
  • Témoignages
  • Activité
  • Affiliate Program
Société
  • À propos
  • Contacts
  • Avis
  • Salle de presse
  • CodeGym pour l'éducation
  • FAQ
  • Support
CodeGym CodeGym est un cours en ligne pour apprendre la programmation Java à partir de rien. Ce cours est le moyen idéal de maîtriser Java pour les débutants. Il contient plus de 1 200 missions avec vérification instantanée et l'essentiel de la théorie sur les fondamentaux de Java. Pour t'aider à réussir dans ton apprentissage, nous avons préparé tout un tas de fonctionnalités motivantes : questionnaires, projets de codage et contenu pour t'aider à apprendre efficacement et te lancer dans une carrière de développeur Java.
Suis-nous
Langue de l'interface
On ne naît pas programmeur, on le devient © 2023 CodeGym
MastercardVisa
On ne naît pas programmeur, on le devient © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.