Write an application that allows you to initialize and use a one-dimensional arr
ID: 3742893 • Letter: W
Question
Write an application that allows you to initialize and use a one-dimensional array of 15 double numbers.. Create and call the method which initializes the array with random numbers generated by the Java Random class. Create and use the custom method to sort the array values in a descending orderThen create and use the custom method to sort the array in an ascending order. Do not use any of the prebuilt sorting methods of the Array class— any sort type must be implemented by you from scratch, using e.g., bubble sort algorithm , binary sort algorithm or others. Write a separate displayArray method that takes the sorted array as an argument and displays the array element values, first time you will use this method to display descending order array values, the second time you will use this method to display ascending order array values. Make your application user friendly and explain to potential users what they see ( array of randomly generated values, sorted array values in descending order, sorted array values in ascending order).
Explanation / Answer
Generate15RandomNumbers.java
import java.util.Random;
public class Generate15RandomNumbers {
public static void main(String[] args) {
// Declaring variables
int size = 15;
// Creating an integer Array of size 100
int randNos[] = new int[size];
initializeArray(randNos);
decendingOrder(randNos);
System.out.println("Displaying array elements in Decending order:");
displayArray(randNos);
System.out.println("Displaying array elements in Ascending order:");
ascendingOrder(randNos);
displayArray(randNos);
}
// This method will sort the array elements in ascending order
private static void ascendingOrder(int[] randNos) {
int temp;
for (int i = 0; i < randNos.length; i++) {
for (int j = i + 1; j < randNos.length; j++) {
if (randNos[i] > randNos[j]) {
temp = randNos[i];
randNos[i] = randNos[j];
randNos[j] = temp;
}
}
}
}
// This method will display array elements
private static void displayArray(int[] randNos) {
for (int i = 0; i < randNos.length; i++) {
System.out.print(randNos[i] + " ");
}
System.out.println();
}
// This method will sort the array elements in descending order
private static void decendingOrder(int[] randNos) {
int temp;
for (int i = 0; i < randNos.length; i++) {
for (int j = i + 1; j < randNos.length; j++) {
if (randNos[i] < randNos[j]) {
temp = randNos[i];
randNos[i] = randNos[j];
randNos[j] = temp;
}
}
}
}
private static void initializeArray(int[] randNos) {
// Creating a random Class object
Random r = new Random();
for (int i = 0; i < randNos.length; i++) {
randNos[i] = r.nextInt(1000) + 1;
}
}
}
________________
Output:
Displaying array elements in Decending order:
940 922 853 796 663 561 501 278 230 203 197 180 172 113 68
Displaying array elements in Ascending order:
68 113 172 180 197 203 230 278 501 561 663 796 853 922 940
__________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.