Answered step by step
Verified Expert Solution
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 arraycurrent
private int prefixProduct;
Construct a new prefix product iterator over the given array.
@param a the array containing the values
public prefixProductArrayIteratorint a
array a;
current ;
prefixProduct ;
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 arraycurrent;
current;
return prefixProduct;
main method to test the prefixProductArrayIterator.
@param args size of array to generate
@param args range of random values
public static void mainString args
if argslength
System.err.printlnUsage: java prefixProductArrayIterator size range";
System.exit;
convert the commandline parameters to the numbers needed
int n ;
int range ;
try
n Integer.parseIntargs;
range Integer.parseIntargs;
catch NumberFormatException e
System.err.printlne;
System.exit;
create and populate the array
java.util.Random r new java.util.Random;
int a new intn;
for int i ; i n; i
ai rnextIntrange;
print the array
System.out.printlnGenerated array: java.util.Arrays.toStringa;
print the prefix products
System.out.printlnPrefix productss:";
java.util.Iterator iter new prefixProductArrayIteratora;
while iterhasNext
System.out.printlniternext;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started