Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Exercise1: Generate 30 random numbers from the following range: {-3, -2, -1

ID: 3604745 • Letter: J

Question

JAVA

Exercise1:
Generate 30 random numbers from the following range: {-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8}

For each of the numbers do the following:

If it is a negative number print the string neg otherwise print the number itself
However, it is a multiple of 4 (0, 4, 8) do NOT print the number. Instead print @@ ( 6 pts )
Notice that the output varies in length (one digit numbers, two characters @@ and three characters neg)
To produce a neat output use left aligned columns of width 6 (see sample output below).
Each row should have exactly 6 columns
When you are done displaying the numbers print all done
It should be printed below the other output separated by a single line.
Below you can see a sample result. Because the numbers are random your output is likely to look different.
Sample Output
6 7 neg @@ neg neg
neg 6 neg 2 1 @@
2 6 2 6 3 6
neg @@ 1 @@ 7 1
7 7 7 neg @@ neg
all done

Explanation / Answer

RandomNumberDisplay.java

import java.util.Random;

public class RandomNumberDisplay {

public static void main(String[] args) {

Random r = new Random();

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

int n = r.nextInt(8 + 1 + 3) - 3;

if(n < 0) {

System.out.print("neg ");

} else if (n % 4 == 0) {

System.out.print("@@ ");

} else {

System.out.print(n+" ");

}

if((i+1) % 6 == 0) {

System.out.println();

}

}

System.out.println("all done");

}

}

Output:

5 5 5 @@ 2 @@
neg @@ neg 3 neg 1
1 5 7 5 neg 5
3 neg @@ @@ 2 @@
@@ @@ neg neg 1 @@
all done