Create a class time24, each object is a value represented the time of day in the form hours, minutes and seconds.Provide a constructor that enables an object of this class to be initialized when it is instantiated. The constructor should contain default values in case no initializes are provided.Provide Public member functions for each of the following operations: set the time, print the time, increment the time by one second,increment the time by one hour,compare two times for equality, determine if one time is “less than” (comes before) another time, and final print the time in format 12 hours.Include any additional operations that you think would be useful for your class.Design,implement, and test your class. Using C++

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Create a class time24, each object is a value represented the time of day in the form hours, minutes and seconds.Provide a constructor that enables an object of this class to be initialized when it is instantiated. The constructor should contain default values in case no initializes are provided.Provide Public member functions for each of the following operations: set the time, print the time, increment the time by one second,increment the time by one hour,compare two times for equality, determine if one time is “less than” (comes before) another time, and final print the time in format 12 hours.Include any additional operations that you think would be useful for your class.Design,implement, and test your class.

Using C++

Expert Solution
Step 1

Program:

#include <iostream>

using namespace std;

class time24 {
private:
 /* data */
 int hours;
 int minutes;
 int seconds;

public:
 /* functions */
 time24()
 {
  hours = 0;
  minutes = 0;
  seconds = 0;
 }
 time24(int hours, int minutes, int seconds)
 {
  this->hours = hours;
  this->minutes = minutes;
  this->seconds = seconds;
 }

 void setTime(int h, int m, int s)
 {
  hours = h;
  minutes = m;
  if (minutes >= 60)
  {
   hours = hours + 1;
   minutes = minutes - 60;
  }
  seconds = s;
  if (seconds > 59)
  {
   minutes = minutes + 1;
   seconds = seconds - 60;
  }
 }

 void printTime()
 {
  cout << "\n Printing time in 12-hour format only";
  if (hours <= 12)
  {
   cout << "\n hours:minutes:seconds \n";
   cout << " " << hours << "\t" << minutes << "\t" << seconds;
  }

  else if (hours > 12)
  {
   cout << "\n hours:minutes:seconds \n";
   cout << " " << (hours - 12) << "\t" << minutes << "\t" << seconds;
  }
 }

 int compare(time24& t)
 {
  if (hours < t.hours)
   return -1;
  else if (hours > t.hours)
   return 1;

  if (minutes < t.minutes)
   return -1;
  else if (minutes > t.minutes)
   return 1;

  if (seconds < t.seconds)
   return 1;
  else if (seconds > t.seconds)
   return -1;

  else
   return 0;
 }

 void incrementSeconds()
 {
  if (seconds < 60)
  {
   seconds = seconds + 1;
  }
  if (seconds >= 60)
  {
   minutes = minutes + 1;
   seconds = 0;
  }

  if (minutes >= 60)
  {
   if (seconds >= 60)
   {
    minutes = minutes + 1;
    seconds = 0;
   }

   else
   {
    hours = hours + 1;
    minutes = 0;
    seconds = seconds;
   }
  }
 }

 void incrementHours()
 {
  hours = hours + 1;
 }

 void incrementMinutes()
 {
  if (minutes < 60)
  {
   minutes = minutes + 1;
  }
  else if (minutes >= 60)
  {
   if (seconds >= 60)
   {
    minutes = minutes + 1;
    seconds = 0;
   }

   else
   {
    hours = hours + 1;
    minutes = 0;
    seconds = seconds;
   }
  }
 }
};

int main()
{
 time24 t1;
 time24 t2(12, 10, 18);
 int res;

 cout << "\n Initial printing of time from 3 distinct objects";
 t1.printTime();
 t2.printTime();

 cout << "\n \n Setting time for object 1 as following: \n";
 cout << " Hours: 18 \n Minutes: 64 \n Seconds: 32 \n";
 t1.setTime(18, 64, 32);
 t1.printTime();

 cout << "\n \n Comparing time between object 1 and object 2";
 cout << " and printing the greater time \n";
 res = t1.compare(t2);
 if (res == -1)
  t2.printTime();
 else if (res == 1)
  t1.printTime();

 cout << "\n Incrementing minutes and seconds for object 2 \n";
 t2.incrementMinutes();
 t2.incrementSeconds();
 // Printing time for object 2
 t2.printTime();

cout << "\n Incrementing minutes and seconds for object 1 \n";
 t1.incrementHours();
 // Printing time for object 1
 t1.printTime();
 return -1;
}

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY