Ok I'm very new to Java and I'm trying to complete a homework assignment, but my object isn't working in my class. (Forgive me if I'm using wrong terminology, noobie here)
So I'm making a basic stopwatch program that records 2 laps. I have gotten to the point of creating the 1st lap and it works great. Then when I call the timer.reset() object to reset the stopwatch it displays the same time as the first lap. Why isn't my timer.reset() not working?
public class StopWatch
{
/**
* This method
*
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Start stopwatch [press s]: ");
System.out.println("Stop stopwatch [press q]: ");
Timer timer = new Timer();
input.next();
timer.start();
input.next();
timer.stop();
int elapsedTime = timer.getElapsedTime();
System.out.print("Elapsed time: ");
System.out.print(elapsedTime);
System.out.println(" milliseconds");
timer.reset();
input.next();
timer.start();
input.next();
timer.stop();
System.out.print("Elapsed time: ");
System.out.print(elapsedTime);
System.out.println(" milliseconds");
}
}
Here's the timer.java class im using in my program.
public class Timer {
private long start;
private long stop;
private int elapsedTime;
public Timer() {
}
public void start() {
if (this.start == 0 && this.stop == 0)
this.start = System.currentTimeMillis();
}
public void stop() {
if (this.start > 0 && this.stop == 0) {
this.stop = System.currentTimeMillis();
this.elapsedTime = (int) (stop - start);
}
}
public int getElapsedTime() {
return this.elapsedTime;
}
public void reset() {
this.start = 0;
this.stop = 0;
this.elapsedTime = 0;
}
}