In this assignment, you\'ll write a Java class that creates an array of integers
ID: 3562911 • Letter: I
Question
In this assignment, you'll write a Java class that creates an array of integers, fills it with values, prints the unsorted values, sorts the values into ascending order, and finally prints the sorted values.
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:
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 (see example logic below).
Sorting
One simple technique of sorting the values in an array into order operates as follows:
Implementing this logic usually is done with nested loops (a loop within a loop). The outer loop keeps repeating until no swaps have occurred. The inner loop steps through the array, comparing adjacent pairs of values. The following pseudo-code illustrates the logic required.
do {
swapped = false
for each element in the array
{
if (element N is greater than element N+1)
{
swap the element values at N and N+1
swapped = true
}
}
} while swapped is true
Note that the inner for loop actually loops one less time than the number of elements in the array, as there is one less pair than number of elements (eg. if three elements in the array, there are two pairs to compare).
As an example, consider the following array of four values:
30 20 40 10
During the first pass through the array, each pair of values is compared (the ones in bold), and if out of order, swapped (the results are shown on the right, with the swapped values in italics). The result of the first pass is shown at the end.
30 20 40 10 => 20 30 40 10
20 30 40 10
20 30 40 10 => 20 30 10 40
20 30 10 40
Since a swap occurred, a second pass through the array is performed using the same process:
20 30 10 40
20 30 10 40 => 20 10 30 40
20 10 30 40
Since a swap occurred, a third pass through the array is performed:
20 10 30 40 => 10 20 30 40
10 20 30 40
10 20 30 40
Since a swap occurred, a fourth pass is required. However, since the array is now in order, no swaps occur during the fourth pass, and the sort completes.
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
Test Data
Use the following test data, plus an example of your own:
1000, -200, 500, 150, 600, 800, 150, 50, -25
Explanation / Answer
please rate - thanks
any questions ask
SAMPLE RUNS
----jGRASP exec: java ArraySort
Enter the size of the array (3 to 10):
13
must be between 3 and 10
Enter the size of the array (3 to 10):
-2
must be between 3 and 10
Enter the size of the array (3 to 10):
9
Enter 9 values
Enter value for element 0:
1000
Enter value for element 1:
-200
Enter value for element 2:
500
Enter value for element 3:
150
Enter value for element 4:
600
Enter value for element 5:
800
Enter value for element 6:
150
Enter value for element 7:
50
Enter value for element 8:
-25
The unsorted values...
1000
-200
500
150
600
800
150
50
-25
The sorted values...
-200
-25
50
150
150
500
600
800
1000
Done - press enter key to end program
----jGRASP: operation complete.
----jGRASP exec: java ArraySort
Enter the size of the array (3 to 10):
4
Enter 4 values
Enter value for element 0:
60
Enter value for element 1:
50
Enter value for element 2:
90
Enter value for element 3:
-4
The unsorted values...
60
50
90
-4
The sorted values...
-4
50
60
90
Done - press enter key to end program
----jGRASP: operation complete.
import java.util.*;
class ArraySort{
public static void main(String[] args)
{Scanner in=new Scanner(System.in);
int num;
System.out.println("Enter the size of the array (3 to 10): ");
num=in.nextInt();
while(num<3 || num>10) //check range of number entered
{System.out.println("must be between 3 and 10"); //give error and reask if out of range
System.out.println("Enter the size of the array (3 to 10): ");
num=in.nextInt();
}
in.nextLine(); //clear enter from input buffer
int[] array = new int[num];
fillArray(array); //fill the array
System.out.println("The unsorted values..."); //print it
printArray(array);
sortArray(array); //sort it
System.out.println("The sorted values...");
printArray(array); //and print it again
System.out.println("Done - press enter key to end program");
in.nextLine();
}
public static void fillArray(int[] array) //fill the array
{Scanner in=new Scanner(System.in);
int i;
System.out.println("Enter "+ array.length+" values");
for(i=0;i<array.length;i++)
{System.out.println("Enter value for element " + i + ":");
array[i]=in.nextInt();
}
}
public static void printArray(int[] array) //print the array
{int i;
for(i=0;i<array.length;i++)
System.out.println(array[i]);
System.out.println();
}
public static void sortArray(int[] array) //sort the array ascending
{boolean swapped;
int i,temp;
do
{swapped=false;
for(i=0;i<array.length-1;i++)
{if(array[i]>array[i+1]) //if the element is larger then the next element
{temp=array[i]; //swap them
array[i]=array[i+1];
array[i+1]=temp;
swapped=true;
}
}
}while(swapped); //do this until there are no more swaps
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.