Java Question Create a Java program that performs the following tasks: 1. 2. Dec
ID: 3688946 • Letter: J
Question
Java Question
Create a Java program that performs the following tasks: 1. 2. Declare an integer array capable of storing 15 values Generate 15 random numbers using the java.util.Random class A. The range of the random numbers should be between 0 and 1000 8. Display the unsorted contents of the array on a single line Store the random values in the array 3. Separate the values using commas and spaces 4. 5. A. Sort the array in ascending order using the Bubble Sort algorithm Display the sorted contents of the array on a single line (see Example Output section) A. Separate the values using commas and spacesExplanation / Answer
Answer-
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Random;
class Codechef
{
public static void main(String [] args)
{
int [] ar = new int[15];
int rand,i=0; /* initialize random seed: */
Random rnd = new Random(); /* generate secret number between 1 and 10: */
do
{
rand = rnd.nextInt(1000);
ar[i]=rand;
i++;
}while (i<15);
System.out.println("Array unsorted");
i=0;
do
{
System.out.print(ar[i]+", ");
i++;
}while (i<15);
int temp = 0;
for(i=0; i < 15; i++)
{
for(int j=1; j < (15-i); j++)
{
if(ar[j-1] > ar[j])
{
temp = ar[j-1];
ar[j-1] = ar[j];
ar[j] = temp;
}
}
}
System.out.println(" Array Sorted");
i=0;
do
{
System.out.print(ar[i]+", ");
i++;
}while (i<15);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.