Concept explainers
Add the following member function to the ADT class DigitalTime defined in Displays 12.1 and 12.2:
void DigitalTime::intervalSince(const DigitalTime& aPreviousTime,
int& hoursInInterval, int& minutesInInterval) const
This function computes the time interval between two values of type DigitalTime. One of the values of type DigitalTime is the object that calls the member function intervalSince, and the other value of type DigitalTime is given as the first argument. For example, consider the following code:
DigitalTime current(5, 45), previous(2, 30);
int hours, minutes;
current.intervalSince(previous, hours, minutes);
cout << “The time interval between ” << previous
<< “ and ” << current << endl
<< “is ” « hours « “ hours and ”
<< minutes << “ minutes.\n”;
In a
The time interval between 2:30 and 5:45
is 3 hours and 15 minutes.
Allow the time given by the first argument to be later in the day than the time of the calling object. In this case, the time given as the first argument is assumed to be on the previous day. You should also write a program to test this revised ADT class.
Compute interval between two times
Program Plan:
- The interface file “dtime.h” is same as in the Display 12.1 however user needs to add the member function “void intervalSince(const DigitalTime& aPreviousTime, int& hoursInInterval, int& minutesInInterval)const;”.
- In the implementation file “dtime.cpp”, add the function definition for function “intervalSince”.
- In this function, first initializes the variables “hoursInInterval” and “minutesInInterval” to “0”.
- Declare a variable for compute the difference in “DigitalTime”.
- Compute the hour difference using “hour - aPreviousTime.hour”.
- Compute the minute difference using “minute - aPreviousTime.minute”.
- Check the condition of “hour” and “minutes”.
- Finally store the hours and minutes difference in their respective variables.
- In the application file that is “main.cpp”.
- Include the directive “dtime.h”.
- Define main function.
- Initializes the time for current and previous.
- Declare “int” variables for “hours” and “minutes”.
- Call “intervalSince()” function.
- Display the interval between two times.
The below C++ program is used to compute interval between two values of type “DigitalTime”.
Explanation of Solution
Program:
Modified code for “dtime.h”:
//DISPLAY 12.1 Interface File for DigitalTime
//Header file dtime.h: This is the INTERFACE for the class DigitalTime.
//Values of this type are times of day. The values are input and output in
//24-hour notation, as in 9:30 for 9:30 AM and 14:45 for 2:45 PM.
#include <iostream>
using namespace std;
class DigitalTime
{
public:
friend bool operator ==(const DigitalTime& time1, const DigitalTime& time2);
//Returns true if time1 and time2 represent the same time;
//otherwise, returns false.
DigitalTime(int theHour, int theMinute);
//Precondition: 0 <= theHour <= 23 and 0 <= theMinute <= 59.
//Initializes the time value to theHour and theMinute.
DigitalTime( );
//Initializes the time value to 0:00 (which is midnight).
void advance(int minutesAdded);
//Precondition: The object has a time value.
//Postcondition: The time has been changed to minutesAdded minutes later.
void advance(int hoursAdded, int minutesAdded);
//Precondition: The object has a time value.
//Postcondition: The time value has been advanced
//hoursAdded hours plus minutesAdded minutes.
void intervalSince(const DigitalTime& aPreviousTime, int& hoursInInterval, int& minutesInInterval)const;
//Precondition: The object has a time value.
//Precondition: The aPreviousTime object has a time value
//Postcondition: The hoursInInterval represents the number of hours that have passed and the minutesInInterval represents the number of minutes that have passed
friend istream& operator >>(istream& ins, DigitalTime& theObject);
//Overloads the >> operator for input values of type DigitalTime.
//Precondition: If ins is a file input stream, then ins has already been
//connected to a file.
friend ostream& operator <<(ostream& outs, const DigitalTime& theObject);
//Overloads the << operator for output values of type DigitalTime.
//Precondition: If outs is a file output stream, then outs has already been
//connected to a file.
private:
int hour;
int minute;
};
Modified code for “dtime.cpp”:
//DISPLAY 12.2 Implementation File for DigitalTime
//Implementation file dtime.cpp (Your system may require some
//suffix other than .cpp): This is the IMPLEMENTATION of the ADT DigitalTime.
//The interface for the class DigitalTime is in the header file dtime.h.
#include <iostream>
#include <cctype>
#include <cstdlib>
#include "dtime.h"
using namespace std;
//These FUNCTION DECLARATIONS are for use in the definition of
//the overloaded input operator >>:
void readHour(istream& ins, int& theHour);
//Precondition: Next input in the stream ins is a time in 24-hour notation,
//like 9:45 or 14:45.
//Postcondition: theHour has been set to the hour part of the time.
//The colon has been discarded and the next input to be read is the minute.
void readMinute(istream& ins, int& theMinute);
//Reads the minute from the stream ins after readHour has read the hour.
int digitToInt(char c);
//Precondition: c is one of the digits '0' through '9'.
//Returns the integer for the digit; for example, digitToInt('3') returns 3.
bool operator ==(const DigitalTime& time1, const DigitalTime& time2)
{
return (time1.hour == time2.hour && time1.minute == time2.minute);
}
//Uses iostream and cstdlib:
DigitalTime::DigitalTime(int theHour, int theMinute)
{
if (theHour < 0 || theHour > 23 || theMinute < 0 || theMinute > 59)
{
cout << "Illegal argument to DigitalTime constructor.";
exit(1);
}
else
{
hour = theHour;
minute = theMinute;
}
}
DigitalTime::DigitalTime( ) : hour(0), minute(0)
{
//Body intentionally empty.
}
void DigitalTime::advance(int minutesAdded)
{
int grossMinutes = minute + minutesAdded;
minute = grossMinutes%60;
int hourAdjustment = grossMinutes/60;
hour = (hour + hourAdjustment)%24;
}
void DigitalTime::advance(int hoursAdded, int minutesAdded)
{
hour = (hour + hoursAdded)%24;
advance(minutesAdded);
}
//Uses iostream:
ostream& operator <<(ostream& outs, const DigitalTime& theObject)
{
outs << theObject.hour << ':';
if (theObject.minute < 10)
outs << '0';
outs << theObject.minute;
return outs;
}
//Uses iostream:
istream& operator >>(istream& ins, DigitalTime& theObject)
{
readHour(ins, theObject.hour);
readMinute(ins, theObject.minute);
return ins;
}
int digitToInt(char c)
{
return ( static_cast<int>(c) - static_cast<int>('0') );
}
//Uses iostream, cctype, and cstdlib:
void readMinute(istream& ins, int& theMinute)
{
char c1, c2;
ins >> c1 >> c2;
if (!(isdigit(c1) && isdigit(c2)))
{
cout << "Error illegal input to readMinute\n";
exit(1);
}
theMinute = digitToInt(c1)*10 + digitToInt(c2);
if (theMinute < 0 || theMinute > 59)
{
cout << "Error illegal input to readMinute\n";
exit(1);
}
}
//Uses iostream, cctype, and cstdlib:
void readHour(istream& ins, int& theHour)
{
char c1, c2;
ins >> c1 >> c2;
if ( !( isdigit(c1) && (isdigit(c2) || c2 == ':' ) ) )
{
cout << "Error illegal input to readHour\n";
exit(1);
}
if (isdigit(c1) && c2 == ':')
{
theHour = digitToInt(c1);
}
else //(isdigit(c1) && isdigit(c2))
{
theHour = digitToInt(c1)*10 + digitToInt(c2);
ins >> c2;//discard ':'
if (c2 != ':')
{
cout << "Error illegal input to readHour\n";
exit(1);
}
}
if ( theHour < 0 || theHour > 23 )
{
cout << "Error illegal input to readHour\n";
exit(1);
}
}
/*Function definition compute interval between the two values of type DigitalTime */
void DigitalTime::intervalSince(const DigitalTime& aPreviousTime, int& hoursInInterval, int& minutesInInterval) const
{
/* Initializes the value of hours in interval to "0" */
hoursInInterval = 0;
/* Initializes the value of minutes in interval to "0" */
minutesInInterval = 0;
/* Declare the variable for time difference */
DigitalTime diff;
/* Compute hour difference */
diff.hour = hour - aPreviousTime.hour;
/* Compute minutes difference */
diff.minute = minute - aPreviousTime.minute;
/* Check condition */
if (hour < aPreviousTime.hour || hour == aPreviousTime.hour && minute < aPreviousTime.minute)
{
//Display given message
cout << "Preceding time is in the preceding day" << endl;
diff.hour = 24 + (hour - aPreviousTime.hour);
}
/* Check the condition hour */
if (diff.minute < 0)
{
diff.hour--;
diff.minute = diff.minute + 60;
}
/* Store hours and minutes interval in respective variable */
hoursInInterval = diff.hour;
minutesInInterval = diff.minute;
return;
}
Modified “main.cpp”:
//Header file
#include <iostream>
//Header file for "dtime.h"
#include "dtime.h"
//For standard input and output
using namespace std;
//Main function
int main( )
{
//Declare time
DigitalTime current(5, 45), previous(2, 30);
//Declare "int" variables
int hours, minutes;
/* Call intervalSince() function */
current.intervalSince(previous, hours, minutes);
/* Display given time interval */
cout << "The time interval between " << previous << " and " << current << endl << "is " << hours << " hours and " << minutes << " minutes.\n";
return 0;
}
The time interval between 2:30 and 5:45
is 3 hours and 15 minutes.
Want to see more full solutions like this?
Chapter 12 Solutions
Problem Solving with C++ (10th Edition)
Additional Engineering Textbook Solutions
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
SURVEY OF OPERATING SYSTEMS
Starting Out With Visual Basic (8th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Mechanics of Materials (10th Edition)
- Modern life has been impacted immensely by computers. Computers have penetrated every aspect of oursociety, either for better or for worse. From supermarket scanners calculating our shopping transactionswhile keeping store inventory; robots that handle highly specialized tasks or even simple human tasks,computers do much more than just computing. But where did all this technology come from and whereis it heading? Does the future look promising or should we worry about computers taking over theworld? Or are they just a necessary evil? Provide three references with your answer.arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Describe in full, consider the following:• Taking the database offline is not allowed since people are connected to it.• Personal data might be bridged and not secured. Provide three refernces with you answerarrow_forwardYou are called by your supervisor to go and check a potential data bridge problem. What are the stepsyou will follow in order to check the database and fix any problems with it? Have in mind that youSHOULD normalize it as well. Describe in full, consider the following:• Taking the database offline is not allowed since people are connected to it.• Personal data might be bridged and not secured. Provide three refernces with you answerarrow_forward
- (A) A cellular system has 12 microcells with 10 channels per cell. The microcells are split into 3 microcells, and each microcell is further split into 4 picocells. Determine the number of channels available in system after splitting into picocelles.arrow_forwardQuestion 8 (10 points) Produce a relational schema diagram that corresponds to the following ER diagram for a Vacation Property Rentals database. Your relational schema diagram should include primary & foreign keys. Upload your relational schema diagram as a PDF document. Don't forget that the relation schemas for "Beach Property" and "Mountain Property" should each have primary keys. FYI: "d" in this notation denotes a subclass. Figure 2: ER Diagram for Question 8 id first RENTER name middle last address phone email 1 signs N id begin date RENTAL AGREEMENT end date amount N street address books city id 1 state address num. rooms PROPERTY zip code base rate type propertyType blocks to beach activity "B" "M" BEACH PROPERTY MOUNTAIN PROPERTYarrow_forwardNotes: 1) Answer All Question, 2) 25 points for each question QI Figurel shows the creation of the Frequency Reuse Pattern Using the Cluster Size K: (A) illustrates how i and j can be used to locate a co-channel cell. huster 3 Cluster Cluster 2 X=7(i=2,j1)arrow_forward
- You are called by your supervisor to go and check a potential data bridge problem. What are the stepsyou will follow in order to check the database and fix any problems with it? Have in mind that youSHOULD normalize it as well. Describe in full, consider the following taking the database offline is not allowed since people are connected to it and how personal data might be bridged and not secured.Provide three references with you answer.arrow_forwardYou are called by your supervisor to go and check a potential data bridge problem. What are the stepsyou will follow in order to check the database and fix any problems with it? Have in mind that youSHOULD normalize it as well. Describe in full, consider the following:• Taking the database offline is not allowed since people are connected to it.• Personal data might be bridged and not secured. Provide three refernces with you answerarrow_forwardYou are called by your supervisor to go and check a potential data bridge problem. What are the stepsyou will follow in order to check the database and fix any problems with it? Have in mind that youSHOULD normalize it as well. Describe in full, consider the following:• Taking the database offline is not allowed since people are connected to it.• Personal data might be bridged and not secured. Provide three refernces with you answer from websitesarrow_forward
- Modern life has been impacted immensely by computers. Computers have penetrated every aspect of oursociety, either for better or for worse. From supermarket scanners calculating our shopping transactionswhile keeping store inventory; robots that handle highly specialized tasks or even simple human tasks,computers do much more than just computing. But where did all this technology come from and whereis it heading? Does the future look promising or should we worry about computers taking over theworld? Or are they just a necessary evil? Provide three references with your answer.arrow_forwardObjective: 1. Implement a custom Vector class in C++ that manages dynamic memory efficiently. 2. Demonstrate an understanding of the Big Five by managing deep copies, move semantics, and resource cleanup. 3. Explore the performance trade-offs between heap and stack allocation. Task Description: Part 1: Custom Vector Implementation 1. Create a Vector class that manages a dynamically allocated array. 。 Member Variables: ° T✶ data; // Dynamically allocated array for storage. std::size_t size; // Number of elements currently in the vector. std::size_t capacity; // Maximum number of elements before reallocation is required. 2. Implement the following core member functions: Default Constructor: Initialize an empty vector with no allocated storage. 。 Destructor: Free any dynamically allocated memory. 。 Copy Constructor: Perform a deep copy of the data array. 。 Copy Assignment Operator: Free existing resources and perform a deep copy. Move Constructor: Transfer ownership of the data array…arrow_forward2.68♦♦ Write code for a function with the following prototype: * Mask with least signficant n bits set to 1 * Examples: n = 6 -> 0x3F, n = 17-> 0x1FFFF * Assume 1 <= n <= w int lower_one_mask (int n); Your function should follow the bit-level integer coding rules Be careful of the case n = W.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning