1

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;
  }
}
1
  • Just a note: if the caller call start() (or stop(), or anything else) and the stopwatch is not in the appropriate state, you should throw an IllegalStateException instead of doing nothing. This will save hours of debugging to the users of your class, including yourself. Commented Jan 19, 2013 at 21:26

2 Answers 2

10

You missed to read the elapsed time again:

public static void main(String[] args)
{
    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();

    System.out.println("Elapsed time: "+ timer.getElapsedTime()+" milliseconds");

    timer.reset();

    input.next();
    timer.start();

    input.next();
    timer.stop();

    System.out.println("Elapsed time: "+ timer.getElapsedTime()+" milliseconds");
}
Sign up to request clarification or add additional context in comments.

1 Comment

So the way I had it, it was storing the getElapsedTime in the variable elapsedTime after the first run, then both iterations were calling that variable. Your way calls the class to get the ElapsedTime again after being reset to 0.
0

The steps you are doing after reset same as the ones you did before reset. This way the result you display immediately before reset is the same as the result after reset plus the repeated steps. Try to print result immediately after reset you will see changes.

1 Comment

Thanks for the input HassanTM, I can't believe I made that mistake, but its all part of learning I guess. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.