Starting from:

$24.99

CS2030 Lab 8- Infinite List Solution

Problem Description
An infinite list InfiniteList is a generic list that can store elements of type T in order where duplicates are allowed. Unlike the previous lab, intermediate operations of InfiniteList should be lazily evaluated.
The Task
You are to design your own InfiniteList interface with the following requirements below. As InfiniteList is similar to Java's Stream in Java, and so, you are not allowed to import packages from java.util.stream
Create the InfiniteList interface and the InfiniteListImpl implementation Define a get method for each operation
This task is divided into several levels. Read through all the levels to see how the different levels are related. You need to complete all levels.
Just remember to:


Level 2
Implement the following intermediate operations following the corresponding specifications of Java's Stream API:
InfiniteList<R> map(Function<T, R> mapper)
InfiniteList<T> limit(int maxSize)
InfiniteList<T> filter(Predicate<T> predicate)
InfiniteList<T> takeWhile(Predicate<T> predicate)
As some of the methods could possibly produce no elements, you will need to redefine get to have a return type of Optional<T>.
jshell> /open InfiniteListImpl.java jshell> /open InfiniteList.java
jshell> InfiniteList<String> ifl = InfiniteList.generate(() -> "A").map(x -> x + 1) ifl ==> InfiniteListImpl$2@28d25987
jshell> IntStream.range(1, 5).forEach(x -> System.out.println(ifl.get())) Optional[A1]
Optional[A1]
Optional[A1] Optional[A1]
jshell> InfiniteList<Integer> ifl = InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 0) ifl ==> InfiniteListImpl$3@7cd62f43
jshell> IntStream.range(1, 5).forEach(x -> System.out.println(ifl.get())) Optional[2]
Optional[4]
Optional[6] Optional[8]
jshell> InfiniteList<Integer> ifl = InfiniteList.iterate(1, x -> x + 1).limit(2) ifl ==> InfiniteListImpl$1@39c0f4a
jshell> IntStream.range(1, 5).forEach(x -> System.out.println(ifl.get())) Optional[1]
Optional[2]
Optional.empty Optional.empty
jshell> InfiniteList<Integer> ifl = InfiniteList.iterate(1, x -> x + 1).limit(2).filter(x -> x % 2 == 0) ifl ==> InfiniteListImpl$3@53b32d7
jshell> IntStream.range(1, 5).forEach(x -> System.out.println(ifl.get())) Optional[2]
Optional.empty
Optional.empty Optional.empty
jshell> InfiniteList<Integer> ifl = InfiniteList.iterate(1, x -> x + 1).takeWhile(x -> x < 3) ifl ==> InfiniteListImpl$4@3abbfa04
jshell> IntStream.range(1, 5).forEach(x -> System.out.println(ifl.get())) Optional[1]
Optional[2]
Optional.empty
Optional.empty
Make a copy of your Java programs to the level directory by typing the Unix commands
$ jar cvf infinite2.jar *.java
$ mkdir infinite2
$ cp *.java infinite2
$ cp infinite2.jar infinite2
Verify your jar archive using
$ jar tf ~/infinite2/infinite2.jar


Level 3
Now implement the following terminal operations by following the corresponding specifications of Java's Stream API:
long count()
void forEach(Consumer<T> action)
Optional<T> reduce(BiFunction<T,T,T> accumulator)
T reduce(T identity, BiFunction<T,T,T> accumulator) Object[] toArray()
You will also need to ensure that the get method can no longer be called from a client class.
jshell> /open InfiniteListImpl.java jshell> /open InfiniteList.java
jshell> InfiniteList.iterate(1, x -> x + 1).filter(x -> x % 2 == 1).limit(10).count()
$.. ==> 10
jshell> InfiniteList.iterate(1, x -> x + 1).limit(10).filter(x -> x % 2 == 1).count()
$.. ==> 5
jshell> InfiniteList.iterate(1, x -> x + 1).limit(5).forEach(System.out::println)
1
2
3
4 5
jshell> InfiniteList.iterate(1, x -> x + 1).limit(5).reduce(0, (x, y) -> x + y)
$.. ==> 15
jshell> InfiniteList.iterate(1, x -> x + 1).limit(0).reduce(0, (x, y) -> x + y)
$.. ==> 0
jshell> InfiniteList.iterate(1, x -> x + 1).limit(5).reduce((x, y) -> x + y)
$.. ==> Optional[15]
jshell> InfiniteList.iterate(1, x -> x + 1).limit(0).reduce((x, y) -> x + y)
$.. ==> Optional.empty
jshell> InfiniteList.iterate(1, x -> x + 1).map(x -> x * 2).limit(10).toArray()
$20 ==> Object[10] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }
jshell> InfiniteList.generate(() -> 1).get() | Error:
| cannot find symbol
| symbol: method get()
| InfiniteList.generate(() -> 1).get() | ^--------------------------------^
jshell> InfiniteList.generate(() -> 1).map(x -> x * 2).get() | Error:
| cannot find symbol
| symbol: method get()
| InfiniteList.generate(() -> 1).map(x -> x * 2).get()
| ^------------------------------------------------^
Make a copy of your Java programs to the level directory by typing the Unix commands
$ jar cvf infinite3.jar *.java
$ mkdir infinite3
$ cp *.java infinite3
$ cp infinite3.jar infinite3
Verify your jar archive using
$ jar tf ~/infinite3/infinite3.jar


Level 4
Finally, create the package cs2030.mystream for the InfiniteList interface and its implementation class.
Define a client class Main that imports cs2030.mystream to test your implementation and compile your program using
$ javac -d . *.java
Make a copy of your Java programs to the level directory by typing the Unix commands
$ jar cvf infinite4.jar *.java
$ mkdir infinite4
$ cp *.java infinite4
$ cp infinite4.jar infinite4
Verify your jar archive using
$ jar tf ~/infinite4/infinite4.jar

More products