Question

PLEASE READ ALL Modify the MergeSort code provided in class (if you find a mistake with...

PLEASE READ ALL

Modify the MergeSort code provided in class (if you find a mistake with it extra credit)

(https://repl.it/@ashokbasawapatna/MergeSortExample) to work with Students (No need for generic types, you will only write Main.java). Then use the compareTo method for strings in Java to sort the students in Alphabetical order via MergeSort.

1) Create an array of 10 students by hand. Make sure it's not in alphabetical order. ONLY USE LOWER CASES FOR NAMES (I'll only test with that).

2) Modify the MergeSort and Merge function prototypes to work with students

3) Inside the Merge function use compareTo to determine which one comes sooner in the alphabet.

If I have two strings a and b

String a="aba";

String b="abb";

String c="aba";

System.out.println(a.compareTo(b));

System.out.println(b.compareTo(a));

System.out.println(c.compareTo(a));

Will output

-1 1 0

Note anything less than zero means the calling object is before the passed object in the dictionary

Zero means they are equal

1 means the calling object is after the passed object in the dictionary

(GIVEN STUDENT CLASS)

public final class Student
{
   private int schoolID;
   private String name;

   public Student(String namePassed,int schoolIDPassed)
   {
       schoolID= schoolIDPassed;
       name= namePassed;
   }

   public int getSchoolID()
   {
       return schoolID;
   }

   public void setSchoolID(int schoolIDPassed)
   {
       schoolID=schoolIDPassed;
   }

   public String getName()
   {
       return name;
   }

   public void setName(String namePassed)
   {
       name=namePassed;
   }

   public boolean equals(Object toCompare)
   {
   Student temp= (Student) toCompare;
       return(temp.getSchoolID()==schoolID);
   }
  
   public String toString()
   {
   String toReturn="name: "+name+" id: "+schoolID;
   return (toReturn);
   }

}

HERE IS GIVEN MAIN CLASS

