CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Paul
Level 24
Vienna
  • 21.08.2020
  • 313views
  • 1comment

my code can't pass the last requirement

Question about the task Set of cats
Java Syntax,  Level 8,  Lesson 11
Under discussion

1. Inside the Solution class, create a public static Cat class.
2. Implement the createCats method. It must create a Set of cats and add 3 cats to it.
3. In the main method, remove one cat from Set cats.
4. Implement the printCats method. It should display all the cats that remain in the set.
Each cat on a new line.

Requirements:
  • The program should display text on the screen.
  • Inside the Solution class, there must be a public static Cat class with a default constructor.
  • The Solution class's createCats() method must return a Set containing 3 cats.
  • The Solution class's printCats() method must display all the cats in the set. Each cat on a new line.
  • The main() method should call the createCats() method once.
  • The main() method should call the printCats() method.
  • The main() method must remove one cat from the set of cats.
package com.codegym.task.task08.task0819; import java.util.HashSet; import java.util.Set; /* Set of cats */ public class Solution { public static void main(String[] args) { Set<Cat> cats = createCats(); //write your code here. step 3 cats.remove(Cat.cat3); printCats(cats); } public static Set<Cat> createCats() { //write your code here. step 2 Set<Cat> catset = new HashSet<>(); catset.add(Cat.cat1); catset.add(Cat.cat2); catset.add(Cat.cat3); return catset; } public static void printCats(Set<Cat> cats) { // step 4 Object[] a = cats.toArray(); for(int i = 0 ; i < a.length; i ++ ){ System.out.println(a[i]); } } public static class Cat { public static Cat cat1 = new Cat(); public static Cat cat2 = new Cat(); public static Cat cat3 = new Cat(); public Cat(){ } } // step 1 }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
dhanush
Level 30 , Chennai, India
21 August 2020, 14:33
It doesn't remove the cat object use for each loop to remove. f
or(Cat cat : cats){
cats.remove(cat);
break;
}
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 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.