In: Computer Science
Question 7 6 Marks
public static void main(String[] args) {
int[] marks = {10, 53, 65, 49, 46, 95, 81, 45, 72, 85};
//use lambdas and streams to work out how many
// marks are >= 50 and display the result.
<part (a) to complete>
}
Output should be similar to the following:
The number of marks greater than or equal to 50 = 6
Given the starting point for the code below, use lambdas and streams to complete the main method as follows:
In the answer template complete the code for parts (i) to (iii) where you see <to be completed> in the code below:
public class StreamQuestion {
public static void main(String[] args) {
Result[] results = {new Result("S123345", 10),
new Result("S678901", 53),
new Result("S778901", 65),
new Result("S878901", 49),
new Result("S078901", 81),
new Result("S688901", 45),
new Result("S698901", 72),
new Result("S679901", 46),
new Result("S678911", 72),
new Result("S678912", 85)};
List<Result> resultList = Arrays.asList(results);
//(i) Write the code for the supplementary predicate
<Part (i) To be completed>
// (ii) using the predicate (and lambdas and streams),
// display the student id and mark for all results in
// the supplementary range
<Part (ii) To be completed>
// (iii) using lambdas and streams calculate and display
// the average mark
<Part (iii) To be completed>
}
The output generated should be similar to the following:
Supplementary List:
<S878901 49>
<S688901 45>
<S679901 46>
Average Mark: 57.80
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
Main.java
package staticClass;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] marks = {10, 53, 65, 49,
46, 95, 81, 45, 72, 85};
int num=0;
//use lambdas and streams to work
out how many
// marks are >= 50 and
display the result.
num=(int)
Arrays.stream(marks).filter(e->e>50).count();
System.out.println("The number of
marks greater than or equal to 50 = "+num);
}
}
output
2)
StreamQuestion.java
package staticClass;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
class Result{
private String id;
private int mark;
public Result(String id, int mark) {
super();
this.id = id;
this.mark = mark;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
@Override
public String toString() {
return "<" + id + " " + mark +
">";
}
}
public class StreamQuestion {
public static void main(String[] args) {
Result[] results = {new Result("S123345", 10),
new Result("S678901", 53),
new Result("S778901", 65),
new Result("S878901", 49),
new Result("S078901", 81),
new Result("S688901", 45),
new Result("S698901", 72),
new Result("S679901", 46),
new Result("S678911", 72),
new Result("S678912", 85)};
List<Result> resultList = Arrays.asList(results);
//(i) Write the code for the
supplementary predicate
Predicate<Result> lesserthan
= i -> (i.getMark()>=45 && i.getMark()<50);
// (ii) using the predicate (and
lambdas and streams),
// display the student id and mark for all results in
// the supplementary range
System.out.println("Supplementary
List:");
resultList.stream().filter(lesserthan).forEach(p->System.out.println(p));
// (iii) using lambdas and streams
calculate and display
double
sum=resultList.stream().mapToInt(i -> i.getMark()).sum();
double avg =
sum/resultList.size();
System.out.printf("Average Mark:
%.2f",avg);
// the average mark
}
}
output