The problem domain is that of runners doing 100 meter sprints. Each runner has a lane number and runs 100 meters three times. The lane number, name of the runner, and the times are kept as instance variables of Performance class. Later, we are required to operate on an array of Performance objects read from a file.
You are provided a template that you should use as a starting point.
Your first task is to complete the Performance class such that it passes the tests in PerformanceTest. Requirements for all methods to be completed are provided as javadoc comments above the method headers.
The list of methods you need to complete are:
void setTimes(double[] t) (validation scenario explained as javadoc comment).
IMPORTANT NOTE: this is the most critical method of the assignment and if you don’t implement this correctly, none of the other methods will pass the tests, even if their implementations are correct.
parameterized constructor with values for each instance variable passed
double averageTime()
double bestTime()
boolean equals(Performance other) int
compareTo(Performance other)
One method we’d like to highlight is compareTo(Performance other). We’d like to compare two Performance objects p1 and p2. The comparison criteria is the average times. We CANNOT compare the two objects as, if ( p1 > p2 )
Hence we create a methods compareTo(Performance other that returns:
1 if average time of p1 is more than average time of p2, and thus performance p1 is more than performance p2, or p1 > p2 (informally).
-1 if average time of p1 is less than average time of p2, and thus performance p1 is less than performance p2, or p1 > p2 (informally).
0 if average time of p1 and that of p2 are equal, and thus both performances are equal, or p1 == p2 (informally).
We then use this method to compare p1 and p2 as follows,
if ( p1.compareTo( p2 ) == 1)     // to check p1 > p2
// or
if ( p1.compareTo( p2 ) == -1)     // to check p1 < p2
// or
if ( p1.compareTo( p2 ) == 0)     // to check p1 == p2
// or
if ( p1.compareTo( p2 ) != -1)     // to check p1 >= p2
if ( p1.compareTo( p2 ) != 1)     // to check p1 <= p2
if ( p1.compareTo( p2 ) != 0)     // to check p1 != p2
The compareTo method will be useful for completing the second task.
The second task is to complete the PerformanceArrayService class that contains the following class methods operating on array passed to them,
public static int linearSearch(Performance[] performances, String name): Returns the in-dex of the last occurrence of an athlete with the same name as the passed String name, in a case insensitive manner. Thus, if you are searching for “Anne Frank” and the name of the last athlete is “anne frank”, it should be considered a match and the index of the last athlete must be returned.
public static int binarySearch(Performance[] performances, double time): Assuming that the array passed to the method is sorted in ascending order, apply binary search algorithm discussed in weeks 3, 5, and 6. Return -1 if array is null. Further requirements are provided as javadoc comments above the method header.
IMPORTANT NOTE: Notice that we are reading data from sortedData.txt in the test for binary search. Therefore, you do not need to worry about implementing sort to complete binary search.
public static void sort(Performance[] performances): Sort the array passed in ascending order, where the order is based on compareTo method from Performance class. Do nothing if the array is null. You are free to use any algorithm you wish. You cannot use any built-in Java method to finish this task.
Advanced Task
This task constitutes for the top 16% of the assignment 2 marks and thus is fairly advanced. The problem is that instead of each athlete having a two-token name (givenName lastName), they may have names with different number of tokens. Some examples are:
Euclid of Alexandria
M K Gandhi
Bender Bending Rodriguez
Also, it’s possible that different athletes have different number of cracks at the 100 meter sprints. They may make zero or more attempts. You are required to write a file reading client, along the lines of that reads such uneven records. A sample of such a file is provided in advancedData.txt. You will notice that the number of attempts made by each runner vary between 0 and 3 in this sample file. Note that you will need to throw FileNotFoundException from the getPerformances method. The throws declaration has already been added in the template.
Note that we may use different test data. So do not hard-code your program to the test data provided.