Write a program that inserts 25 random integers from 0 to 100 in order into a LinkedList object.The program must:• sort the elements,• then calculate the sum of the elements, and• calculate the floating-point average of the elements.Utilizing Java htp 10 late objects approach

Respuesta :

Answer:

Written in Java

import java.util.Collections;

import java.util.Random;

import java.util.LinkedList;

class Main {

 public static void main(String[] args) {

   LinkedList<Integer> myList = new LinkedList<Integer>();

   Random rand = new Random();

   int randnum;

   int sum = 0;

   for(int i = 0;i<25;i++)     {

       randnum = rand.nextInt(101);

       myList.add(new Integer(randnum));    

       sum+=randnum;

   }

   Collections.sort(myList);

   System.out.println(myList);

   System.out.println("Average: "+(float)sum/25);

 }

}

Explanation:

import java.util.Collections;

import java.util.Random;

import java.util.LinkedList;

class Main {

 public static void main(String[] args) {

This declares a linkedlist as integer

   LinkedList<Integer> myList = new LinkedList<Integer>();

This declares random variable rand

   Random rand = new Random();

This declares randnum as integer

   int randnum;

This declares and initializes sum to 0

   int sum = 0;

The following iteration generates random numbers, inserts them into the linkedlist and also calculates the sum of the generated numbers

   for(int i = 0;i<25;i++)    {

       randnum = rand.nextInt(101);

       myList.add(new Integer(randnum));    

       sum+=randnum;

   }

This sorts the list

   Collections.sort(myList);

This prints the list

   System.out.println(myList);

This calculates and prints a floating-point average

   System.out.println("Average: "+(float)sum/25);

 }

}

The program that inserts 25 random integers from 0 to 100 in order into a LinkedList and sort the elements, and then calculate the sum of the elements, and calculate the floating-point average of the elements is represented below:

import random

linkedList = []

for i in range(0, 25):

   random_number = random.randint(0, 100)

   linkedList.append(random_number)

sort = sorted(linkedList)

add = sum(sort)

average = add / len(sort)

print(sort)

print(add)

print(average)

The code is written in python.

An empty variable linkedList is declared and an empty list is stored in it.

we loop through a value from range 0 to 25.

Then get a random of 0 to 100.

The random 25 numbers are appended to the empty list linkedList.

The linkedList are then sorted and stored in a variable sort.

The sorted numbers are summed and stored in the variable add.

The floated  average of the number is calculated and stored in the variable average.

The sorted numbers, sum and floated average are printed out.

The bolded value of the code are python keywords.

read more: https://brainly.com/question/18684565?referrer=searchResults

Ver imagen vintechnology
Q&A Education