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
Question
  • Avis
  • À propos
Démarrer
Démarrer la formation
Démarrer la formation maintenant
  • Toutes les questions
Mathias Engambe
Niveau 12
Paris
  • 27.08.2020
  • 331vues
  • 3commentaires

What's wrong with my code ?

Question sur la mission Compteur de chats
Syntaxe Java,  Niveau 6,  Leçon 5
Discussion en cours


Dans le constructeur de la classe Chat, à savoir public Chat(), incrémente le nombre de chats (la variable static nombreChats de la classe Chat) de 1. Décrémente cette variable de 1 dans la méthode finalize.

Impératifs :
  • Ajoute un constructeur sans paramètres à la classe Chat, c'est-à-dire public Chat().
  • Le constructeur doit incrémenter la valeur de la variable nombreChats de 1.
  • Ajoute la méthode finalize à la classe Chat.
  • La méthode finalize ne doit rien renvoyer (le type de retour est void).
  • La méthode finalize doit réduire la variable nombreChats de 1.
package fr.codegym.task.task06.task0604; /* Compteur de chats */ public class Chat { public static int nombreChats = 0; public void Chat(int nombreChats){ Chat.nombreChats += 1; } protected void finalize(int nombreChats) throws Throwable{ Chat.nombreChats -= 1; } public static void main(String[] args) { } }
0
Commentaires (3)
  • Populaires
  • Nouveau
  • Anciennes
Tu dois être connecté(e) pour laisser un commentaire
Guadalupe Gagnon
niveau Tampa, Tampa, United States
27 août 2020, 15:02
You need to add a constructor to this code (Line 10 is a method and not a constructor). I recommend reading up on constructors if you are unsure what that means. The constructor for this task in the first requirement does not call for any arguments. Make sure that the constructor in this code has none as well. The finalize method is already declared as part of the Object class and needs to be over written in this task to do what the conditions ask for. To override a method properly you need to match the method's signature while the access can not be stricter than the parent's method. The signature is: 1) the return value 2) the method name 3) the method's arguments Make sure that the implementation matches or you will not be over riding the method so much as over loading it instead (line 13 is an over loaded finalize implementation).
0
david
22 décembre 2020, 08:54
I don t have a clue what you are talking about. Is this exercise supposed to be easy?
0
Guadalupe Gagnon
niveau Tampa, Tampa, United States
22 décembre 2020, 14:06
It is really easy, however when you are learning everything brand new it seems like a mountain of information. Look up constructors , how they differ from methods, how to implement them, and how to override a method. Make sure you look these up as Java articles and not another object orientated language like C# or anything.
+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.