Skip to main content

Debugging lap-timer-buggy

SYSC 2004 Lab 3 Help
Part 4: Debugging lap-timer-buggy Section B
1. Another problem is that clicking the Stop button two or more times in a row causes the display to change repeatedly.
2. Correct this problem.
Part 5: Debugging lap-timer-buggy Section C
1. Finally, several of the values displayed in the window are incorrect.
2. Correct this problem. (Don't forget your testing, including regression testing.) Ensure that you have fixed all the bugs by checking that the "buggy" version now works identically to the "works" version. 
Completed code for lap-timer-buggy below (all changes must be done ONLY in class Timing Engine):
TimingEngine
public class TimingEngine
{
  private boolean running;
  /* The number of laps that the runner has completed. */
  private int lapCount;
  /* The time of the lap just completed, in milliseconds. */
  private long lapTime;
  /* The total time the runner has run so far, in milliseconds. */
  private long totalTime;  
  /* The current time of the system clock when the runner
   * started the current lap, in milliseconds.
   * See getSystemTime(), at the end of this class.
   */
  private long lapStartTime;
  /* The lap length, in metres. */
  private int lapLength;
  /* The average speed, in m/s. */
  private int avgSpeed;   
  public TimingEngine()
  {
    running = false;
    lapCount = 0;
    totalTime = 0;
    lapTime = 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) {
      finishLap();
    }
    else {
      lapCount = 0;
      totalTime = 0;
      lapTime = 0;
      lapStartTime = getSystemTime();
      running = true;
    }
  }
  /**
   * 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()
  {
    finishLap();
    running = false;
  }
  /**
   * 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) {
      return "Timing...";
    }
    else {
      return "Stopped";
    }
  }
  /**
   * 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()
  {
    return timeToString(lapTime);
  }
  /**
   * Return the average time for a lap in this run.
   * The result is a string in the format "m:ss:hh".
   */
  public String getAverageTime()
  {
    if (lapCount == 0) {
      return timeToString(0);
    }
    else {
      long avgTime = totalTime / lapCount;
      return timeToString(avgTime);
    }
  }
  /**
   * Return the total time of the last or current run.
   * The result is a string in the format "m:ss:hh".
   */
  public String getTotalTime()
  {
    return timeToString(totalTime);
  }
  /**
   * Return the average speed in this run in meters per second.
   * The result is a string such as "73 m/s".
   */
  public String getAverageSpeed()
  {
    long totalSeconds = totalTime / 1000;
    if (totalSeconds == 0) {
      return "0 m/s";
    }
    else {
      long avgSpeed = lapLength / totalSeconds;
      return avgSpeed + " m/s";
    }
  }
  /**
   * Return the length of a lap.
   */
  public int getLapLength()
  {
    return lapLength;
  }
  /**
   * Set the length of a lap.
   */
  public void setLapLength(int length)
  {
    if(length > 0)
    {
      lapLength = length;
    }
  }
  /**
   * Private method called whenever a lap is finished. This method
   * updates the statistics.
   */
  private void finishLap()
  {
    lapCount++;
    long lapEndTime = getSystemTime();
    lapTime = lapEndTime - lapStartTime;
    totalTime = totalTime + lapTime;
  }
  /**
   * Convert a time interval in milli-seconds into a String in the
   * format "m:ss:hh".
   */
  private String timeToString(long time)
  {
    long hundredths = (time / 10) % 100;
    long seconds = (time / 1000) % 60;
    long minutes = time / 60000;
    return minutes + ":" + twoDigit(seconds) + ":" + twoDigit(hundredths);
  }
  /**
   * Convert a number into a two-digit String representation.
   */
  private String twoDigit(long number)
  {
    if(number < 10) {
      return "0" + number;
    }
    else {
      return "" + number;
    }
  }
  /**
   * Return the current time of the system clock (in milli-seconds).
   */
  private long getSystemTime()
  {
    return System.currentTimeMillis();
  }
}
LapTimer
public class LapTimer
{
  private TimingEngine engine;
  private UserInterface gui;
  public LapTimer()
  {
    engine = new TimingEngine();
    gui = new UserInterface(engine);
  }
  /**
   * In case the window was closed, show it again.
   */
  public void show()
  {
    gui.setVisible(true);
  }
}

Comments

Popular posts from this blog

Should pit bull terriers be banned in my community

 Discussion Forum: Counterarguments (Should pit bull terriers be banned in my community) You created a question about the topic for your W6 Rough Draft. For this discussion, you will give an answer to that question in the form of a thesis statement. "Dieting Makes People Fat" Main Post: Share your thesis statement with your classmates. Please note: As with last week’s discussion, nothing here is set in stone. Be open to changing everything about your topic, including your position and audience, as you research it and get feedback from your classmates. Topic + Position/Purpose + Supporting Points =Thesis Statement Example: Suppose the question you posed in the Week 5 discussion was something like, “Should pit bull terriers be banned in my community?” After doing some preliminary research, you have concluded that pit bulls, if raised properly, are no more dangerous than other breeds of dogs. Your thesis statement can be something like, “Pitbulls should not be banned

Controversy Associated With Dissociative Disorders

 Assignment: Controversy Associated With Dissociative Disorders The  DSM-5-TR  is a diagnostic tool. It has evolved over the decades, as have the classifications and criteria within its pages. It is used not just for diagnosis, however, but also for billing, access to services, and legal cases. Not all practitioners are in agreement with the content and structure of the  DSM-5-TR , and dissociative disorders are one such area. These disorders can be difficult to distinguish and diagnose. There is also controversy in the field over the legitimacy of certain dissociative disorders, such as dissociative identity disorder, which was formerly called multiple personality disorder. In this Assignment, you will examine the controversy surrounding dissociative disorders. You will also explore clinical, ethical, and legal considerations pertinent to working with patients with these disorders. Photo Credit: Getty Images/Wavebreak Media To Prepare · Review this week’s Learning

CYBER SECURITY and how it can impact today's healthcare system and the future

 Start by reading and following these instructions: Create your Assignment submission and be sure to cite your sources, use APA style as required, and check your spelling. Assignment: Recommendations Document Due Week 6 (100 pts) Main Assignment Recommendations Document The 1250 to 1500-word deliverable for this week is an initial draft of your recommendations. Note that this is a working document and may be modified based on insights gained in module eight and your professor's feedback. This document should contain the following elements: Summary of your problem or opportunity definition A list of possible recommendation alternatives. In this section, you are not yet at the point of suggesting the best set of recommendations but you are trying to be creative and explore all the different ways that the problem or opportunity might best be addressed. The end result here will be a list of alternatives among which you will choose your final recom