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

There is no \"best\" way to organize students. Some would argue this should be d

ID: 3812916 • Letter: T

Question

There is no "best" way to organize students. Some would argue this should be done alphabetically, others by the number of credits they have earned, and others by how colorful/tropical their shirts are. This makes this a perfect problem for Comparators. Download the JAR file which includes a definition of the Student class this problem will use and the JUnit test cases that will test your solutions. Then download and complete the two Comparator classes:

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

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

Here's the Jar file which has the JUnit file, make sure you pass all the tests.

import static org.junit.Assert.assertTrue;

import java.util.Comparator;

import org.junit.Test;

public class ComparingTest {

@Test

public void testComparableSmaller() {

Student ugly = new Student("Ugly Betty", -10, 30);

Student pretty = new Student("Sarah Ford", 1000, 120);

assertTrue(ugly.compareTo(pretty) < 0);

}

@Test

public void testComparableLarger() {

Student attractive = new Student("Nice Betty", 999, 10);

Student attractiver = new Student("Sarah Ford", 1000, 30);

assertTrue(attractiver.compareTo(attractive) > 0);

}

@Test

public void testComparableEqual() {

Student pretty = new Student("Helen O'Troy", 1000, 3.4);

Student pretty2 = new Student("Sarah Ford", 1000, 2.2);

assertTrue(pretty.compareTo(pretty2) == 0);

}

@Test

public void testFewerCreditsFirst() {

Comparator comp = new CreditComparator();

Student frosh = new Student("Bob Jones", -10, 29.5);

Student soph = new Student("Bob Jones", 1000, 30.0);

assertTrue(comp.compare(frosh, soph) < 0);

}

@Test

public void testMoreCreditsFirst() {

Comparator comp = new CreditComparator();

Student frosh = new Student("Bob Jones", -10, 29.5);

Student newfrosh = new Student("Bob Jones", 1000, 29.4);

assertTrue(comp.compare(frosh, newfrosh) > 0);

}

@Test

public void testEqualCredits() {

Comparator comp = new CreditComparator();

Student superSenior = new Student("Matthew Hertz", 1001, 150);

Student superSenior2 = new Student("Carl Alphonce", 1000, 150);

assertTrue(comp.compare(superSenior, superSenior2) == 0);

}

@Test

public void testANameFirst() {

Comparator comp = new NameComparator();

Student aname = new Student("Alice Jones", -10, 29.5);

Student bname = new Student("Bob Jones", 1000, 30.0);

assertTrue(comp.compare(aname, bname) < 0);

}

@Test

public void testBNameFirst() {

Comparator comp = new NameComparator();

Student aname = new Student("Bob Alphonce", -10, 29.5);

Student bname = new Student("Bob Bobson", 1000, 29.4);

assertTrue(comp.compare(bname, aname) > 0);

}

@Test

public void testSameName() {

Comparator comp = new NameComparator();

Student hisName = new Student("John Jacob Jingleheimerschmidt", 1001, 150);

Student myNameToo = new Student("John Jacob Jingleheimerschmidt", 1000, 120);

assertTrue(comp.compare(hisName, myNameToo) == 0);

}

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

And here's the code for Student.

/**

* Class used to represent university students. It records the really important information for each student: their

* name, college credits completed, and how "hot" they are. Since we normally organize people by attractiveness, the

* class is Comparable using that attractiveness measure to order them.

*

*/

public class Student implements Comparable {

/** The student's full name within the system. */

private String name;

/** Number of credits earned by this student */

private double completedCredits;

/** How attractive the person is in a clearly quantifiable metric. */

private int boatsLaunched;

/**

   * Create a new person with the given names and measure of attractiveness.

   *

   * @param fullName First and last name of the student represented by this instance.

   * @param milliHelens Attractiveness of this student -- this is a "real" metric measured by the number of ships sent

   * out to rescue the student.

   * @param creditsEarned Number of college credits that the student has earned (and must be a float, since some courses

   * have fractional credits)

   */

public Student(String fullName, int milliHelens, double creditsEarned) {

boatsLaunched = milliHelens;

name = fullName;

completedCredits = creditsEarned;

}

/**

   * Returns the number of credits this student has passed.

   *

   * @return Student's progress towards their eventual degree.

   */

public double getCredits() {

return completedCredits;

}

// SOLUTION start

/**

   * Compare the current instance with this other student. As with all deep & meaningful comparisons, we look at how "hot" they are.

   *

   * @param p2 Student to which the current one is compared

   * @return {@inheritDoc}

   */

public int compareTo(Student p2) {

return (boatsLaunched - p2.boatsLaunched);

}

// SOLUTION end

/**

   * Gets the student's full name.

   *

   * @return String with the student's name as stored in the system.

   */

public String getName() {

return name;

}

}

Explanation / Answer

import java.util.Comparator;

/**
* Class that can be used to compare two {@link Student}s by the number of credits they have earned. Since the number of
* credits is related to the student's standing, this could be used when trying to sort students and then split them
* into teams.
*
*/
public class CreditComparator implements Comparator {

   @Override
   public int compare(Object o1, Object o2) {
       // TODO Auto-generated method stub
       Student s1=(Student)o1;
       Student s2=(Student)o2;
      
       if(s1.getCredits()==s2.getCredits())
       {
           return 0;
          
       }
       else if(s1.getCredits()>s2.getCredits())
       {
           return 1;
       }
       else
       {
           return -1;
       }
   }

}

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

import java.util.Comparator;

/**
* Class that can be used to alphabetize {@link Student}s. This should use the {@link Student#getName()} to get the two
* students' names. It then uses the fact that {@code String} is already comparable to compare the Strings.
*
*/
public class NameComparator implements Comparator {

   @Override
   public int compare(Object o1, Object o2) {
       Student s1=(Student)o1;
       Student s2=(Student)o2;

      
       return s1.getName().compareTo(s2.getName());
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote