Specification: Write a class LapTimer that can be used to time the laps in a rac
ID: 3681241 • Letter: S
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 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:
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.
Explanation / Answer
package assignment;
import java.util.Calendar;
public class LapTimer {
public static void main(String args[]) {
LapTimer timer = new LapTimer(10);
for(int i = 0; i < 10; i++) {
try {
timer.start();
//Wait - for task;
timer.markLap();
} catch (TimerStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private boolean running;// - a boolean indication of whether the timer is running.
private long startTime;// - the time when the timer started
private long lapStart; //- the timer's value when the current lap started
private long lapTime;//- the elapsed time for the last lap
private long totalTime;// - the total time from the start of the race through the last completed lap
private int lapsCompleted;// - the number of laps completed so far
private int lapsInRace;// - the number of laps in the race
// --- start starts the timer. Throws a TimerStateException if the race has already started.
public LapTimer(int lapsInRace) {
this.lapsInRace = lapsInRace;
running = false;
startTime = 0;
lapStart = 0;
lapTime =0;
totalTime = 0;
lapsCompleted = 0;
}
public void start() throws TimerStateException {
if(running)
throw new TimerStateException();
running = true;
this.startTime = Calendar.getInstance().getTimeInMillis();
//TODO: I am assuming first lap is starting a start;
this.lapStart = Calendar.getInstance().getTimeInMillis();
}
// --- markLap marks the end of the current lap and the start of a new lap. Throws a TimerStateException if the race has finished
public void markLap() throws TimerStateException {
if(lapsCompleted == lapsInRace)
throw new TimerStateException();
lapTime = Calendar.getInstance().getTimeInMillis() - - lapStart;
this.totalTime += lapTime;
this.lapsCompleted += 1;
this.lapStart = Calendar.getInstance().getTimeInMillis();
}
// --- getLapTime returns the time of the last lap. Throws a TimerStateException if the first lap has not yet been completed.
public long getLapTime() throws TimerStateException {
if(lapsCompleted == 0)
throw new TimerStateException();
return lapTime;
}
// --- 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.
public long getTotalTime() throws TimerStateException {
if(lapsCompleted == 0)
throw new TimerStateException();
return totalTime;
}
// --- getLapsRemaining returns the number of laps yet to be completed including the current one.
public int getLapsRemaining() {
return lapsInRace - lapsCompleted;
}
}
class TimerStateException extends Exception {
public TimerStateException() {
}
public TimerStateException(String msg) {
super(msg);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.