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

I need some help with Merge Sort. I understand how it works, just having some pr

ID: 3641543 • Letter: I

Question

I need some help with Merge Sort. I understand how it works, just having some problems implementing it. I have 2 classes and a driver. My first class is called Person and has the appropriate get set methods for personID, personName, and personAge. The second class is called employee and contains testdata for the array which is to be used. I need to assemble the testdata into an array of employees, and return it it to the driver, then call on MergeSort() without any arguments and sort the array and pass that result back to the driver. The employees need to be sorted by personID.

Here are the employee class and driver. (I can't change the driver as it was provided to use as-is)
***Employee Class***
public class employee
{
private Person Employees[];

public employee()
{
}

public employee(int number)
{
Employees = new Person[number];
}

public Person[] MergeSort()
{
// this is where I need help
}

public void CreateTestData()
{
Employees[0] = new Person(39, "Mark", 19);
Employees[1] = new Person(67, "Kevin", 39);
Employees[2] = new Person(25, "Frank", 47);
Employees[3] = new Person(93, "Maria", 56);
Employees[4] = new Person(31, "Evan", 29);
Employees[5] = new Person(18, "Audrey", 72);
Employees[6] = new Person(46, "Debbie", 65);
Employees[7] = new Person(84, "Ruth", 19);
Employees[8] = new Person(62, "Kathy", 71);
Employees[9] = new Person(10, "Mona", 45);
}

public String toString()
{
for(int l=0; l < Employees.length ; l++)
System.out.println(Employees[l].toString());
return "";
} // end method toString
}

*** Driver ***
import java.util.Scanner;
public class Driver
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
employees myEmployees = new employee(10);
myEmployees.CreateTestData();
System.out.println(myEmployees.toString() + " ");
myEmployees.MergeSort();
System.out.println(myEmployees.toString() + " ");
}
}

Explanation / Answer

I have done two changes to do a sort. 1) Person class Implements Comparable interface - because wrapper classes default are comparable. But here we have to sort our custom defined class - Person. 2) In Person class I have written a compareTo method, that compare the current and parameter object - helpful when sort is done. 3) In mergeSort method - have called Arrays.sort and passed the created Employee - Array of person object , this will sort the employee object. Please find below the program. Save the below file as Driver.Java and compile and run it. import java.util.Arrays; import java.util.Scanner; class Person implements Comparable { private int personID; private String personName; private int personAge; public Person() { } public Person(int id, String name, int age) { personID = id; personName = name; personAge = age; } public void setpersonID(int id) { personID = id; } public int getpersonID() { return personID; } public void setpersonName(String name) { personName = name; } public String getpersonName() { return personName; } public void setpersonAge(int age) { personAge = age; } public int getpersonAge() { return personAge; } public String toString() { return "EmployeeID: " + personID + " Employee Name: " + personName + " Employee Age: " + personAge; } public int compareTo(Object o) { Person p = (Person) o; Integer i1 = p.personID; Integer i2 = this.personID; return i2.compareTo(i1); } } // End Class Person class employee { private Person Employees[]; public employee() { } public employee(int number) { Employees = new Person[number]; } public Person[] MergeSort() { Arrays.sort(Employees); return Employees; } public void CreateTestData() { Employees[0] = new Person(39, "Mark", 19); Employees[1] = new Person(67, "Kevin", 39); Employees[2] = new Person(25, "Frank", 47); Employees[3] = new Person(93, "Maria", 56); Employees[4] = new Person(31, "Evan", 29); Employees[5] = new Person(18, "Audrey", 72); Employees[6] = new Person(46, "Debbie", 65); Employees[7] = new Person(84, "Ruth", 19); Employees[8] = new Person(62, "Kathy", 71); Employees[9] = new Person(10, "Mona", 45); } public String toString() { for (int l = 0; l