Open Ended Lab
Open Ended Lab
Constructing Virtual Digital Clock Time & Performing Time Comparison of Two Clocks
Using Object Oriented Programming
Objectives:
To write C++ code of a digital clock time using object oriented programming
To compare time of two clocks using methods through object oriented programming
Introduction:
Digital clocks involve hour, minute and second as building blocks. When time is more than 24 hours, it resets to
their initial (zero) value. When any of these building blocks is less than 10, a “0” is also inserted in the start of
that element. In this lab, a digital clock time is created and the time comparison of two clock times is performed
using object oriented programming. So there must be three variables to be defined which represent hour, minute
and second. Since this problem is being solved using OOP, a class is first defined with attributes (private)
representing these building blocks and the corresponding methods (public) to set the times of two clocks,
comparing the two clock times, printing the clock time and then showing whatever the time is. The necessary
operations on the time entered by the user are also defined in the above methods.
Code:
/* The following code is written by Engr. Syed Ahtisham Mehmood Shah Feb 22, 2024*/
#include<iostream>
#include<string>
using namespace std;
class clock{
private:
int hr;
int min;
int sec;
public:
void set_time(int hours, int minutes, int seconds){
int count;
int remaining;
int count2;
int remaining2;
if(0<=hours && hours<24){
hr=hours;
} else{
hr=0;
}
if(0<=minutes && minutes < 60){
min=minutes;
}else if (minutes >=60){
count=minutes/60;
remaining=minutes%60;
hr=hr+count;
min=0+remaining;
}
else{
min=0;
}
if(0<=seconds && seconds < 60){
sec=seconds;
} else if (seconds >=60){
count2=seconds/60;
remaining2=seconds%60;
min=min+count2;
sec=0+remaining2;
}
else{
sec=0;
}
}
void get_time(int &hours,int &minutes,int &seconds)const{
hours=hr;
minutes=min;
seconds=sec;
}
void print_time()const{
if(hr<10){
cout<<"0";
cout<<hr<<":";
}
else{
cout<<hr<<":";
}
if(min<10){
cout<<"0";
cout<<min<<":";
}
else{
cout<<min<<":";
}
if(sec<10){
cout<<"0";
cout<<sec<<":";
}
else{
cout<<sec<<":";
}
}
void increment_hours(){
hr++;
if(hr>23){
hr=0;
}
}
void increment_minutes(){
min++;
if(min>59){
min=0;
increment_hours();
}
}
void increment_seconds(){
sec++;
if(sec>59){
sec=0;
increment_minutes();
}
}
bool equal_time(const clock&other_clock)const{
return (hr==other_clock.hr && min==other_clock.min&&sec==other_clock.sec);
}
};
int main(){
clock myclock;
clock yourclock;
int hours;
int minutes;
int seconds;
myclock.set_time(6,5,31);
cout<<"The time in my clock is: ";
myclock.print_time();
cout<<endl;
cout<<"The time in your clock is: ";
yourclock.print_time();
cout<<endl;
yourclock.set_time(6,45,31);
cout<<"After setting, The time in your clock is: ";
yourclock.print_time();
cout<<endl;
if(myclock.equal_time(yourclock)){
cout<<"Both times are equal"<<endl;}
else{
cout<<"Both times are not equal"<<endl;
}
cout<<"Enter the hours, minutes, and "<< "seconds: ";
cin>>hours>>minutes>>seconds;
cout<<endl;
myclock.set_time(hours,minutes,seconds);
cout<<"New time in my clock is: ";
myclock.print_time();
cout<<endl;
myclock.increment_seconds();
cout<<"After incrementing my clock by one second, the new time in my clock is ";
myclock.print_time();
cout<<endl;
myclock.get_time(hours,minutes,seconds);
cout<<"hours= "<<hours<<" minutes= "<<minutes<<" seconds= "<<seconds<<endl;
return 0;
}
/* The following code is written by Engr. Syed Ahtisham Mehmood Shah Feb 22, 2024*/
Code Output:
The output of the program depicts when the time of two clocks are not equal, the program notifies this aspect.
Similarly, when the time of two clocks is equal, it is also notified by the program as written in equal_time
method. When the user inputs hours, minutes and seconds, the time is also represented in hours, minutes and
time. The second is also incremented and the new time is also shown to verify the increment method. When the
time is given in terms of minutes and seconds greater than 60, the hours, minutes and seconds building blocks
adjust accordingly to right values. Similarly, when the time has passed 24 hours, the clock resets again and
restarts.
Conclusion:
This lab suggests that object oriented programming (OOP) is faster and easier to use programming language for
large codes which facilitates code reusability along with short development time. The above code, if written in
procedural oriented programming, would not have followed the “DRY” strategy.