⌛⌛ Comparison interfaces

Place your code into a file named Attainment.java in the directory Round6/comparison. Remember to pull student_template_project for material.

In this task you will try different ways of implementing a comparison function for some class. The task uses a similar class Attainment as an earlier exercise task related to a student register. The class describes a course attainment by storing a course code, student number and grade.

You should now implement Attainment so that is has the following properties:

  • Implements the interface Comparable<Attainment>.

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

  • Public constructors / 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 (that are strings) and the grade (an int), respectively.

    • Member function toString() that overrides the default version inherited from Object. The function returns a string of form “courseCode studentNumber grade\n” based on the values stored in the object (a correction done on 9.2: the string ends with a new line character).

  • Public static constant variables (ie. have the modifiers public static final):

    • 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).

You may initialize the member variables CODE_STUDENT_CMP and CODE_GRADE_CMP e.g. 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 e.g. 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...