Put all work into a directory hw4 of your Git project and push it once a day. Don't put any code into packages. Don't put any code into a src subdirectory.
You are given an interface NumberSequence that represents a finite or infinite sequence of integers:
public interface NumberSequence
{
long next();
default boolean hasNext() { return true; }
}
SquareSequence that implements this interface, so that successive calls to the next method yield the squares of integers 0, 1, 4, 9, 16, 25, 36, ...of(long... args) to the NumberSequence interface so that one can construct an instance of a finite sequence by specifying the elements, like this:
NumberSequence seq = NumberSequence.of(1, 1, 2, 3, 5, 8, 13, 21);
NumberSequence interface, provide a default method double average(int n) that computes the average of the first n elements (or all elements if there are fewer) and a method double average() that computes the average of all elements (which should only be called on a finite sequence). Sample usage:
double avg = new SquareSequence().average(5); // yields (0 + 1 + 4 + 9 + 16) * 1.0 / 5 avg = NumberSequence.of(1, 1, 2, 3, 5, 8, 13, 21).average(); // yields the average of all elements
NumberSequence interface, provide a default method toArray that yields an array of the first n elements of the sequence:
long[] array = someSequence.toArray(n);If there are fewer than
n elements, return a shorter array holding all of them.NumberSequence interface, provide a method default NumberSequence filter(LongPredicate p) that yields a sequence of numbers fulfilling the predicate. For example:
NumberSequence oddSquares = new SquareSequence().filter(n -> n % 2 != 0); // the sequence with elements 1, 9, 25, 49, ...
NumberSequence interface, produce a static method iterate(long seed, LongUnaryOperator f) that produces an infinite stream of values seed, f(seed), f(f(seed)), and so on. For example,
NumberSequence.iterate(0, x -> x + 1)
NumberSequence interface, produce a static method random(long seed) that creates random numbers, using a linear congruential generator with m = 231, a = 1103515245 and c = 12345. Use iterate and a lambda expression.Here is a JUnit test class.