⌛⌛ Comparison interfaces

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

The exercise is returned as a Maven project. Place the pom.xml file in the round6/comparison directory of your local repository and create to this directory the src/main/java subdirectory. Create class file Attainment.java and attach it to the fi.tuni.prog3.comparison package. Your files should be in the round6/comparison/src/main/java/fi/tuni/prog3/comparison directory.

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:

  • Package declaration package fi.tuni.prog3.comparison;

  • Implements the interface Comparable<Attainment>. Use the Comparable interface provided by the Java class library.

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

The automatic tests, and the ones given below, assume that you make the following definitions in your pom.xml project file:

  • The value of artifactId is comparison.

  • The value of version is 1.0.

  • The values of the maven.compiler.source and maven.compiler.target elements are 17 or lower. The grader uses Java 17, so any newer versions won’t work.

  • A Onejar plugin definition where the value of mainClass is ComparisonTest which is the name of the given test class (see below).

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.

Set ComparisonTest.java into the root of the src/main/java subdirectory of your Maven project, and the other files into the root directory of your Maven project, that is, where the pom.xml is. Note that ComparisonTest.java does not include a package definition and therefore is not placed into a deeper subdirectory.

After this you can compile the program with mvn package and run the first test as java -jar target/comparison-1.0.one-jar.jar attainments1.txt and the second test as java -jar target/comparison-1.0.one-jar.jar attainments2.txt in the root directory of the project. The expected outputs of these two tests are given in the files output1.txt and output2.txt.

A+ presents the exercise submission form here.