import java.util.Arrays;
class Main {
public static void main(String[] args) {
Student [] myStudents= new Student [10];
myStudents[0]=new Student("shenice",100);
myStudents[1]=new Student("bonnie", 200);
myStudents[2]=new Student("johnny",300);
myStudents[3]=new Student("olivia",400);
myStudents[4]=new Student("ashtrash",500);
myStudents[5]=new Student("jeter",600);
myStudents[6]=new Student("danielle",700);
myStudents[7]=new Student("rodrick",800);
myStudents[8]=new Student("jeter",900);
myStudents[9]=new Student("zoey",1000);

//INSERT YOUR CODE TO SORT HERE AND INSERT YOUR FUNCTIONS BELOW MAIN. DO NOT DELETE THE STUFF UNDER HERE!! i.e. call your function here to sort

if(myStudents[0].getName().equals("ashtrash")&&myStudents[1].getName().equals("bonnie")&&myStudents[2].getName().equals("danielle")&&myStudents[3].getName().equals("jeter")&&myStudents[4].getName().equals("jeter") && myStudents[5].getName().equals("johnny") && myStudents[6].getName().equals("olivia") &&myStudents[7].getName().equals("rodrick") && myStudents[8].getName().equals("shenice") &&myStudents[9].getName().equals("zoey"))
{
System.out.println("Sort Test Passed!");
}
else
{
System.out.println("Sort test failed! Check your code!");
}
}
  
//paste your functions down here!
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java program

import java.util.Arrays;

class Student
{
private int schoolID;
private String name;

public Student(String namePassed,int schoolIDPassed)
{
schoolID= schoolIDPassed;
name= namePassed;
}

public int getSchoolID()
{
return schoolID;
}

public void setSchoolID(int schoolIDPassed)
{
schoolID=schoolIDPassed;
}

public String getName()
{
return name;
}

public void setName(String namePassed)
{
name=namePassed;
}

public boolean equals(Object toCompare)
{
Student temp= (Student) toCompare;
return(temp.getSchoolID()==schoolID);
}

public String toString()
{
String toReturn="name: "+name+" id: "+schoolID;
return (toReturn);
}

};

public class Main {
public static void main(String[] args) {
Student [] myStudents= new Student [10];
myStudents[0]=new Student("shenice",100);
myStudents[1]=new Student("bonnie", 200);
myStudents[2]=new Student("johnny",300);
myStudents[3]=new Student("olivia",400);
myStudents[4]=new Student("ashtrash",500);
myStudents[5]=new Student("jeter",600);
myStudents[6]=new Student("danielle",700);
myStudents[7]=new Student("rodrick",800);
myStudents[8]=new Student("jeter",900);
myStudents[9]=new Student("zoey",1000);

//INSERT YOUR CODE TO SORT HERE AND INSERT YOUR FUNCTIONS BELOW MAIN. DO NOT DELETE THE STUFF UNDER HERE!! i.e. call your function here to sort
Student [] temp= new Student [10];
mergesort(myStudents , temp ,0 , 9);
if(myStudents[0].getName().equals("ashtrash")&&myStudents[1].getName().equals("bonnie")&&myStudents[2].getName().equals("danielle")&&myStudents[3].getName().equals("jeter")&&myStudents[4].getName().equals("jeter") && myStudents[5].getName().equals("johnny") && myStudents[6].getName().equals("olivia") &&myStudents[7].getName().equals("rodrick") && myStudents[8].getName().equals("shenice") &&myStudents[9].getName().equals("zoey"))
{
System.out.println("Sort Test Passed!");
}
else
{
System.out.println("Sort test failed! Check your code!");
}
}
public static void merge(Student a[],Student temp[],int l,int m,int r){
int temp_pos=l,size=r-l+1,left_end=m-1;
while(l<=left_end&&m<=r){
if(a[l].getName().compareTo(a[m].getName())<0)temp[temp_pos++]=a[l++];
else temp[temp_pos++]=a[m++];
}
while(l<=left_end)temp[temp_pos++]=a[l++];
while(m<=r)temp[temp_pos++]=a[m++];
for(int i=0;i<size;i++){
a[r]=temp[r];
r--;
}
}
public static void mergesort(Student a[],Student temp[],int l,int r){
if(l<r){
int mid=l+(r-l)/2;
mergesort(a,temp,l,mid);
mergesort(a,temp,mid+1,r);
merge(a,temp,l,mid+1,r);
}
}

}
//sample output

<terminated> Main (2) [Java Application] C: Program Files\Java\jdk1.8.0 151 bin javaw.exe Sort Test Passed!

Add a comment
Know the answer?
Add Answer to:
PLEASE READ ALL Modify the MergeSort code provided in class (if you find a mistake with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MERGESORT ASSIGNMENT FOLLOW THE FORMAT Modify the MergeSort code provided in class (if you find a...

    MERGESORT ASSIGNMENT FOLLOW THE FORMAT Modify the MergeSort code provided in class (if you find a mistake with it extra credit) (https://repl.it/@ashokbasawapatna/MergeSortExample) to work with Students (No need for generic types, you will only write Main.java). Then use the compareTo method for strings in Java to sort the students in Alphabetical order via MergeSort. 1) Create an array of 10 students by hand. Make sure it's not in alphabetical order. ONLY USE LOWER CASES FOR NAMES (I'll only test with...

  • For the code below write a public static main() method in class Student that: - creates...

    For the code below write a public static main() method in class Student that: - creates an ArrayList<Student> object called students - adds 4 new Student objects to the students list, with some made up names and dates - sort the students list by name and display the sorted collection to System.out. use function getCompByName() - sort the students list by enrollment date and display the sorted collection to System.out. use function getCompByDate() import java.util.Comparator;    import java.util.Date;    public...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • A teacher wants to create a list of students in her class. Using the existing Student...

    A teacher wants to create a list of students in her class. Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created. In the constructor, you will have to add that Student to the ArrayList. Coding below was given to edit and use public class ClassListTester { public static void main(String[] args) { //You don't need to change anything here, but feel free...

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

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • Hi, I am writing Java code and I am having a trouble working it out. The...

    Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT