SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Interfaces And Inner Classes
Exercise:
Programming Projects
Question:3 | ISBN:9780132830317 | Edition: 5

Question

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

{

// Returns true if another element in the collection exists

public boolean hasNext();

// 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;

/**

* The list of names is initialized from outside

* and passed in as an array of strings

*/

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 ()

{

// Complete code here using an inner class

}

}

Complete the method getEnumeration() 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.



TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

interface Enumeration 
{
    // Returns true if another element in the collection exists
    boolean hasNext();

    // Returns the next element in the collection as an Object
    Object getNext();
}

/**
 * NameCollection implements a collection of names using a simple array.
 */
class NameCollection
{
    String[] names;

    /**
     * The list of names is initialized from outside and passed in as an array of strings
     */
    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() 
    {
        // Return an instance of an anonymous inner class that implements
       // the Enumeration interface
        return new Enumeration() 
        {
            private int index = 0;

            @Override
            public boolean hasNext() 
            {
                return index < names.length;
            }

            @Override
            public Object getNext() 
            {
                if (hasNext()) 
                {
                    return names[index++];
                }
                return null;
            }
        };
    }
}

public class Main 
{
    public static void main(String[] args) 
    {
        // Sample array of names
        String[] namesArray = {"Alice", "Bob", "Charlie", "David"};

        // Create a NameCollection object with the sample array
        NameCollection nameCollection = new NameCollection(namesArray);

        // Retrieve the Enumeration for this class via getEnumeration()
        Enumeration enumeration = nameCollection.getEnumeration();

        // Iterate through the enumeration and output each name using the getNext() method
        while (enumeration.hasNext()) {
            System.out.println(enumeration.getNext());
        }
    }
}

OUTPUT:

Alice
Bob
Charlie
David

 

0 0

Discussions

Post the discussion to improve the above solution.