Write a Java program that inserts 25 random integers rangin…

Write a Java program that inserts 25 random integers ranging from 0  to 100 into an ArrayList in ascending order. The program should then  output the numbers and display the sum and average of the numbers. After you are done, submit the source code and screenshot.

Answer

In order to solve this problem, we will write a Java program that inserts 25 random integers ranging from 0 to 100 into an ArrayList in ascending order. The program will then output the numbers and display the sum and average of the numbers.

To do this, we will follow the following steps:

1. Create an ArrayList to store the random integers.
2. Generate 25 random integers using the `Random` class and add them to the ArrayList.
3. Sort the ArrayList in ascending order using the `Collections.sort()` method.
4. Calculate the sum of the numbers in the ArrayList.
5. Calculate the average of the numbers by dividing the sum by the number of elements in the ArrayList.
6. Output the numbers, sum, and average to the console.

Here is the Java code that accomplishes this task:

“`java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class RandomNumbers {
public static void main(String[] args) {
ArrayList numbers = new ArrayList();

// Generate 25 random integers
Random rand = new Random();
for (int i = 0; i < 25; i++) {
int randomNum = rand.nextInt(101);
numbers.add(randomNum);
}

// Sort the numbers in ascending order
Collections.sort(numbers);

// Calculate the sum of the numbers
int sum = 0;
for (int num : numbers) {
sum += num;
}

// Calculate the average of the numbers
double average = (double) sum / numbers.size();

// Output the numbers, sum, and average
System.out.println("Numbers: " + numbers);
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
}
}
“`

After running the above program, you will see the output displayed in the console. It will show the numbers generated, the sum of the numbers, and the average of the numbers.

Please note that you will need to have the Java Development Kit (JDK) installed on your computer in order to compile and run this program.

The post Write a Java program that inserts 25 random integers rangin… appeared first on My Perfect Tutors.

 

"Is this question part of your assignment? We Can Help!"

Essay Writing Service