In: Computer Science
Programming
Write a program that initializes an array with 10 random integers between 0 and 100 and then prints exactly 5 lines of output. These 5 lines should contain:
Initially write your code so that you initialize the array in the main method. You will not ask the user for any input, but will use Math.random to generate 10 random numbers between 0 and 100 to put in the array.
As an example, if the array contained { 34, 82, 45, 92, 71, 1, 18, 20, 52, 99 }, the five lines would be:
Array: [ 34, 82, 45, 92, 71, 1, 18, 20, 52, 99 ]
Elements at even index: 34 45 71 18 52
Even elements: 34 82 92 18 20 52
Reverse order: 99 52 20 18 1 71 92 45 82 34
First: 34 and Last: 99
If you complete all the steps above, please go back and move your code that fills the array into a separate method within your class. The method will create the array, fill it with the random numbers, and then return it to the caller.
Remember to use good coding style, and javadoc comments where appropriate.
NOTE: Please read instruction and example carefully while writing java code. Please include JAVADOC too. Thank you in advance.
CODE:
import java.util.*;
class randomNumbers {
public static void main(String[] args) {
Random r = new Random();
int minValue = 0, maxValue =
100;
int randomIntegersArray[] = new
int[10];
for (int i = 0; i <
10; i++)
// Randomly
generating values with range 0 - 100
randomIntegersArray[i] = r.nextInt(maxValue - minValue) +
minValue;
// Displaying
randomIntegersArray using Arrays.toString() method
System.out.print("Array : " +
Arrays.toString(randomIntegersArray));
// Displaying even
indexes element in randomIntegersArray
System.out.print("\nElements at
even index : ");
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
{
System.out.print(randomIntegersArray[i] + "
");
}
}
System.out.print("\nEven
elements : ");
for (int i = 0; i < 10; i++)
{
// Accessing
every even element in randomIntegersArray
if
(randomIntegersArray[i] % 2 == 0) {
System.out.print(randomIntegersArray[i] + "
");
}
}
// Displaying elements
in reverse order of randomIntegersArray elements
System.out.print("\nReverse order:
");
for (int i = 9; i >= 0;
i--)
// Accessing
every element in reverse order of randomIntegersArray
System.out.print(randomIntegersArray[i] + " ");
// Displaying first and
last elements in randomIntegersArray
System.out.println("\nFirst : " +
randomIntegersArray[0] + " and Last :"
+ randomIntegersArray[9]);
}
}
Screenshot of Code & Output:
