CodeGym
Promotion
Apprentissage
Cours
Exercices
Sondages & Quiz
Jeux
Aide
Planning
Communauté
Utilisateurs
Forum
Chat
Articles
Témoignages de réussite
Activité
Avis
Abonnements
Thème clair
Question
  • Avis
  • À propos de nous
Commencer
Commencer à apprendre
Commencez à apprendre maintenant
  • Toutes les questions
CodeGym/Aide pour les tâches Java/What's wrong with my code ?
Mathias Engambe
Niveau 12
Paris
  • 27/08/2020
  • 672views
  • 3commentaires

What's wrong with my code ?

Question concernant la tâche Compteur de chats
Java Syntax,  Niveau 6,  Leçon 5
En discussion


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.

Exigences :
  • 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
  • Nouveaux
  • Anciens
Vous devez être connecté pour laisser un commentaire
Guadalupe Gagnon
Niveau 37 , 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
Niveau 10
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 37 , 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 Java
  • Aide pour les exercices
  • Tarifs
  • Syntaxe Java
Communauté
  • Utilisateurs
  • Articles
  • Forum
  • Chat
  • Témoignages de réussite
  • Activité
  • Programme d'affiliation
Entreprise
  • À propos de nous
  • Contacts
  • Avis
  • Espace presse
  • CodeGym pour l'éducation
  • FAQ
  • Support
CodeGymCodeGym est un cours en ligne pour apprendre la programmation Java depuis le début. Ce cours est un moyen idéal pour maîtriser Java pour les débutants. Il contient plus de 1200 exercices avec vérification instantanée et un ensemble essentiel de notions fondamentales de Java. Pour vous aider à réussir, nous avons mis en place des fonctionnalités motivantes : quiz, projets de codage, contenu sur l'apprentissage efficace, et une rubrique sur la carrière de développeur Java.
Suivez-nous
Langue de l'interface
English Deutsch Español हिन्दी
Français
Português Polski বাংলা 简体中文 मराठी தமிழ் Italiano Bahasa Indonesia 繁體中文 Nederlands 日本語 한국어 Bulgarian Danish Hungarian Basa Jawa Malay Norwegian Romanian Swedish Telugu Thai Українська Filipino Turkish Azərbaycan Русский Vietnamese
"Les programmeurs se forment, ils ne naissent pas" © 2026 CodeGym
MastercardVisa
"Les programmeurs se forment, ils ne naissent pas" © 2026 CodeGym