/ Published in: Java
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public class ReverseGear { /** * @param args */ /* The following array and varible was used here * because some of the classes would not reconise * the array and varible made in the main class */ static int[] lapTime; static int avgLapTime; { int lapNumber0Fix; // intialises int variable for fixing the lap number (used later) lapTime = new int[10]; // sets the array sizes to 10 for(int lapNumber = 0; lapNumber < 10; lapNumber = lapNumber + 1) {lapNumber0Fix = lapNumber + 1; // makes it show the correct lap number is shown to user int enteredLapTime = getValidNumber(60,240); // creates an int variable and send the entered number to the GetValidNumber class to check if number is correct, also sends the values 60 and 240 to that class aswell lapTime[lapNumber] = enteredLapTime; // places the entered lap number into the array } highestLapTime(); // goes to te highestLapTime class and finds the highest time lowestLapTime(); // goes to the lowestLapTime class and finds the lowest time averageTime(); // goes to averageTime class which calculates average time difFromAvg(); // goes to difFromAvg class which calculates the difference from average time } //checks if number entered is a valid number. Wont exit loop till valid numbered is entered public static int getValidNumber(int min, int max) // used min and max in case in the future someone wanted to change the min and max values { int num; ConsoleReader console = new ConsoleReader(); num = console.readInt(); while (num < min || num > max) num = console.readInt(); } return num; } // Finds the highest lap time public static void highestLapTime() { int highestTime = lapTime[0]; // sets the variable to the first number in the array int highestLap = 0; // sets the lowest lap to 0 for (int a = 0; a < 10; a = a + 1) // loops 10 times { if (lapTime[a] >= highestTime) // checks to the see if the amount in the array is higher then the number stored in the highestTime varible { highestTime = lapTime[a]; // sets the highestTime variable to the size of the current array size. highestLap = a + 1; // sets the highestLap variable to the current loop number, adds one to show lap number later } } } // Finds the lowest lap time public static void lowestLapTime() { int lowestTime = lapTime[0]; // sets the variable to the first number in the array int lowestLap = 0; // sets the lowest lap to 0 for (int b = 0; b < 10; b = b + 1) // loops 10 times { if (lapTime[b] <= lowestTime) // checks to the see if the amount in the lapTime array is less then the time stored the lowestTime variable { lowestTime = lapTime[b]; // sets the variable lowestTime to the current lap time lowestLap = b + 1; // sets the lowest lap variable to the current loop number, adds one to show lap number later } } } // calculates average time it took to do a lap public static void averageTime() { int totalLapTime = 0; // sets totalLapTime to 0 so that more numbers can be added for (int avgLap = 0; avgLap < 10; avgLap = avgLap + 1) {totalLapTime = totalLapTime + lapTime[avgLap]; // adds the lapTime to the other lap times } avgLapTime = totalLapTime / 10; // once all the lap times are added together this calculates the average lap time } // calculates the difference for the average time, either above or below public static void difFromAvg() { for (int x = 0; x < 10; x = x + 1) //loops ten times to get the lap times from the array, change x later to something more appropiate. { int loopFix = x + 1; // makes it so that the correct lap is shown to the user if (lapTime[x] > avgLapTime) // checks to see if laptime is greater then average time { } else if (lapTime[x] < avgLapTime) // checks to see if lap time is below lap average { } else if (lapTime[x] == avgLapTime) //checks to see if lap time is equal to average time { } } } }