Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

prefixProductArrayIterator: public class prefixProductArrayIterator implements java.util.Iterator { / * * the array over which we are iterating * / private int [ ] array; /

prefixProductArrayIterator:
public class prefixProductArrayIterator implements java.util.Iterator {
/** the array over which we are iterating */
private int[] array;
/** the index of the next value to include in the prefix product */
private int current;
/** the product of elements before array[current]*/
private int prefixProduct;
/**
Construct a new prefix product iterator over the given array.
@param a the array containing the values
*/
public prefixProductArrayIterator(int[] a){
array = a;
current =0;
prefixProduct =1;
}
/**
Return whether there are more elements that have not yet been
part of the prefix product sequence.
@return whether there are more elements that have not yet been
part of the prefix product sequence
*/
@Override
public boolean hasNext(){
return current array.length;
}
/**
Return the next value in the prefix product sequence.
@return the next value in the prefix product sequence
*/
@Override
public Integer next(){
prefixProduct = prefixProduct * array[current];
current++;
return prefixProduct;
}
/**
main method to test the prefixProductArrayIterator.
@param args[0] size of array to generate
@param args[1] range of random values
*/
public static void main(String args[]){
if (args.length !=2){
System.err.println("Usage: java prefixProductArrayIterator size range");
System.exit(1);
}
// convert the command-line parameters to the numbers needed
int n =0;
int range =0;
try {
n = Integer.parseInt(args[0]);
range = Integer.parseInt(args[1]);
}
catch (NumberFormatException e){
System.err.println(e);
System.exit(1);
}
// create and populate the array
java.util.Random r = new java.util.Random();
int a[]= new int[n];
for (int i =0; i n; i++){
a[i]= r.nextInt(range)+1;
}
// print the array
System.out.println("Generated array: "+ java.util.Arrays.toString(a));
// print the prefix products
System.out.println("Prefix productss:");
java.util.Iterator iter = new prefixProductArrayIterator(a);
while (iter.hasNext()){
System.out.println(iter.next());
}
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions