Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Java please! Create a SequenceIterator class that implements the interface Iterator of java.util package and can iterate over any sequence of numbers. Your SequenceIterator class

Java please!

Create a SequenceIterator class that implements the interface Iterator of java.util package and can iterate over any sequence of numbers.

Your SequenceIterator class defines two private fields:

generator of type NumberGenerator that allows to generate the next number in a sequence,

index of type int that represents the index of the next number to be generated.

The SequenceIterator class one constructor that constructs a sequence iterator object with given number generator, and size. It also initializes the index to 0.

The SequenceIterator class overrides also the two methods hasNext() and next() defined by the Iterator interface. Create SequenceIterator class consisting of the following:

import java.util.Iterator;

public class SequenceIterator implements Iterator{

private NumberGenerator generator; // a NumberGenerator object

// that generates and returns a number of index n in a

// sequence of numbers

private int size; // size of the sequence

private int index; // index of the next number to be generated in the sequence

/**

* Constructs a SequenceIterator with given number generator and size

* This constructs initializes also the index to 0

* @param generator

* @param size

*/

public SequenceIterator(NumberGenerator generator, int size) {

// Your code comes here

}

@Override

public boolean hasNext() {

// Your code comes here

}

@Override

public Integer next() {

// Your code comes here

}

}

Here's the NumberGenerator class:

public interface NumberGenerator {

/**

* generates and returns the number of order n in a sequence of numbers

* @param n order of the generated number in the sequence

* @return the nth number in the sequence

*/

public int generateNumber(int n);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions