Java: create a SortArray class and a test class to bubbleSort, InsertionSort, an
ID: 3585847 • Letter: J
Question
Java: create a SortArray class and a test class to bubbleSort, InsertionSort, and selectionSort based on user input:
Tasks: Implement the sorting techniques Insertion, Bubble, and Selection using the following outline. Importnt notes: You MUST follow the following outline.
a. Class SortArray
b. Attribute: String s ;// sequence of intergers separated by blankd ( all ending blanks shoulde be removed by trim();
c. static int[] sA ; //this is a global variable, NOT an attribute; it is the outcome of split() the string attribute mentionned above.
d. Constructors. The will be 3 constructors: i. The one without parameter: //because users will enter the attribute via keyboard one line of all integrs separated by blanks. ii. The one with a string parameter as the attribute iii. The one with array parameter as n array of integers.
e. The method toString to display the global variable sA
f. Three methods to sort the attribute and store the result to sA
g. Other methods needed to support items b-f
4) class Test includes only main to address the following tasks:
a. an object is created by the first constructor ( the one without parameter)
b. its atrributes is sorted by one of the sorting techniques.
c. Display the outcome ( i.e., print the array sA)
d. Another object is created by the second constructor
e. Display its attribute
f. Sort this attribute by one of the remaining sorting techniques.
g. Display the outcome of sorting
h. Repeat 4 d-g using the last remaining sorting technique. NOTE: All output must be labeled to identify which instruction performs the output.
Explanation / Answer
SortArray.java
public class SortArray {
//Declaring instance variables
private String s;
//Declaring static variable
public static int[] sA;
//Zero argumented constructor
public SortArray() {
}
//Parameterized constructor
public SortArray(String str) {
String arr[] = str.split(" ");
int nos[] = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
nos[i] = Integer.parseInt(arr[i]);
}
sA = nos;
this.s = str;
}
//Parameterized constructor
public SortArray(int arr[]) {
sA = arr;
}
//getters and setters
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
String arr[] = s.split(" ");
int nos[] = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
nos[i] = Integer.parseInt(arr[i]);
}
sA = nos;
}
//This method will sort the array using bubble sort
public void bubbleSort() {
int temp;
for (int i = 0; i < sA.length; i++) {
for (int j = i + 1; j < sA.length; j++) {
if (sA[i] > sA[j]) {
temp = sA[i];
sA[i] = sA[j];
sA[j] = temp;
}
}
}
}
//This method will sort the array using insertion sort
public void insertionSort() {
for (int m = 1; m < sA.length; m++) {
int val = sA[m];
int n = m - 1;
while ((n > -1) && (sA[n] > val)) {
sA[n + 1] = sA[n];
n--;
}
sA[n + 1] = val;
}
}
/*
* This method will sort the elements in the array using selection sort
* algorithm
*/
public void selectionSort() {
int small;
for (int i = 0; i < sA.length; i++) {
int m = i;
for (int j = i + 1; j < sA.length; j++) {
if (sA[j] < sA[m])
m = j;
}
small = sA[m];
sA[m] = sA[i];
sA[i] = small;
}
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
System.out.println("After Sorting :");
for (int i = 0; i < sA.length; i++) {
System.out.print(sA[i] + " ");
}
return " ";
}
}
_____________________
TestClass.java
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
//Declaring variables
String str;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.println("Enter Sequence of Numbers :");
str = sc.nextLine();
//Creating an SortArray class object
SortArray sa1 = new SortArray();
//calling the setter method
sa1.setS(str);
//calling the method
sa1.bubbleSort();
System.out.println(sa1.toString());
System.out.println(" Enter Sequence of Numbers :");
str = sc.nextLine();
//Creating an SortArray class object by passing the string as argument
SortArray sa2 = new SortArray(str);
//calling the method
sa2.insertionSort();
System.out.println(sa2.toString());
System.out.print(" How many number you want to enter :");
int size = sc.nextInt();
int nums[] = new int[size];
for (int i = 0; i < size; i++) {
System.out.print("Enter Number#" + (i + 1) + ":");
nums[i] = sc.nextInt();
}
//Creating an SortArray class object by passing the integer array as argument
SortArray sa3 = new SortArray(nums);
//calling the method
sa3.selectionSort();
System.out.println(sa3.toString());
}
}
___________________
Output:
Enter Sequence of Numbers :
34 43 54 11 23 78 85 49 92 28 74 55 70 91
After Sorting :
11 23 28 34 43 49 54 55 70 74 78 85 91 92
Enter Sequence of Numbers :
34 56 71 89 90 39 84 56 71 1 49 80 54
After Sorting :
1 34 39 49 54 56 56 71 71 80 84 89 90
How many number you want to enter :10
Enter Number#1:45
Enter Number#2:56
Enter Number#3:54
Enter Number#4:32
Enter Number#5:1
Enter Number#6:89
Enter Number#7:90
Enter Number#8:87
Enter Number#9:67
Enter Number#10:57
After Sorting :
1 32 45 54 56 57 67 87 89 90
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.