Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.TreeSet; import java.util.Stack ; import java.util.Set; import java.util.Iterator ; import java.util.Random ; /** Illustrates basic operations of an iterator on a set. Notice that

import java.util.TreeSet;
import java.util.Stack ;
import java.util.Set;
import java.util.Iterator ;
import java.util.Random ;

/**
Illustrates basic operations of an iterator on a set.  Notice that the iterator for a set differs from the iterator for a list (i.e.,a list iterator), in that the set iterator has no previous method (since the set has no order it does not makes sense to ask to go backwards), and the set iterator has no add method (since the set has no order it makes no sense to try to add an element at a particular position).

The part that you write adds 1000 random letters a-z to a set, then puts
them in a stack then pops and prints them.

Expected output:
A-B-D-F-G
zyxwvutsrqponmlkjihgfedcba
*/
public class SetTester4
{
public static void main(String[] args)
{
//This first part is just an example of set operations.
Set set = new TreeSet() ;
set.add("A") ;
set.add("B") ;
set.add("C") ;
set.add("D") ;
set.add("F") ;
set.add("G") ;
//The following line shows how to make an iterator for a set.
Iterator iterator = set.iterator() ;
//The following loop shows how to go through the set with the iterator
while (iterator.hasNext()) {
    String element = iterator.next() ;
    if (element.equals("C"))
iterator.remove() ;
    else if (iterator.hasNext())
System.out.print(element + "-") ;
    else
System.out.print(element) ;
}
System.out.println() ;
System.out.println("-----------------------------Your work:") ;
set = new TreeSet() ;
Random random = new Random() ;
char ch = 'a' ;
for (int i = 0 ; i < 1000 ; i++) {
    //-----------Start below here. To do: approximate lines of code = 6
    // 1. add a randomly selected letter "a" to "z" to set ;
   
}//
//2. iterator = ... an iterator on set ;
   
//3. make a stack for strings
   
//4-5. use the iterator to put all of the letters into the stack ;
   
   
   
//6. while there are letters on the stack
   
    System.out.print(stack.pop()) ;//
System.out.println() ;//
    //-----------------End here. Do not remove this comment. Reminder: no changes outside the todo regions.
}
}

Make sure your code is formatted well and follow instructions  exactly and post output screenshot so we know it is as expected!


Step by Step Solution

3.37 Rating (153 Votes )

There are 3 Steps involved in it

Step: 1

import javautilTreeSet import javautilStack import javautilSet import javautilIterator import javaut... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Elementary Linear Algebra with Applications

Authors: Howard Anton, Chris Rorres

9th edition

471669598, 978-0471669593

More Books

Students also viewed these Programming questions