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
  • 20.07.2020
  • 288vues
  • 1commentaire

I don't understand why is it not working ?

Question sur la mission Dix chats
Syntaxe Java,  Niveau 9,  Leçon 11
Discussion en cours


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.

Impératifs :
  • 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
  • Nouveau
  • Anciennes
Tu dois être connecté(e) pour laisser un commentaire
Gellert Varga
niveau Szekesfehervar, 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 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.