Write a class LapTimer that can be used to time the laps in a race. The class sh
ID: 3681235 • Letter: W
Question
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
import java.lang.System;
import java.util.Calendar;
public class LapTimer
{
private boolean running;
private int lapsInRace;
private long totalTime;
private long lapTime;
private int avgSpeed;
private int lapLength;
private long startTime;
private long lapEndTime;
public LapTimer()
{
running = false;
lapsInRace = 0;
lapTime = 0;
totalTime = 0;
lapTime = 0;
avgSpeed = 0;
lapLength = 400;
startTime = getSystemTime();
}
public void startLap()
{
running=true;
if(getSystemTime()> startTime)
{
lapTime=(getSystemTime() - startTime);
}
totalTime = (totalTime + lapTime);
startTime=getSystemTime();
lapsInRace++;
}
public void stop()
{
running = false;
lapTime = (getSystemTime() - startTime);
startTime = 0;
}
public String getStatus()
{
if(running = false)
{
return "Stopped";
}
else
{
return "Timing";
}
}
public int getLapsInRace()
{
return lapsInRace;
}
public String getLapTime()
{
String lapTimeString = Long.toString(lapTime);
return lapTimeString;
}
public String getAverageTime()
{
long averageTime;
if(lapsInRace!=0)
{
averageTime = totalTime/lapsInRace;
}
else
{
averageTime = totalTime;
}
String averageTimeString = Long.toString(averageTime);
return averageTimeString;
}
public String getTotalTime()
{
String totalTimeString = timeToString(totalTime);
return totalTimeString;
}
public String getAverageSpeed()
{
return "unfinished";
}
public int getLapLength()
{
return lapLength;
}
public void setLapLength(int length)
{
lapLength = length;
}
private void finishLap()
{
}
private String timeToString(long time)
{
int totalSeconds;
int hours;
int remainingMinutes;
int minutes;
int seconds;
totalSeconds = (int)totalTime/1000;
hours = totalSeconds/3600;
remainingMinutes=totalSeconds%3600;
minutes=remainingMinutes/60;
seconds = remainingMinutes%60;
String timeString = Long.toString(hours + minutes + seconds);
return timeString;
}
private String twoDigit(long number)
{
return "unfinished";
}
private long getSystemTime()
{
return System.currentTimeMillis();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.