Question: For this lab, you will create a RandomIntList class that, when constructed, generates and stores a list of random integers (by use of a random

For this lab, you will create a RandomIntList class that, when constructed, generates and stores a list of random integers (by use of a random number generator function in Java). The class is to provide a number of iterator objects that iterate over the generated list in various ways (e.g., all values, only even values, only values under a given cutoff value, etc.). Skeleton code for this is given below.

// Iterator Interface

public interface IntListItrInterface {

boolean hasNext();

int next();

}

import java.util.Random;

// Aggregation Class

public class RandomIntList {

int[] list; // package access

public RandomIntList(int size) // constructor

{ initList(); }

public IntListItrInterface getAllValuesIterator() // factory method

{ // complete }

private void initList()

{ // complete }

}

// Iterator Class has package access so that only other classes in the same package can create

objects of this type

class AllValuesIterator implements IntListItrInterface {

private int current_index; // index value of current item in array

private RandomIntList list; // reference to aggregator iterating over

public AllValuesIterator(RandomIntList list, int start_loc) {

current_index = start_loc;

this.list = list;

}

public boolean hasNext()

{ // to complete }

public int next()

{ // to complete }

}

The iterator classes will be hidden from the client code by declaring the classes as having package access. (Leaving out the access modifier of public, protected and private as in the AllValuesIterator class above - implicitly declares the class as having package access.) Thus, the interface, aggregation class, and each of the iterator classes will be placed in one package. The interface and aggregation class will be visiable and accessible from the package, but the iterator classes will only be accessible by the classes in the same package (i.e., RandomIntList). Also, the int[] array of the aggregation class is declared package access so that the methods of the iterator classes have access to it.

Following is the client code for testing your classes.

// Client Code

// create random list of 100 integers

RandomIntList rList = new RandomIntList(100);

// get iterator from RandomIntList instance

IntListItrInterface itr = rList.getAllItemsIterator();

// display all values in the list

while(itr.hasNext())

System.out.println(itr.next()); // retrieves next int in list

To generate random integer values, the Random class of the java.util package can be used:

import java.util.Random;

.

.

Random rand = new Random();

for(int i = 0; i < 20; i++){

System.out.println(rand.nextInt(100)); // generates a random int x, 0 <= x < 100

}

Once you have this code working, create additional iterators for iterating through the list in various ways (e.g., backwards, only even values, etc.) You can create whatever iterators that you like. For each iterator created, modify the client code to test it.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!