Return to Snippet

Revision: 35295
at November 5, 2010 05:06 by sinisterdeath


Initial Code
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;
	
	public static void main(String[] args) 
	{
		System.out.println("Welcome to Reverse Gear");
		System.out.println("Reading lap times for 10 laps");
		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
			System.out.print("Please enter the lap time for lap " + lapNumber0Fix + ": ");
			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
		System.out.println("Thank you for using Reverse Gear");
		
	}

	//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)
			{System.out.print("The lap time must be within " + min + "..." + max + ". Please re-input:");
			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
			}	
		}
		System.out.println("The Highest lap time was " + highestTime + " this was on lap " + highestLap);
	}
	
	// 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
			}	
		}
		System.out.println("The Lowest lap time was " + lowestTime + " this was on lap " + lowestLap);
		
	}
	
	// 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
		System.out.println("The average Lap Time was " + avgLapTime);
	}
	
	// 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
			{
				System.out.print("The time for lap ");
				System.out.print(loopFix);
				System.out.print(" was ");
				System.out.print(lapTime[x]);
				System.out.print(" this was ");
				System.out.print(lapTime[x] - avgLapTime); // takes the average lap time away from the lap time
				System.out.print(" seconds above the average Lap time");
				System.out.println("");
			} else if (lapTime[x] < avgLapTime) // checks to see if lap time is below lap average
			{
				System.out.print("The time for lap ");
				System.out.print(loopFix);
				System.out.print(" was ");
				System.out.print(lapTime[x]);
				System.out.print(" this was ");
				System.out.print(avgLapTime - lapTime[x]);
				System.out.print(" seconds below the average Lap time");
				System.out.println("");
			} else if (lapTime[x] == avgLapTime) //checks to see if lap time is equal to average time
			{
				System.out.print("The time for lap ");
				System.out.print(loopFix);
				System.out.print(" was ");
				System.out.print(lapTime[x]);
				System.out.print(" this was ");
				System.out.print(avgLapTime - lapTime[x]);
				System.out.print(" seconds below or above the average Lap time");
				System.out.println("");
			}
				
			
		}
	}
	
	

}

Initial URL


Initial Description


Initial Title
java assignment

Initial Tags
java

Initial Language
Java