Reimplement the digital clock from the preceding exercise, representing the time as the minutes from midnight. This makes the pulse member function very easy, but you have to work harder to recover the hours and minutes. For example, 100 minutes after midnight is 1 hour and 40 minutes. C++

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Reimplement the digital clock from the preceding exercise, representing the time as the minutes from midnight. This makes the pulse member function very easy, but you have to work harder to recover the hours and minutes. For example, 100 minutes after midnight is 1 hour and 40 minutes.

C++

void Clock::pulse()
{
get_minutes++;
if (get_minutes == 60)
{
get_minutes = 0;
get_hours++;
if (get_hours == 24)
{
get _hours = 0;
}
}
39
40
41
42
43
44
45
46
47
48
49
50
51
int Clock: :get_hours() const
{
return get_hours;
}
int Clock: :get_minutes() const
52
53
54
55
56
57
{
return get_minutes;
}
58
59
60
61
int main()
62
{
Clock my_clock; my_clock.reset();
63
64
for (int i = 0; i < 100; i++) { my_clock.pulse(); }
cout « my_clock.get_hours() « endl;
cout « "Expected: 1" « endl;
cout « my_clock.get_minutes() « endl;
cout « "Expected: 40" « endl;
65
66
67
68
69
70
for (int i = 0; i < 78; i++) { my_clock.pulse(); }
cout « my_clock.get_hours() « endl;
cout « "Expected: 2" « endl;
cout « my_clock.get_minutes() « endl;
cout « "Expected: 50" « endl;
71
72
<<
73
74
75
76
for (int i = 0; i < 1270; i++) { my_clock.pulse(); }
cout « my_clock.get_hours () « endl;
cout « "Expected: 0" « endl;
cout « my_clock.get_minutes() « endl;
cout « "Expected: 0" « endl;
77
78
79
80
81
82
83
return e;
84 }
Transcribed Image Text:void Clock::pulse() { get_minutes++; if (get_minutes == 60) { get_minutes = 0; get_hours++; if (get_hours == 24) { get _hours = 0; } } 39 40 41 42 43 44 45 46 47 48 49 50 51 int Clock: :get_hours() const { return get_hours; } int Clock: :get_minutes() const 52 53 54 55 56 57 { return get_minutes; } 58 59 60 61 int main() 62 { Clock my_clock; my_clock.reset(); 63 64 for (int i = 0; i < 100; i++) { my_clock.pulse(); } cout « my_clock.get_hours() « endl; cout « "Expected: 1" « endl; cout « my_clock.get_minutes() « endl; cout « "Expected: 40" « endl; 65 66 67 68 69 70 for (int i = 0; i < 78; i++) { my_clock.pulse(); } cout « my_clock.get_hours() « endl; cout « "Expected: 2" « endl; cout « my_clock.get_minutes() « endl; cout « "Expected: 50" « endl; 71 72 << 73 74 75 76 for (int i = 0; i < 1270; i++) { my_clock.pulse(); } cout « my_clock.get_hours () « endl; cout « "Expected: 0" « endl; cout « my_clock.get_minutes() « endl; cout « "Expected: 0" « endl; 77 78 79 80 81 82 83 return e; 84 }
Tester.cpp
1
#include <iostream>
2 using namespace std;
3 /**
A simulated digital clock.
*/
4
6.
class Clock
7
{
public:
/**
10
Sets the clock to 00:00.
11
*/
12
void reset ();
13
14
/**
15
Advances this clock to the next minute.
16
*/
17
void pulse();
18
19
/**
Gets the hours of this clock.
ereturn the hours (between e and 23)
*/
int get hours() const;
20
21
22
23
24
25
/**
26
Gets the minutes of this clock.
27
@return the minutes (between e and 59)
28
29
int get_minutes() const;
private:
int minutes_from midnight;
30
31
32 };
33
void Clock::reset ()
{
get_hours - 0;
get_minutes = 0;
34
35
36
37
38
Transcribed Image Text:Tester.cpp 1 #include <iostream> 2 using namespace std; 3 /** A simulated digital clock. */ 4 6. class Clock 7 { public: /** 10 Sets the clock to 00:00. 11 */ 12 void reset (); 13 14 /** 15 Advances this clock to the next minute. 16 */ 17 void pulse(); 18 19 /** Gets the hours of this clock. ereturn the hours (between e and 23) */ int get hours() const; 20 21 22 23 24 25 /** 26 Gets the minutes of this clock. 27 @return the minutes (between e and 59) 28 29 int get_minutes() const; private: int minutes_from midnight; 30 31 32 }; 33 void Clock::reset () { get_hours - 0; get_minutes = 0; 34 35 36 37 38
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Random Class and its operations
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education