package squishgame;

/*
 * The stop watch class will allow a program to start and stop a stopwatch as
 * well as return the stopwatch as a string, a total ms's, or the hour, min, and
 * seconds separately.
 *
 * The System.currentTimeMillis() returns the number of milliseconds since
*/


public class Stopwatch {
private long myStarttime;    // in milliseconds
private long myStoptime;
private boolean isTiming;
public Stopwatch() {
    myStarttime = 0;
    myStoptime = 0;
    isTiming = false;
}
public void Start() {
    // starts the clock by getting system time
    myStarttime = System.currentTimeMillis();
    isTiming = true;
}
public void Stop() {
    // stops the clock by getting the system time
    if (isTiming) {
        myStoptime = System.currentTimeMillis();
        isTiming = false;
    }
}
public int getHour(){
    // returns the hours of the current time (if not timing)
}
public int getMinutes(){
    // returns the minutes of the current time (if not timing)
}
public int getSeconds(){
    // returns the seconds of the current time (if not timing)
}
public int getMillis(){
    // returns the millis of the current time (if not timing)
}
public String toString() {
    // returns a string of the time in hours:minutes:seconds.millis
}
}

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

I will give you some help. Then you must be able to write other functions.

public int getHour()
{
	if(isTiming)
		return -999;    //returns that the timer is running
	else
	{
 	       int timeDiff= myStoptime - myStarttime ;
	       return (timeDiff/1000)/(60*60);
	}
}

public int getMinutes()
{
	if(isTiming)
		return -999;
	else
	{
		int timeDiff= myStoptime - myStarttime ;
		return (timeDiff/(1000*60)) % 60;
	}
}

public String toString() 
{
	if(isTiming)
		return -999;
	else
	{
		String output = getHour() + ":" + getMinutes() + ":" + getSeconds() + ":" + getMillis() ;
		return output;
	}
}
This article has been dead for over six months. Start a new discussion instead.