Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 9, Problem 13E

Write a class LapTimer that can be used to time the laps in a race. The class should have the following private attributes:

  • running — a boolean indication of whether the timer is running
  • startTime — the time when the timer started
  • lapStart — the timer’s value when the current lap started
  • lapTime — the elapsed time for the last lap
  • totalTime — the total time from the start of the race through the last completed lap
  • lapsCompleted — the number of laps completed so far
  • lapsInRace — the number of laps in the race

The class should have the following methods:

  • LapTimer (n) — a constructor for a race having n laps.
  • start — starts the timer. Throws an exception if the race has already started.
  • markLap — marks the end of the current lap and the start of a new lap. Throws an exception if the race is finished.
  • getLapTime — returns the time of the last lap. Throws an exception if the first lap has not yet been completed.
  • getTotalTime — returns the total time from the start of the race through the last completed lap. Throws an exception if the first lap has not yet been completed.
  • getLapsRemaining — returns the number of laps yet to be completed, including the current one.

Express all times in seconds.

To get the current time in milliseconds from some baseline date, invoke

Calendar.getInstance ().getTimeInMillis ()

This invocation returns a primitive value of type long. By taking the difference between the returned values of two invocations at two different times, you will know the elapsed time in milliseconds between the invocations. Note that the class Calendar is in the package java.util.

Blurred answer
Students have asked these similar questions
Using Visual Studio C# Language Note: not a User Input
class Widget:    """A class representing a simple Widget     === Instance Attributes (the attributes of this class and their types) ===    name: the name of this Widget (str)    cost: the cost of this Widget (int); cost >= 0     === Sample Usage (to help you understand how this class would be used) ===    >>> my_widget = Widget('Puzzle', 15)    >>> my_widget.name    'Puzzle'    >>> my_widget.cost    15    >>> my_widget.is_cheap()    False    >>> your_widget = Widget("Rubik's Cube", 6)    >>> your_widget.name    "Rubik's Cube"    >>> your_widget.cost    6    >>> your_widget.is_cheap()    True    """     # Add your methods here if __name__ == '__main__':    import doctest    # Uncomment the line below if you prefer to test your examples with doctest    # doctest.testmod()
A TV Class The attributes of a TV object are the channel, the volume, and a flag (or switch) indicating whether the TV is on or off. The methods perform the following actions: • Turn the TV on or off. • Set the channel to an integer from 0 to 99 inclusive. • Raise or lower the volume by one unit. The volume can range from 0 to 20. • View the value of the volume. • View the channel. • Determine whether the TV is on or off. Write a TV class that implements all relevant functions. A newly created TV object is set to off with the channel set to 2 and the volume initially 10. Include a main(...) method that tests the methods of the TV class.

Chapter 9 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
6 Stages of UI Design; Author: DesignerUp;https://www.youtube.com/watch?v=_6Tl2_eM0DE;License: Standard Youtube License