interface Enumeration { public boolean hasNext(); public Object getNext(); } cla
ID: 3642593 • Letter: I
Question
interface Enumeration{
public boolean hasNext();
public Object getNext();
}
class NameCollection
{
String[] names;
NameCollection(String[] names)
{
this.names = names;
}
//getEnumeration should return an instance of a class that implements the Enumeration interface where
// hasNext() and getNext)_ correspond to data stored within the names array.
Enumeration getEnumeration()
{
// ENTER CODE HERE USING AN INNER CLASS
}
}
Complete the method getEnumeration() above so that it returns an anonymous inner class that corresponds to the Enumeration interface for the names array in NamesCollection. Then write a main method that creates a NamesCollection object with a sample array of strings, retrieves the Enumeration for this class via getEnumeration(), and then iterates through the enumeration outputting each name using the getNext() method.
Explanation / Answer
Enumeration getEnumeration()
{
return new Enumeration()
{
int index = 0;
public boolean hasNext()
{
return index < names.length;
}
public Object getNext()
{
return names[index++];
}
};
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.