/Users/adamklas/Desktop/IB/Net Beans/Dossier for Ms. Boughey/src/dossier/pkgfor/ms/boughey/BestTrial.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 
  7 package dossier.pkgfor.ms.boughey;
  8 
  9 /**
 10  *
 11  * @author adamklas
 12  */
 13 public class BestTrial {
 14     
 15     //Here I define my two arrays (one of 3 distances, and one of 2 times)
 16     //They are established here, so that they can be accesed in the whole class
 17     private Double[] distanceBestScoreArray = new Double[3];
 18     private Double[] timeBestScoreArray = new Double[2];
 19     
 20     //When this method is called, it receives 3 trials (Doubles) and will return the best one (Double)
 21     public double bestTrialDistance(double trial1, double trial2, double trial3){
 22         
 23         //The values for the already create Array are set to be the ones received
 24         distanceBestScoreArray[0] = trial1;
 25         distanceBestScoreArray[1] = trial2;
 26         distanceBestScoreArray[2] = trial3;
 27         
 28         //Here the result is created, set as an error
 29         double bestTrial = -1;
 30         
 31         //Below the Array gets sorted:
 32         //Integer "n" is set to be the Array Length
 33         int n = distanceBestScoreArray.length;
 34         //Boolean sorted is set to be false for the start
 35         boolean sorted = false;
 36         //While the boolean is not "sorted" (false) the loop below keeps repeating
 37         while(!sorted){
 38             //n is decreased by 1, to make the sorting more efficient
 39             n--;
 40             //the Array is considered to be sorted
 41             sorted = true;
 42             //loop through all the distances being focused on, in the Array
 43             for(int i = 0; i < n ; i++){
 44                 //if the 'lower' student has a better then the code below is carried out
 45                 if(distanceBestScoreArray[i].compareTo(distanceBestScoreArray[i +1]) > 0){
 46                     //here I make a temporary distance, and set it to be the 'lower' distance
 47                     double temp = distanceBestScoreArray[i];
 48                     //Here I set the 'lower' student's distance to be the 'higher' student's distance
 49                     distanceBestScoreArray[i] = distanceBestScoreArray[i + 1];
 50                     //Here the 'higher' student's distance is set to be the 'lower' student's distance
 51                     distanceBestScoreArray[i+1] = temp;
 52                     //"sorted" is set to be false, so the loop is carried out again.
 53                     sorted = false;
 54                     
 55                     
 56                 }
 57             }
 58         }
 59         
 60         //the best trial is the last one, as the array was just sorted
 61         bestTrial = distanceBestScoreArray[2];     
 62         //the best trial is returned
 63         return bestTrial;
 64         
 65     }
 66     
 67     public double bestTrialTimeSeconds(double trial1, double trial2){
 68         
 69         //The values for the already create Array are set to be the ones received
 70         timeBestScoreArray[0] = trial1;
 71         timeBestScoreArray[1] = trial2;
 72         
 73         //Here the result is created, set as an error
 74         double bestTrial = -1;
 75         
 76         //Below the Array gets sorted
 77         //refer to lines 31 - 57 for description
 78         int n = timeBestScoreArray.length;
 79         boolean sorted = false;
 80         while(!sorted){
 81             n--;
 82             sorted = true;
 83             for(int i = 0; i < n ; i++){
 84                 //only difference here is that the switch is carried out if
 85                 //the 'lower' students time is lower than the 'higher' student's time
 86                 //because a smaller time (when running) is a better result
 87                 if(timeBestScoreArray[i].compareTo(timeBestScoreArray[i +1]) < 0){
 88                     double temp = timeBestScoreArray[i];
 89                     timeBestScoreArray[i] = timeBestScoreArray[i + 1];
 90                     timeBestScoreArray[i+1] = temp;
 91                     sorted = false;
 92                     
 93                     
 94                 }
 95             }
 96         }
 97         //the best trial is the last one, as the array was just sorted
 98         bestTrial = timeBestScoreArray[1];
 99         
100         //the best trial is returned
101         return bestTrial;
102         
103     }
104     
105     
106     
107 }
108