In: Computer Science
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing:
The program should use three different methods to implement the functionalities above.
Call the primary source file ArrayManipulator.java
import java.util.Random;
public class Main
{
public static void main(String[] args)
{
Random rand = new Random(); // create object of Random class to
call
int[] arr= new int[10];// create array of size 10
// loop to fill array with random numbers between 0 to 50
for(int i=0; i<arr.length;i++)
arr[i]= rand.nextInt(50);
System.out.println("Every element at an odd index :");
oddIndex(arr);
System.out.println("Every odd element in array :");
oddElements(arr);
System.out.println("\nArray contents in reverse order are
:");
reverse(arr);
}
// function to print elements at odd index
static void oddIndex(int arr[])
{
for(int i=1;i<arr.length;i+=2)
System.out.println("index =" + i + "element = " +arr[i]);
}
static void oddElements(int arr[])
{
for(int i=0; i<arr.length;i++)
{
if (arr[i]%2!=0)
System.out.println("index =" + i + "element = " +arr[i]);
}
}
//function to print in reverse
static void reverse(int arr[])
{
for(int i=arr.length-1;i>=0;i--)
System.out.print( "\t" + arr[i] );
}
}