question archive Listed next is a code skeleton for an interface called  "Enumeration"  and a class called "NameCollection "

Listed next is a code skeleton for an interface called  "Enumeration"  and a class called "NameCollection "

Subject:Computer SciencePrice:3.87 Bought7

Listed next is a code skeleton for an interface called  "Enumeration"  and a class called "NameCollection " .  Enumeration  provides an interface to sequentially iterate through some type of collection. In this case, the collection will be the class  NameCollection  that simply stores a collection of names using an array of strings.

 

interface Enumeration

{

// Asks user for an index and return true if a value exists in the next index

public boolean hasNext();

// Asks user for an index and returns the next element in the collection as an Object

public Object getNext();

}

//NameCollection implements a collection of names using  a simple array.

 class NameCollection

{

 String[] names = new String[100];

 }

Create constructor and abstract methods of interface in the class NameCollection.

Then write a main  method that creates a  NamesCollection object with a sample array of strings,

and then iterates through the enumeration outputting each name using the getNext()  method.  

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:

NameCollection.java

public class NameCollection implements Enumeration

{
	private int cursor=0; // to get current object
	private final int size; // to store size of array stored. Because it can be less than 100.

 String[] names = new String[100];
  

 public NameCollection(String[] names)
 {
	 this.names=names;
	 this.size=names.length;
 }
  
 @Override
 public boolean hasNext() {
	if(cursor+1 <=size)
		return true;
	return false;
 }

 @Override
 public Object getNext() {
	
	return names[cursor++];
 }
  
 public static void main(String args[])
 {
	 String arr[]= {"John","jack","Nick","Abhishek","Vaishali","ABC"};
	 NameCollection obj=new NameCollection(arr);
	 while(obj.hasNext())
	 {
		 System.out.println(obj.getNext());
	 }
 }

 }

Enumeration.java

interface Enumeration

{

// Asks user for an index and return true if a value exists in the next index

public boolean hasNext();

// Asks user for an index and returns the next element in the collection as an Object

public Object getNext();

}

Output:

John
jack
Nick
Abhishek
Vaishali
ABC