Hi there.
Got stuck removing the cat, I have been reading about iterators for a while now but can't get my head around them.
Any pointers?
Thank you
package com.codegym.task.task08.task0819;
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
import java.util.List;
/*
Set of cats
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:
1. The program should display text on the screen.
2. Inside the Solution class, there must be a public static Cat class with a default constructor.
3. The Solution class's createCats() method must return a Set containing 3 cats.
4. The Solution class's printCats() method must display all the cats in the set. Each cat on a new line.
5. The main() method should call the createCats() method once.
6. The main() method should call the printCats() method.
7. The main() method must remove one cat from the set of cats.
*/
public class Solution {
public static void main(String[] args) {
Set<Cat> cats = createCats();
for (Iterator<Cat> iterator = cats.iterator(); iterator.hasNext();) {
Cat s = iterator.next();
if (cats.size() % 3 == 0) {
iterator.remove();
}
} //write your code here. step 3
printCats(cats);
}
public static Set<Cat> createCats() {
HashSet<Cat> set = new HashSet<Cat>();
Cat cat1 = new Cat();
Cat cat2 = new Cat();
Cat cat3 = new Cat();
set.add(cat1);
set.add(cat2);
set.add(cat3);//write your code here. step 2
return set;
}
public static void printCats(Set<Cat> cats) {
for(Cat cat : cats){
System.out.println(cat);
}
// step 4
}
public static class Cat {
public static void Cat(){
}
}// step 1
}