A Java program to demonstrate use of Comparable

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

**Students.java code**

// A Java program to demonstrate use of Comparable
import java.util.*;

// A class 'Student' that implements Comparable
class Student implements Comparable<Student>
{
    private String FirstName;
    private String LastName;
    private int StudentID;

    // Used to sort movies by year
    public int compareTo(Student a)
    {
        return this.StudentID - a.StudentID;
    }

    // Constructor
    public Student(String fm, String ls, int id)
    {
        this.FirstName = fm;
        this.LastName = ls;
        this.StudentID = id;
    }

    // Getter methods for accessing private data
    public String getFirstName() { return FirstName; }
    public String getLastName() { return LastName; }
    public int getStudentID()     { return StudentID; }
}

// Driver class
class Students
{
    public static void main(String[] args)
    {
        ArrayList<Student> list = new ArrayList<Student>();
        list.add(new Student("Amir", "Alao", 123456));
        list.add(new Student("John", "Smith", 654321));
        list.add(new Student("Mario", "Ellie", 198045));
        list.add(new Student("James", "Harden", 198312));
        list.add(new Student("LeBron", "James", 139284));
        System.out.println("Student names before Sorting : ");
    

        for (Student student: list)
        {
            System.out.println(student.getFirstName() + " " +
                            student.getLastName() + " " +
                            student.getStudentID());
        }
        System.out.println("*******************Sorting algorithm Implemented. Students are Arranged by Student ID ********************* : ");

        Collections.sort(list);

        System.out.println("Student names after Sorting : ");
        for (Student student: list)
        {
            System.out.println(student.getFirstName() + " " +
                            student.getLastName() + " " +
                            student.getStudentID());
        }
    }
}

 

 

***ObjectMergeSorter Code***

import java.util.ArrayList;
import java.util.Collections;

public class ObjectMergeSorter {
   // calls recursive sortArray method to begin merge sorting
                                 

   // splits array, sorts subarrays and merges subarrays into sorted array
   private static void sortArray(int[] Student, int low, int high) {
      // test base case; size of array equals 1     
      if ((high - low) >= 1) { // if not base case
         int middle1 = (low + high) / 2; // calculate middle of array
         int middle2 = middle1 + 1; // calculate next element over     


         // split array in half; sort each half (recursive calls)
         sortArray(Student, low, middle1); // first half of array       
         sortArray(Student, middle2, high); // second half of array     

         // merge two sorted arrays after split calls return
         merge (Student, low, middle1, middle2, high);             
      }                                            
   }                               
   
   // merge two sorted subarrays into one sorted subarray             
   private static void merge(int[] Student, int left, int middle1, 
      int middle2, int right) {

      int leftIndex = left; // index into left subarray              
      int rightIndex = middle2; // index into right subarray         
      int combinedIndex = left; // index into temporary working array
      int[] combined = new int[Student.length]; // working array        
      


      // merge arrays until reaching end of either         
      while (leftIndex <= middle1 && rightIndex <= right) {
         // place smaller of two current elements into result  
         // and move to next space in arrays                   
         if (Student[leftIndex] <= Student[rightIndex]) {       
            combined[combinedIndex++] = Student[leftIndex++]; 
         } 
         else {                                                 
            combined[combinedIndex++] = Student[rightIndex++];
         } 
      } 
   
      // if left array is empty                                
      if (leftIndex == middle2) {                             
         // copy in rest of right array                        
         while (rightIndex <= right) {                        
            combined[combinedIndex++] = Student[rightIndex++];
         } 
      } 
      else { // right array is empty                             
         // copy in rest of left array                         
         while (leftIndex <= middle1) {                        
            combined[combinedIndex++] = Student[leftIndex++]; 
         } 
      } 

      // copy values back into original array
      for (int i = left; i <= right; i++) { 
         Student[i] = combined[i];          
      } 

      // output merged array
     
        
   } 
// Driver class


    }

I need help fixing my codes in my Java Project. I need to make my merge sort algorithm
sort a sample array of comparable "Student" class objects. I made a Student.java class ,
and a ObjectMergeSorter.java class. The Student class sorts the array with collection sort,
but i need it to sort the list array with merge sort, which i tried to define in the
ObjectMergeSorter class.The codes will be attached below.
The Project should print unsorted and sorted sample array of at least 5 sample
Comparable Student data and verify output that the merge sort work correctly.
*******k*
Transcribed Image Text:I need help fixing my codes in my Java Project. I need to make my merge sort algorithm sort a sample array of comparable "Student" class objects. I made a Student.java class , and a ObjectMergeSorter.java class. The Student class sorts the array with collection sort, but i need it to sort the list array with merge sort, which i tried to define in the ObjectMergeSorter class.The codes will be attached below. The Project should print unsorted and sorted sample array of at least 5 sample Comparable Student data and verify output that the merge sort work correctly. *******k*
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY