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

Java Array Application Write a class named ArraySort that has no instance variab

ID: 671040 • Letter: J

Question

Java Array Application

Write a class named ArraySort that has no instance variables, and the following public static methods

public static void main(String[] args) - this main method performs the following steps:

+ Prompts for the size of the array to be created (limit it to between 3 and 10).
+ Creates a Scanner to read the int value for the array size, reads it, and verifies it is within the correct range.
+ Dynamically creates a local integer array of the requested size.
+ Calls the fillArray method to read values into the array (passing its local array as the parameter).
+ Calls the printArray method to print the unsorted values (passing its local array as the parameter)..
+ Calls the sortArray method to sort the array values into ascending order (passing its local array as the parameter)..
+ Calls the printArray method to print the sorted values (passing its local array as the parameter).
+ Note that the array of integers is a local array created inside of the main() method, and is not a static member of the class. The array is passed as a parameter to the other methods, as outlined above and below..

public static void fillArray(int[] array) - this method fills the parameter array with values. It creates a local Scanner object, loops once for each array element, prompts for a value, reads an integer value using the Scanner, and assigns it to the array element.

public static void printArray(int[] array) - this method prints all the values in the parameter array.

public static void sortArray(int[] array) - this method sorts the values in the parameter array into ascending order. You must write the Java code to do the actual sorting of the array values

-- Sample Output --

Enter the size of the array (3 to 10):
3
Enter 3 values
Enter value for element 0:
15
Enter value for element 1:
10
Enter value for element 2:
5

The unsorted values...
15
10
5

The sorted values...
5
10
15

Done - press enter key to end program

Explanation / Answer

import java.util.Scanner;
class ArraySort {
public static void fillArray(int[] array){
Scanner sc = new Scanner(System.in);
for(int i=0;i<array.length;i++){
System.out.println("Enter value for element "+i+":");
array[i]= sc.nextInt();
}
}
public static void printArray(int[] array) {
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
public static void sortArray(int[] array) {
for(int i=0;i<array.length-1;i++){
for(int j=i+1;j<array.length;j++) {
if(array[i]>array[j]){
int temp=array[i];
   array[i]=array[j];
   array[j]=temp;
}
}
}
}
public static void main(String args[]){
System.out.println("Enter the size of the array (3 to 10):");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
if(i<3 || i>10){
System.out.println("Invalid Range");
}
else{
int array[]=new int[i];
fillArray(array);
System.out.println("The Unsorted Values...");
printArray(array);
sortArray(array);
System.out.println("The Sorted Values...");
printArray(array);
System.out.println("Done - press enter key to end program");
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote