Timer Class Concepts tested: Classes 1) Write a class called Timer that models a stopwatch with start, stop, and reset buttons. It has the following members: a) -int startTime; // variable tracking when the timer was started (unused when timer is not running). In number of seconds since 1970 b) -int timeOnClock; // variable tracking the time on the clock since last reset in seconds c) -bool isRunning; //variable tracking wether the clock is running or stopped d) +Timer(); //constructor, initializes all to zero/false e) +Timer(bool startNow); // constructur that starts clock automatically if startNow is true f) +int getTime(); // returns time on clock in seconds. If clock is running, this must also include the time since it was last started g) +void addTimeToClock(int seconds); // allows manually adding time to the clock h) +void reset(); // resets time on clock to zero i) +void start(); // starts clock j) +void stop(); // stops clock k) +void printTime(); // prints the time (via cout) EXACTLY in the format 0h:0m:0s 2) Although you would normally create separate cpp and header files for your code, you must define and fully implement the class in the main file due to the constraints of the lab environment 3) Class name and class member names MUST BE EXACTLY THE SAME as above Example: --If the timer is started and stopped after 2 seconds, a call getTime should return int integer 2. A call to printTime should print "0h:0m:2s". --If the same timer is then started and stopped after an hour, getTime() should return 3602 and printTime() should print "1h:0m:2s"; --If addTimeToClock(120) is then run, printTime() should print "1h:2m:2s"; --If timer is reset, getTime() should return 0, and printTime() should print "0h:0m:0s". code given: #include // OTHER INCLUDES??? using namespace std; // YOUR CODE HERE #include "testcode.h" // For automatic grading purposes DO NOT REMOVE (but you can comment out temporarily for testing) int main() { testTimerClass(); // Test code for automatic grading. DO NOT REMOVE (but you can temporarily comment out for your own testing). Remove any test code you may have added prior to final submission. } Additional Files: These files will automatically be compiled with your code. You cannot modify them. testcode.h using namespace std; void testGetTime(){ Timer timer(true); system("sleep 2"); cout << timer.getTime() << std::endl; } void testStartAndStop(){ Timer timer(true); system("sleep 1"); timer.printTime(); timer.stop(); system("sleep 1"); timer.printTime(); timer.start(); system("sleep 1"); timer.printTime(); timer.stop(); system("sleep 1"); timer.printTime(); } void testAddTimeToClock(){ Timer timer; timer.addTimeToClock(60); cout << timer.getTime() << std::endl; } void testReset(){ Timer timer; timer.addTimeToClock(60); timer.printTime(); timer.reset(); timer.printTime(); } void testHoursMinutes(){ Timer timer; timer.addTimeToClock(90); timer.printTime(); timer.addTimeToClock(3600 * 2); timer.printTime(); } void testTimerClass(){ int testNumber; cin >> testNumber; switch (testNumber){ case 1: testStartAndStop(); break; case 2: testAddTimeToClock(); break; case 3: testReset(); break; case 4: testHoursMinutes(); break; } }

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter10: Introduction To Inheritance
Section: Chapter Questions
Problem 13RQ
icon
Related questions
Question

Timer Class

Concepts tested: Classes

1) Write a class called Timer that models a stopwatch with start, stop, and reset buttons.  It has the following members:

    a) -int startTime; // variable tracking when the timer was started (unused when timer is not running). In number of seconds since 1970

    b) -int timeOnClock; // variable tracking the time on the clock since last reset in seconds

    c) -bool isRunning; //variable tracking wether the clock is running or stopped

    d) +Timer(); //constructor, initializes all to zero/false

    e) +Timer(bool startNow); // constructur that starts clock automatically if startNow is true

    f) +int getTime(); // returns time on clock in seconds. If clock is running, this must also include the time since it was last started

    g) +void addTimeToClock(int seconds); // allows manually adding time to the clock

    h) +void reset(); // resets time on clock to zero

    i) +void start(); // starts clock

    j) +void stop(); // stops clock

    k) +void printTime(); // prints the time (via cout) EXACTLY in the format 0h:0m:0s<newline>

2) Although you would normally create separate cpp and header files for your code, you must define and fully implement the class in the main file due to the constraints of the lab environment

3) Class name and class member names MUST BE EXACTLY THE SAME as above

 

Example:

  --If the timer is started and stopped after 2 seconds, a call getTime should return int integer 2.  A call to printTime should print "0h:0m:2s".

  --If the same timer is then started and stopped after an hour, getTime() should return 3602 and printTime() should print "1h:0m:2s";

  --If addTimeToClock(120) is then run, printTime() should print "1h:2m:2s";

  --If timer is reset, getTime() should return 0, and printTime() should print "0h:0m:0s".

 

 

code given:
 

#include <iostream>
// OTHER INCLUDES???

using namespace std;

// YOUR CODE HERE

#include "testcode.h" // For automatic grading purposes DO NOT REMOVE (but you can comment out temporarily for testing)

int main() {
  
  testTimerClass(); // Test code for automatic grading. DO NOT REMOVE (but you can temporarily comment out for your own testing). Remove any test code you may have added prior to final submission.
}

 

Additional Files:

These files will automatically be compiled with your code. You cannot modify them.

testcode.h

using namespace std;

void testGetTime(){
  Timer timer(true);
  system("sleep 2");
  cout << timer.getTime() << std::endl;
}
void testStartAndStop(){
  Timer timer(true);
  system("sleep 1");
  timer.printTime();
  timer.stop();
  system("sleep 1");
  timer.printTime();
  timer.start();
  system("sleep 1");
  timer.printTime();
  timer.stop();
  system("sleep 1");
  timer.printTime();
}

void testAddTimeToClock(){
  Timer timer;
  timer.addTimeToClock(60);
  cout << timer.getTime() << std::endl;
}

void testReset(){
  Timer timer;
  timer.addTimeToClock(60);
  timer.printTime();
  timer.reset();
  timer.printTime();
}

void testHoursMinutes(){
  Timer timer;
  timer.addTimeToClock(90);
  timer.printTime();
  timer.addTimeToClock(3600 * 2);
  timer.printTime();
}

void testTimerClass(){
  int testNumber;
  cin >> testNumber;
  switch (testNumber){
    case 1: testStartAndStop(); break;
    case 2: testAddTimeToClock(); break;
    case 3: testReset(); break;
    case 4: testHoursMinutes(); break;
  }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Class
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
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning