Question: Specification: Write a class LapTimer that can be used to time the laps in a race. The class should have the following member variables (all

Specification: Write a class LapTimer that can be used to time the laps in a race. The class should have the following member variables (all private): running - a boolean indication of whether the timer is running. startTime - the time when the timer started lapStart - the timer's value when the current lap started lapTime - the elapsed time for the last lap totalTime - the total time from the start of the race through the last completed lap lapsCompleted - the number of laps completed so far lapsInRace - the number of laps in the race The class should have a single constructor that takes an integer n signifying the number of laps. In addition, the class should contain the following methods: start starts the timer. Throws a TimerStateException if the race has already started. markLap marks the end of the current lap and the start of a new lap. Throws a TimerStateException if the race has finished getLapTime returns the time of the last lap. Throws a TimerStateException if the first lap has not yet been completed. getTotalTime returns the total time from the start of the race through the last completed lap. Throws a TimerStateException if the first lap has not yet been completed. getLapsRemaining returns the number of laps yet to be completed including the current one. You should express all times in milliseconds. To get the current time in milliseconds from some baseline date, you should use the following code: Calendar.getInstance().getTimeInMillis() This returns a primitive of type long, which is equivalent to an integer with a larger MAX_VALUE. You will need to import java.util.Calendar to use this To find the elapsed time, you can take the difference between two values obtained from the above code.

/** * This is the driver for lab 18 so that you can test your code in LapTimer. It tests all methods and provides expected values. * It also checks that you implemented the custom exception class in your start() method correctly. The other methods should throw exceptions similarly * Note that TimeUnit requires Java compiler compliance 1.5 or greater to operate properly * @date 2016-03-24 * @author Christian Merchant * @version 1.1 * @copyright This is provided for student use by the creator Christian Merchant */ import java.util.Random; import java.util.concurrent.TimeUnit; public class Driver { /** * NOTE: Main will take approximately 11 seconds to finish executing and terminate. There is nothing wrong. Be patient... */ public static void main(String[] args) { LapTimer lapTimer = new LapTimer(3); //Start a lap timer with 3 laps try { lapTimer.start(); TimeUnit.SECONDS.sleep(3);//Simulate lap # 1 by waiting for 3 seconds before marking the lap lapTimer.markLap(); System.out.println("First Lap was: " + (lapTimer.getLapTime()/1000) + " seconds. Expected: 3 seconds"); System.out.println("Number of laps remaining: "+ lapTimer.getLapsRemaining()+ " Expected: 2 laps remaining"); TimeUnit.SECONDS.sleep(5); //Simulate lap # 2 by waiting for 5 seconds before marking the lap lapTimer.markLap(); System.out.println("Second Lap was: " + (lapTimer.getLapTime()/1000) + " seconds. Expected: 5 seconds"); System.out.println("Number of laps remaining: "+ lapTimer.getLapsRemaining()+ " Expected: 1 lap remaining"); System.out.println("Total Race Time through 2 completed laps: "+ (lapTimer.getTotalTime()/1000)+ " seconds. Expected: 8 seconds"); TimeUnit.SECONDS.sleep(1);//Simulate lap # 3 by waiting for 1 seconds before marking the lap lapTimer.markLap(); System.out.println("Lap 3 (final) Lap was: " + (lapTimer.getLapTime()/1000) + " seconds. Expected: 1 second"); System.out.println("Number of laps remaining: "+ lapTimer.getLapsRemaining()+ " Expected: 0 laps remaining"); System.out.println("Attempting to start a new race. Expected: Should succeed. No exceptions thrown"); lapTimer.start(); System.out.println("New race start succeeded. Now attempting to start another lap timer. Expected: TimerStateException thrown. Execution Stops..."); TimeUnit.SECONDS.sleep(2); //Sleeping for 2 seconds to allow previous printlns to be sent to the console output before attempting to throw exception lapTimer.start(); } catch (TimerStateException | InterruptedException e) { e.printStackTrace(); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!