//============================================================ // SOLUTION TO HOMEWORK 2! //============================================================ // HOMEWORK 2 //------------------------------------------------------------ // DUE DATE: Monday, 3/6/2006 //============================================================ // // Below is the outline for the declaration and implementation // of a class Time. Note that this is very similar to the // example of the class Date we discussed during class. // // Complete the missing parts by: // // - Adding necessary datafields // // - Implementing the constructors // // - Implementing the function "bool IsBefore(Time t)" // that receives one Time object t and compares if // the object for which the method was called happens // to occur before the time t. // // - Implementing the member function Read() // that asks the user to enter all data // for the object and stores it in the data fields // // - EXTRA CREDIT // write a member function "int SecondsLaterThan(Time t)" // that computes how many seconds the current Time object // is than t - the time object that was passed as a parameter #include #include using namespace std; //------------------------------------------------------------ // class declaration for class Time // class Time { private: // ADD DATAFIELDS double hours; // store hours in 24-hour time format - to avoid problems // with comparing am and pm times double minutes; double seconds; public: //Constructors Time(); Time(double h, double m, double s, string am_pm); //Data Access Functions //Task Functions void Read(); bool IsBefore(Time t); }; // - - - - - - - - - - - - - - - - - - - - // class implementation for class Time // // WRITE THE IMPLENTATIONS FOR ALL THE MEMBER FUNCTIONS // OF THE CLASS Time HERE //Constructors Time::Time() // Default Constructor { seconds=0; minutes=0; hours=1; } Time::Time(double h, double m, double s, string am_pm) { seconds=s; minutes=m; hours=h; if (am_pm == "PM" or am_pm == "pm") hours = hours + 12; } // TASK FUNCTIONS void Time::Read() { cout << "Please enter hour (1-12): " << "\n"; cin >> hours; while (hours < 1 or hours > 12) { cout << "Invalid value!!! "; cout << "Please enter hour (1-12): " << "\n"; cin >> hours; } string am_pm; cout << "Please enter AM or PM: " << "\n"; cin >> am_pm; while (am_pm != "AM" and am_pm != "PM") { cout << "Invalid value!!! "; cout << "Please enter AM or PM: " << "\n"; cin >> am_pm; } if (am_pm == "PM") hours = hours+12; cout << "Please enter minutes (0-59): " << "\n"; cin >> minutes; while (minutes < 0 or minutes > 59) { cout << "Invalid value!!! "; cout << "Please enter minutes (1-12): " << "\n"; cin >> minutes; } cout << "Please enter seconds (0-59): " << "\n"; cin >> seconds; while (seconds < 0 or seconds > 59) { cout << "Invalid value!!! "; cout << "Please enter seconds (1-12): " << "\n"; cin >> seconds; } } bool Time::IsBefore(Time t) { // NOTE: by storing PM times as hours 13-24, no need to compare // am and pm - just comparing the hours is sufficient. if (hours < t.hours) return true; if (hours > t.hours) return false; if (minutes < t.minutes) return true; if (minutes > t.minutes) return false; if (seconds < t.seconds) return true; else return false; }; //============================================================ // main program // int main() { Time t1(8,15,6,"PM"); Time t2; t2.Read(); // let user enter the time for t2 if (t2.IsBefore(t1)) { cout << "You are early!\n"; } else if (t1.IsBefore(t2)) { cout << "You are late!\n"; } else { cout << "You are just on time!\n"; } }