Please help me write this code in Java 8 . Please make sure that the code meets
ID: 3749452 • Letter: P
Question
Please help me write this code in Java 8. Please make sure that the code meets all the needed requirements mentioned in the question under each part such as in INPUT FORMAT, OUTPUT FORMAT, AND CONSTRAINTS. In the question it says to create a Java file for each sorting method, but you can choose to just put them all in 1 file with seperate classes for each sorting method. Just make sure that the output of each sorting method matches the sample output given in the question for that specific sorting method. The user input to get the given outputs is the same for all the methods and is shown in the question below.
I have asked this question four times before, but got a completely wrong answer that didn't meet the requirements so please provide me with the correct answer that does meet all the requirements. I am running out of questions, so please only answer if your outputs match the outputs in the question. Thank You!
-----------------------------------------------------------------------------
You'll be given a list of students, and you'll need to sort it using merge sort, heap sort, and quick sort. We want to sort the students according to graduation year (soonest first), and for students with the same graduation year, alphabetically by first name (and if there are any students with the same first name and graduation year, ties should be broken by last name). However, rather than just printing the final sorted list: for merge sort you'lI print the partially sorted list after each call to merge, for quick sort you'll print the partially sorted list after each call to partition, and for heap sort you'l print the list after it has been converted to a heap and the partially sorted list after each call to extractMax. You will have 3 files named: MergeSort.java, HeapSort.java, and QuickSort.java. Constraints You can assume n is a non-negative integer. You can further assume the list contains no students with the same first name, last name, and graduation year. For merge sort when splitting your array, if it has odd size, make the left half larger than the right half. For quick sort when choosing your pivot, if the array has even size, choose the left of the two middle elements Input Format The first line of the input consists of an integer, n, indicating the number of words you'll need to sort. The next n lines will each consist of student data with space separated first name, last name, and graduation year, in that order. The input will terminate with a blank line Output Format You will be printing the partially sorted list: for merge sort as many times as merge is called (the last time will be entirely sorted), for quick sort you will be printing the partially sorted list as many times as partition is called (the last time will be entirely sorted), and for heap sort you will be printing the partially sorted list after it has been heapified and again after every call to extractMax (the last time will be entirely sorted). Each student should appear on a new line without terminating whitespace, space separated in the same format it was provided in the input, and each partially sorted list should be followed by a blank line to indicate where it ends and the next one beginsExplanation / Answer
Note: I have fixed quick sort issue. Plese verify
####################### Student.java ########################
package sorting;
package sorting;
/**
* The Class Student.
*/
public class Student implements Comparable<Student> {
/** The first name. */
private String firstName;
/** The last name. */
private String lastName;
/** The grduation year. */
private int grduationYear;
/**
* Instantiates a new student.
*
* @param firstName the first name
* @param lastName the last name
* @param grduationYear the grduation year
*/
public Student(String firstName, String lastName, int grduationYear) {
this.firstName = firstName;
this.lastName = lastName;
this.grduationYear = grduationYear;
}
/**
* Gets the first name.
*
* @return the first name
*/
public String getFirstName() {
return firstName;
}
/**
* Sets the first name.
*
* @param firstName the new first name
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Gets the last name.
*
* @return the last name
*/
public String getLastName() {
return lastName;
}
/**
* Sets the last name.
*
* @param lastName the new last name
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* Gets the grduation year.
*
* @return the grduation year
*/
public int getGrduationYear() {
return grduationYear;
}
/**
* Sets the grduation year.
*
* @param grduationYear the new grduation year
*/
public void setGrduationYear(int grduationYear) {
this.grduationYear = grduationYear;
}
/**
* to compare two students objects
*/
@Override
public int compareTo(Student student) {
if (grduationYear != student.grduationYear) {
return getGrduationYear() - student.getGrduationYear();
}
else if (!firstName.equals(student.firstName)) {
return getFirstName().compareTo(student.getFirstName());
}
else {
return getLastName().compareTo(student.getLastName());
}
}
@Override
public String toString() {
return String.format("%s %s %s", firstName, lastName, grduationYear);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
}
else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
}
else if (!lastName.equals(other.lastName))
return false;
return true;
}
}
#################################### MergeSort.java ###################################
package sorting;
/**
* The Class MergeSort.
*/
public class MergeSort {
/**
* Merge.
*
* @param arr the arr
* @param l the l
* @param m the m
* @param r the r
*/
void merge(Student arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
Student left[] = new Student[n1];
Student right[] = new Student[n2];
for (int i = 0; i < n1; ++i)
left[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
right[j] = arr[m + 1 + j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (left[i].compareTo(right[j]) <= 0) {
arr[k] = left[i];
i++;
}
else {
arr[k] = right[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = left[i];
i++;
k++;
}
while (j < n2) {
arr[k] = right[j];
j++;
k++;
}
}
/**
* Sort.
*
* @param arr the arr
* @param l the l
* @param r the r
*/
void sort(Student arr[], int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
printArray(arr);
}
}
/**
* Prints the array.
*
* @param arr the arr
*/
void printArray(Student arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.println(arr[i].toString());
System.out.println();
}
}
#################################### QuickSort.java ###################################
package sorting;
/**
* The Class QuickSort.
*/
public class QuickSort {
/**
* Sort.
*
* @param arr the arr
* @param low the low
* @param high the high
*/
public void sort(Student[] arr, int low, int high) {
// check for empty or null array
if (arr == null || arr.length == 0) {
return;
}
if (low >= high) {
return;
}
partiton(arr, low, high);
printArray(arr);
}
/**
* Partiton.
*
* @param arr the arr
* @param low the low
* @param high the high
*/
private void partiton(Student[] arr, int low, int high) {
// Get the pivot element from the middle of the list
int middle = 0;
if (arr.length % 2 == 0) {
middle = low + ((high - low) / 2 + 1);
}
else {
middle = low + (high - low) / 2;
}
Student pivot = arr[middle];
// make left < pivot and right > pivot
int i = low, j = high;
while (i <= j) {
// Check until all values on left side array are lower than pivot
while (arr[i].compareTo(pivot) < 0) {
i++;
}
// Check until all values on left side array are greater than pivot
while (arr[j].compareTo(pivot) > 0) {
j--;
}
// Now compare values from both side of lists to see if they need
// swapping
// After swapping move the iterator on both lists
if (i <= j) {
swap(arr, i, j);
i++;
j--;
}
}
// Do same operation as above recursively to sort two sub arrays
if (low < j) {
sort(arr, low, j);
}
if (high > i) {
sort(arr, i, high);
}
}
/**
* Swap.
*
* @param array the array
* @param x the x
* @param y the y
*/
public void swap(Student array[], int x, int y) {
Student temp = array[x];
array[x] = array[y];
array[y] = temp;
}
/**
* Prints the array.
*
* @param arr the arr
*/
void printArray(Student arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.println(arr[i].toString());
System.out.println();
}
}
#################################### HeapSort.java ###################################
package sorting;
/**
* The Class HeapSort.
*/
public class HeapSort {
/** The size. */
private int size;
/**
* Sort.
*
* @param arr the arr
*/
public void sort(Student arr[]) {
heapify(arr);
System.out.println("After heapified.....");
printArray(arr);
for (int i = size; i > 0; i--) {
swap(arr, 0, i);
size = size - 1;
extractMax(arr, 0);
printArray(arr);
}
}
/**
* Heapify.
*
* @param arr the arr
*/
public void heapify(Student arr[]) {
size = arr.length - 1;
for (int i = size / 2; i >= 0; i--)
extractMax(arr, i);
}
/**
* Extract max.
*
* @param arr the arr
* @param i the i
*/
public void extractMax(Student arr[], int i) {
int left = 2 * i;
int right = 2 * i + 1;
int max = i;
if (left <= size && arr[left].compareTo(arr[i]) > 0)
max = left;
if (right <= size && arr[right].compareTo(arr[max]) > 0)
max = right;
if (max != i) {
swap(arr, i, max);
extractMax(arr, max);
}
}
/**
* Swap.
*
* @param arr the arr
* @param i the i
* @param j the j
*/
public void swap(Student arr[], int i, int j) {
Student tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
/**
* Prints the array.
*
* @param arr the arr
*/
void printArray(Student arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.println(arr[i].toString());
System.out.println();
}
}
#################################### HeapSort.java ###################################
package sorting;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SortingTestManager {
public static void main(String[] args) {
File file = new File("input.txt");
Scanner scn;
try {
scn = new Scanner(file);
int n = Integer.parseInt(scn.next());
Student[] students = new Student[n];
int count = 0;
while (scn.hasNext()) {
Student student = new Student(scn.next(), scn.next(), Integer.valueOf(scn.next()));
students[count++] = student;
}
scn.close();
doMergeSorting(n, students);
doQuickSorting(n, students);
doHeapSorting(n, students);
}
catch (FileNotFoundException e) {
System.exit(0);
}
}
private static void doMergeSorting(int n, Student[] students) {
System.out.println("############# Performing Merge Sort ###################");
MergeSort mergeSort = new MergeSort();
mergeSort.sort(students, 0, n - 1);
}
private static void doQuickSorting(int n, Student[] students) {
System.out.println("############# Performing Quick Sort ###################");
QuickSort quickSort = new QuickSort();
quickSort.sort(students, 0, n - 1);
}
private static void doHeapSorting(int n, Student[] students) {
System.out.println("############# Performing Heap Sort ###################");
HeapSort heapSort = new HeapSort();
heapSort.sort(students);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.