In Java**** Write an application or applet that implements a lap timer using the
ID: 3694080 • Letter: I
Question
In Java****
Write an application or applet that implements a lap timer using the class LapTimer. The new lap timer should have two buttons: Start and Lap, as well as two labels, one for the time of the last lap and the other for the total time of all laps. Your class should create a private instance of LapTimer. Any reasonable small number can be used for the number of laps.
Write a class LapTimer that can be used to time the laps in a race. The class should have the following private attributes:
-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 elasped time for the last lap
-totalTime - thee 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 the following methods:
-LapTimer(n) - a constructor for a race having n laps
-start - starts the timer. Throws an exception if the race has already started
-markLap -marks the end of the current lap and the start of a new lap. Throws an exception if the race is finished.
-getLapTime - returns the time of the last lap. Throws an exception 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 an exception if the first lap has not yet been completed.
-getLapsRemaining - returns the number of laps yet to be completed, including the current one.
Express all times in seconds. To get the currents timein milliseconds from some baseline date, invoke
Calendar.getInstance().getTimeInMillis()
This invocation returns a primitive value of type long. By taking the difference between the returned values of two invocations at two different times, you will know the elapsed time in milliseconds between the invocations.
Explanation / Answer
******* Try this code*******
import java.lang.System;
public class TimingEngine
{
private boolean running;
private int lapCount;
private long totalTime;
private long lastTime;
private int avgSpeed;
private int lapLength;
private long lapStartTime;
private long lapEndTime;
/**
* Create a TimingEngine object. The object will be initialised at 0,
* status is "Stopped", ready to start timing. The default lap length
* is 400m.
*/
public TimingEngine()
{
running = false;
lapCount = 0;
lastTime = 0;
totalTime = 0;
lastTime = 0;
avgSpeed = 0;
lapLength = 400;
lapStartTime = getSystemTime();
}
/**
* Instruct the timer to start timing a lap.
* If we were not timing before, this starts the timer for a new
* run. If we were already timing, this starts a new lap, adding the
* current lap time to the total.
*/
public void startLap()
{
running=true;
if(getSystemTime()>lapStartTime)
{
lastTime=(getSystemTime() - lapStartTime);
}
totalTime = (totalTime + lastTime);
lapStartTime=getSystemTime();
lapCount++;
}
/**
* Stop timing. Add the current lap time to the total, and set
* the timer into idle mode (waiting for a new run).
*/
public void stop()
{
running = false;
lastTime = (getSystemTime() - lapStartTime);
lapStartTime = 0;
}
/**
* Return the current status of the timer. The status is one of the
* two Strings "Timing..." or "Stopped", indicating whether this
* timer is currently running or stopped.
*/
public String getStatus()
{
if(running = false)
{
return "Stopped";
}
else
{
return "Timing";
}
}
/**
* Return the number of laps completed in this run.
*/
public int getLapCount()
{
return lapCount;
}
/**
* Return the time of the last lap completed.
* The result is a string in the format "m:ss:hh", where m is
* the number of minutes, ss the number of seconds, and hh the number
* of hundredths of a second. For example "7:02:43".
*/
public String getLastTime()
{
String lastTimeString = Long.toString(lastTime);
return lastTimeString;
}
/**
* Return the average time for a lap in this run.
* The result is a string in the format "m:ss:hh".
*/
public String getAverageTime()
{
long averageTime;
if(lapCount!=0)
{
averageTime = totalTime/lapCount;
}
else
{
averageTime = totalTime;
}
String averageTimeString = Long.toString(averageTime);
return averageTimeString;
}
/**
* Return the total time of the last or current run.
* The result is a string in the format "m:ss:hh".
*/
public String getTotalTime()
{
String totalTimeString = timeToString(totalTime);
return totalTimeString;
}
/**
* Return the average speed in this run in meters per second.
* The result is a string such as "73 m/s".
*/
public String getAverageSpeed()
{
return "unfinished";
}
/**
* Return the length of a lap.
*/
public int getLapLength()
{
return lapLength;
}
/**
* Set the length of a lap.
*/
public void setLapLength(int length)
{
lapLength = length;
}
/**
* Private method called whenever a lap is finished. Thsi method
* updates the statistics.
*/
private void finishLap()
{
}
/**
* Convert a time interval in milli-seconds into a String in the
* format "m:ss:hh".
*/
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;
}
/**
* Convert a number into a two-digit String representation.
*/
private String twoDigit(long number)
{
//remember there is an example of this in the book!!
return "unfinished";
}
/**
* Return the current time of the system clock (in milli-seconds).
*/
private long getSystemTime()
{
return System.currentTimeMillis();
}
}
Hope this will help to you!
Thank you!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.