This course has already ended.

⌛⌛ Comparison interfaces

The remote material repository (the round6/comparison directory) has supplementary files for this question.

Place your code into a file named Attainment.java in the round6/comparison directory of your local repository.

In this question, you will study different ways of implementing a comparison function for a class. The Attainment class of this question is partly the same as the Attainment class of an earlier exercise question concerning a student register. The class describes a course attainment by storing a course code, student number and grade.

Code the Attainment class so that is has the following properties:

  • Implements the interface Comparable<Attainment>.

    • Implement the compareTo function to compare primarily student numbers and secondarily course codes. Use the compareTo function of the String class in both comparisons.

  • Public constructors and member functions:

    • Constructor Attainment(String courseCode, String studentNumber, int grade) initializes the Attainment object with the parameter values.

    • Member functions getCourseCode(), getStudentNumber() and getGrade() return the course code and student number as strings and the grade as an int, respectively.

    • Member function String toString() that overrides the default version inherited from Object. The function returns a string of form “courseCode studentNumber grade” based on the values stored in the object.

  • Two public class constants:

    • Comparator<Attainment> CODE_STUDENT_CMP

      • An object that implements the interface Comparator<Attainment> and whose member function compare compares two Attainment objects primarily based on their course codes and secondarily based on their student numbers.

    • Comparator<Attainment> CODE_GRADE_CMP

      • An object that implements the interface Comparator<Attainment> and whose member function compare compares two Attainment objects primarily based on their course codes and secondarily based on their grades. The grades are compared in reverse manner (corresponding to descending order).

    • Remember to define both constants with the public, static and final modifiers.

    • You may initialize the constants, for example, by using anonymous class definitions.

Testing

You may test your implementation by using the test program given in the file ComparisonTest.java, the test data files attainments1.txt and attainments2.txt, and the example output given in the files output1.txt and output2.txt. Place these files to the same directory with your code, compile the test program, for example, as javac *.java, and run the first test as java ComparisonTest attainments1.txt and the second test as java ComparisonTest attainments2.txt. The expected outputs of these two tests are given in the files output1.txt and output2.txt.

A+ presents the exercise submission form here.

Posting submission...