Posted By

sinisterdeath on 11/05/10


Tagged

java


Versions (?)

java assignment


 / Published in: Java
 

  1. public class ReverseGear {
  2.  
  3. /**
  4. * @param args
  5. */
  6.  
  7.  
  8. /* The following array and varible was used here
  9. * because some of the classes would not reconise
  10. * the array and varible made in the main class
  11. */
  12. static int[] lapTime;
  13. static int avgLapTime;
  14.  
  15. public static void main(String[] args)
  16. {
  17. System.out.println("Welcome to Reverse Gear");
  18. System.out.println("Reading lap times for 10 laps");
  19. int lapNumber0Fix; // intialises int variable for fixing the lap number (used later)
  20. lapTime = new int[10]; // sets the array sizes to 10
  21. for(int lapNumber = 0; lapNumber < 10; lapNumber = lapNumber + 1)
  22. {lapNumber0Fix = lapNumber + 1; // makes it show the correct lap number is shown to user
  23. System.out.print("Please enter the lap time for lap " + lapNumber0Fix + ": ");
  24. 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
  25. lapTime[lapNumber] = enteredLapTime; // places the entered lap number into the array
  26.  
  27. }
  28. highestLapTime(); // goes to te highestLapTime class and finds the highest time
  29. lowestLapTime(); // goes to the lowestLapTime class and finds the lowest time
  30. averageTime(); // goes to averageTime class which calculates average time
  31. difFromAvg(); // goes to difFromAvg class which calculates the difference from average time
  32. System.out.println("Thank you for using Reverse Gear");
  33.  
  34. }
  35.  
  36. //checks if number entered is a valid number. Wont exit loop till valid numbered is entered
  37. 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
  38. {
  39. int num;
  40. ConsoleReader console = new ConsoleReader();
  41. num = console.readInt();
  42. while (num < min || num > max)
  43. {System.out.print("The lap time must be within " + min + "..." + max + ". Please re-input:");
  44. num = console.readInt();
  45. }
  46. return num;
  47. }
  48.  
  49.  
  50. // Finds the highest lap time
  51. public static void highestLapTime()
  52. {
  53. int highestTime = lapTime[0]; // sets the variable to the first number in the array
  54. int highestLap = 0; // sets the lowest lap to 0
  55. for (int a = 0; a < 10; a = a + 1) // loops 10 times
  56. {
  57. if (lapTime[a] >= highestTime) // checks to the see if the amount in the array is higher then the number stored in the highestTime varible
  58. {
  59. highestTime = lapTime[a]; // sets the highestTime variable to the size of the current array size.
  60. highestLap = a + 1; // sets the highestLap variable to the current loop number, adds one to show lap number later
  61. }
  62. }
  63. System.out.println("The Highest lap time was " + highestTime + " this was on lap " + highestLap);
  64. }
  65.  
  66. // Finds the lowest lap time
  67. public static void lowestLapTime()
  68. {
  69. int lowestTime = lapTime[0]; // sets the variable to the first number in the array
  70. int lowestLap = 0; // sets the lowest lap to 0
  71. for (int b = 0; b < 10; b = b + 1) // loops 10 times
  72. {
  73. if (lapTime[b] <= lowestTime) // checks to the see if the amount in the lapTime array is less then the time stored the lowestTime variable
  74. {
  75. lowestTime = lapTime[b]; // sets the variable lowestTime to the current lap time
  76. lowestLap = b + 1; // sets the lowest lap variable to the current loop number, adds one to show lap number later
  77. }
  78. }
  79. System.out.println("The Lowest lap time was " + lowestTime + " this was on lap " + lowestLap);
  80.  
  81. }
  82.  
  83. // calculates average time it took to do a lap
  84. public static void averageTime()
  85. {
  86. int totalLapTime = 0; // sets totalLapTime to 0 so that more numbers can be added
  87. for (int avgLap = 0; avgLap < 10; avgLap = avgLap + 1)
  88. {totalLapTime = totalLapTime + lapTime[avgLap]; // adds the lapTime to the other lap times
  89. }
  90. avgLapTime = totalLapTime / 10; // once all the lap times are added together this calculates the average lap time
  91. System.out.println("The average Lap Time was " + avgLapTime);
  92. }
  93.  
  94. // calculates the difference for the average time, either above or below
  95. public static void difFromAvg()
  96. {
  97. 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.
  98. {
  99. int loopFix = x + 1; // makes it so that the correct lap is shown to the user
  100. if (lapTime[x] > avgLapTime) // checks to see if laptime is greater then average time
  101. {
  102. System.out.print("The time for lap ");
  103. System.out.print(loopFix);
  104. System.out.print(" was ");
  105. System.out.print(lapTime[x]);
  106. System.out.print(" this was ");
  107. System.out.print(lapTime[x] - avgLapTime); // takes the average lap time away from the lap time
  108. System.out.print(" seconds above the average Lap time");
  109. System.out.println("");
  110. } else if (lapTime[x] < avgLapTime) // checks to see if lap time is below lap average
  111. {
  112. System.out.print("The time for lap ");
  113. System.out.print(loopFix);
  114. System.out.print(" was ");
  115. System.out.print(lapTime[x]);
  116. System.out.print(" this was ");
  117. System.out.print(avgLapTime - lapTime[x]);
  118. System.out.print(" seconds below the average Lap time");
  119. System.out.println("");
  120. } else if (lapTime[x] == avgLapTime) //checks to see if lap time is equal to average time
  121. {
  122. System.out.print("The time for lap ");
  123. System.out.print(loopFix);
  124. System.out.print(" was ");
  125. System.out.print(lapTime[x]);
  126. System.out.print(" this was ");
  127. System.out.print(avgLapTime - lapTime[x]);
  128. System.out.print(" seconds below or above the average Lap time");
  129. System.out.println("");
  130. }
  131.  
  132.  
  133. }
  134. }
  135.  
  136.  
  137.  
  138. }

Report this snippet  

You need to login to post a comment.