In: Computer Science
Write a java program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
Complete java program:
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class Calc {
public static void main(String[] arg) {
List<Integer> numbers = new
LinkedList<>();
Random r = new Random();
for(int i=1;i<=25;i++) {
// Generating
random integers from 0 to 100
int value =
r.nextInt((100 - 0) + 1) + 0;
//inserting 25
random integers into a LinkedList object
numbers.add(value);
}
//sorting the elements
Collections.sort(numbers);
//calculating the sum of the
elements
Integer sum =
numbers.stream().reduce(0, Integer::sum);
//calculating the sum of the
elements
float avg = (float)sum /
numbers.size();
System.out.println("Sum of the
elements:"+sum);
System.out.println("Average of the
elements:"+avg);
}
}
Output: