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/I don't understand why is it not working ?
Mathias Engambe
Niveau 12
Paris
  • 20/07/2020
  • 560views
  • 1commentaire

I don't understand why is it not working ?

Question concernant la tâche Dix chats
Java Syntax,  Niveau 9,  Leçon 11
En discussion


Il y a une classe Chat avec une variable String nom.
Crée une Map<String, Chat> et ajoute 10 chats représentés par des paires (nom, Chat).
Obtiens un Set de tous les chats de la Map et affiche le tout à l'écran.

Exigences :
  • Le programme ne doit pas lire de données du clavier.
  • La méthode creerMap doit créer un nouvel objet HashMap<String, Chat>.
  • La méthode creerMap doit ajouter 10 chats à la Map, représentés par des paires (nom, Chat).
  • La méthode creerMap doit renvoyer la Map créée.
  • La méthode convertirMapEnSet doit créer et renvoyer l'objet Set créé avec les chats extraits de la Map passée.
  • Le programme doit afficher tous les chats de l'ensemble.
package fr.codegym.task.task09.task0927; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /* Dix chats */ public class Solution { public static void main(String[] args) { Map<String, Chat> map = creerMap(); Set<Chat> ensemble = convertirMapEnSet(map); imprimerSetChats(ensemble); } public static Map<String, Chat> creerMap() { HashMap<String, Chat> map = new HashMap<>(10); map.put("Simba", new Chat("Simba")); map.put("Tom", new Chat("Tom")); map.put("Arbus", new Chat("Arbus")); map.put("Laurandin", new Chat("Laurandin")); map.put("Aldo", new Chat("Aldo")); map.put("Krypto", new Chat("Krypto")); map.put("Grimaldo", new Chat("Grimaldo")); map.put("Harry", new Chat("Harry")); map.put("Gervey", new Chat("Gervey")); map.put("Berto", new Chat("Berto")); return map; } public static Set<Chat> convertirMapEnSet(Map<String, Chat> map) { //écris ton code ici HashSet<Chat> ensemble = new HashSet<>(10); ensemble.add(new Chat("Simba")); ensemble.add(new Chat("Tom")); ensemble.add(new Chat("Arbus")); ensemble.add(new Chat("Laurandin")); ensemble.add(new Chat("Aldo")); ensemble.add(new Chat("Krypto")); ensemble.add(new Chat("Grimaldo")); ensemble.add(new Chat("Harry")); ensemble.add(new Chat("Gervey")); ensemble.add(new Chat("Berto")); return ensemble; } public static void imprimerSetChats(Set<Chat> ensemble) { for (Chat chat : ensemble) { System.out.println(chat); } } public static class Chat { private String nom; public Chat(String nom) { this.nom = nom; } public String toString() { return "Chat " + this.nom; } } }
0
Commentaires (1)
  • Populaires
  • Nouveaux
  • Anciens
Vous devez être connecté pour laisser un commentaire
Gellert Varga
Niveau 23 , Szekesfehervar, Hungary
20 juillet 2020, 13:01
Requirements: "The convertirMapEnSet method must create and return the set of cats retrieved from the passed map." -- !!from the passed map!! But You have not relocated (copied) any items from the passed map to the hashSet. You have created completely new cat objects. (It's true, with the same names, but they're completely new objects.)
0
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