$24
Java Streams
Topic Coverage
Application of Java Streams
Requirements
Java Streams are to be used for the method implementations as specifed in this assignment
The Tasks
There are several tasks in this assignment.
For each task, you are to defne the appropriate method(s) within the Main class in Main.java. You may also submit other utility classes if necessary.
Task 1 — Count Twin Primes
A prime number is a natural number greater than 1 that is only divisible by 1 and itself. A twin prime is one of a pair of prime numbers with a difference of 2. For example, 41 and 43 are twin primes.
Defne the method countTwinPrimes in class Main which takes in an integer n and counts the number of distinct twin primes from 0 until n inclusive.
static long countTwinPrimes(int n)
Save your Main class in the fle Main.java.
jshell> Main.countTwinPrimes(100)
$.. ==> 15
jshell> Main.countTwinPrimes(2)
$.. ==> 0
jshell> Main.countTwinPrimes(3)
$.. ==> 1
Task 2 — Reverse String
Defne the method reverse in class Main that takes in a String str and returns the reverse of str.
static String reverse(String str)
https://codecrunch.comp.nus.edu.sg/task_view.php?tid=5109
1/3
26/11/2021, 03:13 CodeCrunch
Save your Main class in the fle Main.java.
jshell> Main.reverse("orange")
$.. ==> "egnaro"
jshell> Main.reverse("one two three")
$.. ==> "eerht owt eno"
jshell> Main.reverse("")
$.. ==> ""
jshell> Main.reverse("the quick brown fox jumps over the lazy dog.")
$.. ==> ".god yzal eht revo spmuj xof nworb kciuq eht"
Task 3 — Counting Repeats
Defne the method countRepeats in class Main that takes in an integer array of digits 0 to 9 and returns the number of occurrences of adjacent repeated digits. You may assume that there are at least three elements in the array.
static long countRepeats(int... array)
For example,
the array {0, 1, 2, 2, 1, 2, 2, 1, 3, 3, 1} has three occurrences of repeated digits the array {0, 1, 1, 1, 1, 2} has one occurrence
Save your Main class in the fle Main.java.
jshell> Main.countRepeats(0,1,2,2,1,2,2,1,3,3,1)
$.. ==> 3
Task 4 — Normalized Mean
Given a list T of n integers ti, the normalized value of each ti is defned as
where minT and maxT represent the minimum and maximum values among all n values in T.
For example, the list of values {1,2,3,4,5} upon normalizing will become {0,0.25,0.5,0.75,1} since minT = 1 and maxT=5.With the set of normalized values generated, the normalized mean can be easily computed to be 0.5.
Notice from the above that fnding the normalized mean requires values in the list to be accessed twice: once for fnding the maximum/minimum, and a second time to compute each normalized value and fnding the mean.
Alternatively, we can re-expressed the normalized mean as
This way we need to only access each element in the list exactly once.
Defne the method normalizedMean in class Main that takes in a Stream of Integer elements and returns the normalized mean
static double normalizedMean(Stream<Integer> stream)
Save your Main class in the fle Main.java.
jshell> Main.normalizedMean(Stream.<Integer>of(1, 2, 3, 4, 5))
$.. ==> 0.5
jshell> Main.normalizedMean(Stream.<Integer>of(1, 1))
$.. ==> 0.0
jshell> Main.normalizedMean(Stream.<Integer>of(1))
$.. ==> 0.0
https://codecrunch.comp.nus.edu.sg/task_view.php?tid=5109
2/3
26/11/2021, 03:13
CodeCrunch
jshell>
Main.normalizedMean(Stream.<Integer>of())
$.. ==>
0.0
© Copyright 2009-2021 National University of Singapore.
MySoC | Computing Facilities | Search | Campus Map
All Rights Reserved.
School of Computing, National University of Singapore
Terms of Use | Privacy | Non-discrimination
https://codecrunch.comp.nus.edu.sg/task_view.php?tid=5109 3/3