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
John Peterson
Level 9
Blacksburg
  • 08.10.2019
  • 569views
  • 2comments

Last requirement not met

Question about the task Greater than 10? You're not a good fit for us
Java Syntax,  Level 8,  Lesson 8
Under discussion

Create a set of numbers (Set<Integer>) and add 20 different numbers to it.
Remove from the set all numbers greater than 10 .

Requirements:
  • The program should not display text on the screen.
  • The program should not read values from the keyboard.
  • The Solution class must have only three methods.
  • The createSet() method must create and return a HashSet containing 20 different numbers.
  • The removeAllNumbersGreaterThan10() method must remove from the set all numbers greater than 10.
package com.codegym.task.task08.task0814; import java.util.HashSet; import java.util.Set; import java.util.Iterator; /* Greater than 10? You're not a good fit for us */ public class Solution { public static HashSet<Integer> createSet() { // write your code here HashSet<Integer> set = new HashSet<Integer>(20); set.add(new Integer(671)); set.add(new Integer(451)); set.add(new Integer(41)); set.add(new Integer(15613)); set.add(new Integer(3)); set.add(new Integer(1581)); set.add(new Integer(111)); set.add(new Integer(91)); set.add(new Integer(19)); set.add(new Integer(14)); set.add(new Integer(1)); set.add(new Integer(189)); set.add(new Integer(167)); set.add(new Integer(15)); set.add(new Integer(16)); set.add(new Integer(11)); set.add(new Integer(9)); set.add(new Integer(12)); set.add(new Integer(51)); set.add(new Integer(31)); return set; } public static HashSet<Integer> removeAllNumbersGreaterThan10(HashSet<Integer> set) { // write your code here for (Iterator i = set.iterator(); i.hasNext();) { Integer integer = (Integer) i.next(); if((Integer) i.next() > 10) { set.remove((Integer) i.next()); } } return set; } public static void main(String[] args) { } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
John Peterson
Level 9 , Blacksburg, United States
8 October 2019, 19:32
Thanks Guadalupe. I think i get it.
0
Guadalupe Gagnon
Level 37 , Tampa, United States
8 October 2019, 19:14
Every time you call .next() on an iterator it goes to the next value. If you have a set with 3 values, lets just say [1,2,3] (this is just an example) at line 44 the number 1 will be pulled by .next() at line 45 the number 2 will be pulled by .next() at line 47 the number 3 will be pulled by .next() So after the code is run only the number three will have been removed and the set would be [1,2]
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.