In: Computer Science
You are expected to write a program from scratch. In the program, an array will be initialized with 23 random integers between 1000 and 1999 (inclusive). The output of the program is 4 lines on the screen, specifically,
Note that you have to use the method Math.random() we covered previously to generate the random numbers. Other ways like using Random class we haven't covered or just reading in 23 numbers from keyboards are NOT accepted.
Please explain/comment on what each line means and what the process is. Thank you, I appreciate it.
Please find the code below::
RandomArray.java
package classes10;
public class RandomArray {
public static void main(String[] args) {
//declare array of size 23
int myArray[] = new int[23];
//populate array with random
data
for(int i=0;i<23;i++){
myArray[i] =
(int) (Math.random()*1000+999);
}
//printing array in right
order
for(int i=0;i<23;i++){
System.out.print(myArray[i]+" ");
}
System.out.println();
//printing array in reverse
order
for(int i=22;i>=0;i--){
System.out.print(myArray[i]+" ");
}
System.out.println();
//printing odd index number less
than 1500
for(int i=0;i<23;i++){
if(i%2==0
&& myArray[i]<1500)
System.out.print(myArray[i]+" ");
}
System.out.println();
//printing odd index number less
than 1500
for(int i=0;i<23;i++){
if(myArray[i]>1500 && myArray[i]%2!=0)
System.out.print(myArray[i]+" ");
}
}
}
output: