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

public class SortArray { //Declaring instance variables private String s; //Decl

ID: 3603531 • Letter: P

Question

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;
}

//Constructor
public SortArray(int arr[]) {
sA = arr;
}

//Get and Set
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;
}
//Bubble sort Array
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;
}
}
}
}
//Insertion Sort Array
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;
}
}
//Selection Sort
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 to output Display
@Override
public String toString() {
System.out.println("After Sorting :");
for (int i = 0; i < sA.length; i++) {
System.out.print(sA[i] + " ");
}
return " ";
}
}

------------------------------------------------------------------------------------------

//Test Class

import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {

//Variable Declaration
String str;
//Scanner Class for user input

Scanner sc = new Scanner(System.in);

//Requesting input from user

System.out.println("Enter Sequence of Numbers :");

str = sc.nextLine();

//SortArray Class Object

SortArray sa1 = new SortArray();

//Call setter method

sa1.setS(str);

//Call method

sa1.bubbleSort();

System.out.println(sa1.toString());

System.out.println(" Enter Sequence of Numbers :");

str = sc.nextLine();

//SortArray sort

SortArray sa2 = new SortArray(str);

//Call 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();
}

//Pass integer array to create SortArray
SortArray sa3 = new SortArray(nums);

//Call method
sa3.selectionSort();

System.out.println(sa3.toString());

}
}

2) Instructor will compile and execute your classes via Terminal If (You must be careful to make sure that your classes will be compiled by javac in Terminal) the compilation is failed, the grading process will be stopped. 3) Tasks: Extend and modify Project1 to do Quick Sort on a sequence Fractions a. Install Quick Sort into Project 1. Run to test. b. Bring the class Fraction into the same folder for Project3 c. Modify a) and b) to allow to sort on a sequence of fractions. Note: All classes except Test, you need to label all components such as attributes, global variables, constructors, and methods 4) Input/output: a. Input the sequence of fractions can be done via keyboard or a string in the mairn program (Test). Fractions are separated by one or more spaces. Each fraction can be an integer (e.g. 15) or in 2 integers separated by slash '/"' (e.g. 11/2) Output is a detailed work of QuickSort. (See a DEMO in Practices.) Original string = “ 5/10 0/9 5/1 3 8/8 7/10 2 b. “ 1 . Given: array 5/10 0/9 5/1 3 8/8 7/10 2 2. Goal: To sort using the algorithm QuickSort 3. Process: 1. Begin with subArray with Left-0, right-6: 2. Do partition to find index of Cut-5 1/2,0,2,7/10, 1, [3],5 1. Begin with subArray with Left-0, right-4: 2. Do partition to find index of Cut4 1/2,0,1, 7/10, [2], 3,5 1. Begin with subArray with Left 0, right 3:: [12], 0, 1(7/10], 2, 3, 5 2. Do partition to find index of Cutal 0, [1/2],1,7/10,2,3,5 1 . Begin with subAmay with Left= 1, right-3 2. Do partition to find index of Cut3 0, 1/2,7/10, [1],2,3,5 1 . Begin with subarray with Left= 1, right-2: 0, [1/21, [7/10],1,2,3,5 2. Do partition to find index of Cut2 0, 1/2, [7/10],1,2,3,5 1. Begin with subArray with Left- 5, right-6 2. Do partition to find index of Cut =6 0, 1/2,7/10,1,2,3, [5] 4. Answer = 0, 1/2,7/10, 1, 2, 3, 5

Explanation / Answer

the code is modified and fixed

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;
}

//Constructor
public SortArray(int arr[]) {
   sA = arr;
   }

//Get and Set
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;
   }
//Bubble sort Array
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;
         }
      }
   }
}
//Insertion Sort Array
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;
      }
   }
//Selection Sort
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 to output Display
@Override
public String toString() {
   System.out.println("After Sorting :");
   for (int i = 0; i < sA.length; i++) {
      System.out.print(sA[i] + " ");
      }
   return " ";
   }
}

------------------------------------------------------------------------------------------

//Test Class

import java.util.Scanner;
   public class TestClass {
   public static void main(String[] args) {

//Variable Declaration
   String str;
//Scanner Class for user input

   Scanner sc = new Scanner(System.in);

//Requesting input from user

   System.out.println("Enter Sequence of Numbers :");

   str = sc.nextLine();

//SortArray Class Object

   SortArray sa1 = new SortArray();

//Call setter method

   sa1.setS(str);

//Call method

   sa1.bubbleSort();

   System.out.println(sa1.toString());

   System.out.println(" Enter Sequence of Numbers :");

   str = sc.nextLine();

//SortArray sort

   SortArray sa2 = new SortArray(str);

//Call 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();
   }

//Pass integer array to create SortArray
   SortArray sa3 = new SortArray(nums);

//Call method
   sa3.selectionSort();

   System.out.println(sa3.toString());

   }
}