stuck on a lap timer
807598Jun 20 2006 — edited Jun 21 2006This is an exercise I have to do and while I don't expect anyone to do the work for me, any suggestions or guidelines will be appreciated more than you know. I think I can handle most of the methods, and have some started and/or finished. I just need to get the startLap going so I can get the rest to work. Code would be great, but I would also appreciate any general guidance on how to make it work. The comments tell what we are to do and there are other classes that work with this. I am just a beginner programmer and am very lost. I am completely blanking on this method. The code thus far is as follows:
/**
* The timer engine for a lap timer application. This engine can keep
* track of a 'run'. A run is a sequence of one or more consecutive laps.
* The timer records single lap times and the time for the total run,
* and it calculate averages and speed.
*
* @author (your name)
* @version (a version number or a date)
*/
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;
/**
* 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;
}
/**
* 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()
{
if(running = false)
{
long startTime = getSystemTime();
startTime = startTime;
}
else
{
startTime = startTime;
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 = Long.toString(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)
{
return "unfinished";
}
/**
* 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();
}
}