Create a UML class diagram of the application illustrating class hierarchy, collaboration, and the content of each class. There is only one class. There is a main method that calls four methods. I am not sure if I made the UML Class diagram correct.
Create a UML class diagram of the application illustrating class hierarchy, collaboration, and the content of each class.
There is only one class. There is a main method that calls four methods. I am not sure if I made the UML Class diagram correct.
import java.util.Random; // import Random package
import java.util.Scanner; // import Scanner Package
public class SortArray{ // class name
public static void main(String[] args) { //main method
Scanner scanner = new Scanner(System.in); // creates object of the Scanner class
int[] array = initializeArray(scanner);
System.out.print("Array of randomly generated values: ");
displayArray(array); // Prints unsorted array
sortDescending(array); // sort the array in descending order
System.out.print("Values in desending order: ");
displayArray(array); // prints the sorted array, should be in descending order
sortAscending(array); // sort the array in ascending order
System.out.print("Values in asending order: ");
displayArray(array); // prints the sorted array, should be in ascending order
} // end main()
public static int[] initializeArray(Scanner scan) { //initializeArray method
System.out.print("Enter an array length: "); // ask user to enter a number
int n = scan.nextInt(); // parse user input
// The while loop below validates user input.
// It runs repeatedly until user enters a numbet greater than zero.
while (n <= 0) {
System.out.println("Length of array must be greater than 0.");
System.out.print("Enter an array length: " ); // ask user to enter a number
n = scan.nextInt(); // parse user input
}
int[] myArray = new int[n]; // instantiate array with specified length
Random ranValue = new Random(); // creates random object
int maxValue = 1000; // valid ints: 0-999, 1000 is the upperbound.
for (int i = 0; i < myArray.length; i++) { // loop to access all elements in array
myArray[i] =ranValue.nextInt(maxValue); // storing random integers, from 0-999, in the
// array from index 0
}
return myArray; // return the array of random integers
}
public static void sortDescending(int[] array) { //sortDescending method using bubblesort
int n = array.length;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (array[j - 1] < array[j]) {
//swap the elements
int temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
}
public static void sortAscending(int[] array) { //sortAscending method using bubblesort algorithm
int n = array.length;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (array[j - 1] > array[j]) {
//swap elements
int temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
}
public static void displayArray(int[] array) { //displayArray method
for (Integer number : array) {
System.out.print(number + " ");
}
System.out.println();
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images