Starting from:

$29.99

CS2030 Lab 7-Your Own Finite Integer Stream Solution

Problem Description
To understand declarative programming using streams, the best way is to come up with your own stream operations. In this exercise, you will develop a finite int stream, much like how Java's IntStream works, accept that it is finite, and eager-evaluated.
The Task
You are to design your own MyIntStream with the following requirements. Note that you are not allowed to import anything from
java.util.stream
Create the MyIntStream interface and the MyIntStreamImpl implementation
Use a List to represent the stream elements
Write an appropriate toString() method to keep track of the stream elements
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 1
Start the stream pipeline via the following data sources:
MyIntStream.of(int... values)
MyIntStream.range(int start, int end)
MyIntStream.rangeClosed(int start, int end)
In order to prevent constructs like: MyIntStream.of(1,2,3).range(1,3), you need to implement the above methods as static methods in the interface. More specifically, construct the appropriate list of elements within a MyIntStreamImpl object and return it.
Note also that
Every stream operation generates a new stream of elements
An empty stream is denoted by an empty list
Since the stream elements are stored in a list and eagerly evaluated through each stream operation, the stream can actually be consumed multiple times
jshell> MyIntStream.of(1, 2, 3)
.. ==> [1, 2, 3]
jshell> MyIntStream.of(new int[]{1, 2, 3})
.. ==> [1, 2, 3]
jshell> MyIntStream.range(1, 10) .. ==> [1, 2, 3, 4, 5, 6, 7, 8, 9]
jshell> MyIntStream.rangeClosed(1, 10) .. ==> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
jshell> MyIntStream.range(1, 1)
.. ==> []
jshell> MyIntStream.of(1, 2, 3).range(1, 3) | Error:
| illegal static interface method call
| the receiver expression should be replaced with the type qualifier 'MyIntStream'
| MyIntStream.of(1, 2, 3).range(1, 3)
| ^---------------------------------^
Make a copy of your Java programs to the level directory by typing the Unix commands
$ jar cvf intstream1.jar *.java
$ mkdir intstream1
$ cp *.java intstream1
$ cp intstream1.jar intstream1
Verify your jar archive using
$ jar tf ~/intstream1/intstream1.jar


Level 2
Implement the following terminal operations:
count() and sum(), that returns a long and int value respectively. In particular, 0 is returned when the stream is empty average() that returns an OptionalDouble object. In the case of IntStream, the following expressions
MyIntStream.range(1,1).average()
MyIntStream.range(1,2).average()
would return OptionalDouble.empty and OptionalDouble[1.0] respectively max and min, each returning an OptionalInt object
forEach(IntConsumer action) that takes as argument an action and performs the action on each stream element
jshell> MyIntStream.range(1, 1).count()
.. ==> 0
jshell> MyIntStream.range(1, 10).count()
.. ==> 9
jshell> MyIntStream.range(1, 1).sum()
.. ==> 0
jshell> MyIntStream.range(1, 10).sum()
.. ==> 45
jshell> MyIntStream.range(1, 1).average()
.. ==> OptionalDouble.empty
jshell> MyIntStream.range(1, 10).average()
.. ==> OptionalDouble[5.0]
jshell> MyIntStream.range(1, 1).min()
.. ==> OptionalMyInt.empty
jshell> MyIntStream.range(1, 10).min()
.. ==> OptionalMyInt[1]
jshell> MyIntStream.range(1, 1).max()
.. ==> OptionalMyInt.empty
jshell> MyIntStream.range(1, 10).max() .. ==> OptionalMyInt[9]
Make a copy of your Java programs to the level directory by typing the Unix commands
$ jar cvf intstream2.jar *.java
$ mkdir intstream2
$ cp *.java intstream2
$ cp intstream2.jar intstream2
Verify your jar archive using
$ jar tf ~/intstream2/intstream2.jar


Level 3
Now, implement the following intermediate operations:
limit(int maxSize) that returns a MyIntStream stream of elements truncated to be no longer than maxSize in length distinct() that returns a MyIntStream stream of distinct elements. Hint: use a Set
filter(IntPredicate predicate) that takes as argument a predicate function and returns a MyIntStream stream of elements that match the given predicate
map(IntUnaryOperator mapper) that takes in a mapper function and returns a MyIntStream stream of elements consisting of the results of applying the given function to the stream elements
jshell> MyIntStream.range(1, 10).limit(5)
.. ==> [1, 2, 3, 4, 5]
jshell> MyIntStream.range(1, 10).limit(0)
.. ==> []
jshell> MyIntStream.of(2, 0, 3, 0).distinct()
.. ==> [2, 0, 3]
jshell> MyIntStream.range(1, 10).filter(x -> x < 5)
.. ==> [1, 2, 3, 4]
jshell> MyIntStream.range(1, 10).filter(x -> x < 0)
.. ==> []
jshell> MyIntStream.range(1, 10).map(x -> x / 2)
.. ==> [0, 1, 1, 2, 2, 3, 3, 4, 4]
jshell> MyIntStream.range(1, 10).map(x -> x / 2).distinct() .. ==> [0, 1, 2, 3, 4]
Make a copy of your Java programs to the level directory by typing the Unix commands
$ jar cvf intstream3.jar *.java
$ mkdir intstream3
$ cp *.java intstream3
$ cp intstream3.jar intstream3
Verify your jar archive using
$ jar tf ~/intstream3/intstream3.jar


Level 4
Finally, implement the following two variants of the reduce terminal operations:
reduce(int identity, IntBinaryOperator op) that takes in an identity value and binary function, and performs a reduction on the stream elements using the identity value and an associative accumulation function. The reduced int value is returned reduce(IntBinaryOperator op) that takes in a binary function and performs a reduction on the elements of this stream, using an associative accumulation function. An OptionalInt describing the reduced value, if any, is returned
In addition, you will need to create the package cs2030.mystream for your implementation.
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 intstream4.jar *.java
$ mkdir intstream4
$ cp *.java intstream4
$ cp intstream4.jar intstream4
Verify your jar archive using
$ jar tf ~/intstream4/intstream4.jar

More products