Commit d7eccde

mokha <mokha@cisco.com>
2019-05-08 23:56:52
add doc and fix error in better than average logic
1 parent f263324
src/Q4/BanffMarathonRunner.java
@@ -1,3 +1,11 @@
+/**
+ * Assignment 1, COMP268 Class: BanffMarathonRunner.java
+ *
+ * @description Represents a marathon runner
+ * @author: mo khan Student ID: 3431709
+ * @date May 8, 2019
+ * @version 1.0
+ */
 package Q4;
 
 import Q1.*;
@@ -8,44 +16,90 @@ public class BanffMarathonRunner extends AddressBook {
   private int time;
   private int years;
 
+  /**
+   * Constructs a BanffMarathonRunner object.
+   *
+   * @param firstName the first name of the runner
+   * @param lastName the last name of the runner
+   * @param time the time it took the runner to complete the marathon
+   * @param years the number of years they participated
+   */
   public BanffMarathonRunner(String firstName, String lastName, int time, int years) {
     super(firstName, "", lastName);
     this.time = time;
     this.years = years;
   }
 
+  /**
+   * Performs a comparison of this runner with another based on the time it took them to complete
+   * the marathon. Because java generics does not allow implementing a generic interface with
+   * different type parameters, this code assumes the AddressBook is an instance of a Runner.
+   *
+   * @param AddressBook the other runner to compare against
+   * @return a negative integer, zero, or a positive integer as the first argument is less than,
+   *     equal to, or greater than the second.
+   */
   public int compareTo(AddressBook other) {
     BanffMarathonRunner runner = (BanffMarathonRunner) other;
     return Integer.compare(this.time, runner.time);
   }
 
+  /**
+   * Returns a string representation of the runner.
+   *
+   * @return The first name + the # of years that they participated.
+   */
   @Override
   public String toString() {
     return super.getFirstName() + " " + this.years;
   }
 
+  /**
+   * Sorts the list of runners based on time, then returns the fastest runner.
+   *
+   * @param runners the list of runners.
+   * @return the fastest runner
+   */
   public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners) {
     Arrays.sort(runners);
     return runners[0];
   }
 
+  /**
+   * Sorts the list of runners based on time, then returns the second fastest runner.
+   *
+   * @param runners the list of runners.
+   * @return the second fastest runner
+   */
   public static BanffMarathonRunner getSecondFastestRunner(BanffMarathonRunner[] runners) {
     Arrays.sort(runners);
     return runners[1];
   }
 
+  /**
+   * Calculates the average time that it took the runners to complete the marathon.
+   *
+   * @param runners the array of runners.
+   * @return the average time taken to complete the marathon
+   */
   public static int getAverageTime(BanffMarathonRunner[] runners) {
     int sum = 0;
     for (BanffMarathonRunner runner : runners) sum += runner.time;
     return sum / runners.length;
   }
 
+  /**
+   * Returns the runners that finished the marathon in above or equal to average time.
+   *
+   * @param the list of runners
+   * @return the list of runners that finished the marathon in above average time.
+   */
   public static String getAboveAverageRunners(BanffMarathonRunner[] runners) {
     int average = getAverageTime(runners);
     ArrayList<String> winners = new ArrayList<String>();
 
     for (BanffMarathonRunner runner : runners)
-      if (runner.time >= average) winners.add(runner.toString());
+      if (runner.time <= average) winners.add(runner.toString());
 
     return String.join(System.lineSeparator(), winners);
   }
src/Q4/BanffMarathonRunnerTest.java
@@ -50,13 +50,14 @@ public class BanffMarathonRunnerTest extends TestCase {
     String expected =
         String.join(
             System.lineSeparator(),
-            "Elena 1",
-            "Suzie 7",
-            "Philip 9",
-            "James 1",
-            "Jane 1",
-            "Emily 4",
-            "Neda 3");
+            "Thomas 2",
+            "Hamilton 5",
+            "Alex 3",
+            "Emma 4",
+            "John 1",
+            "Daniel 4",
+            "Aaron 6",
+            "Kate 8");
     assertEquals(expected, BanffMarathonRunner.getAboveAverageRunners(this.runners));
   }
 }