Define a class in Matlab that represents a clock Just take some pictures with th
ID: 3803019 • Letter: D
Question
Define a class in Matlab that represents a clock
Just take some pictures with the code written
Clock: Creates a new clock with initial time 00:00:00 on 1 January 1970
addDays: Adds the given integer number of days to the clock
addHours: Adds the given integer number of hours to the clock
addMonths: Adds the given integer number of months to the clock
addSeconds: Adds the given integer number of seconds to the clock
addYears: Adds the given integer number of years to the clock
countdown: Returns the time until the given time (represented as a Clock object) is reached as a string in the format years:months:weeks:days:hours:minutes:seconds
getDay: Returns the day on the clock as an integer
getHour: Returns the hours on the clock as an integer
getMinute: Returns the minute on the clock as an integer
getMonth: Returns the month on the clock as an integer
getNotation: Returns the notation of the clock as an integer (12 or 24)
getSecond: Returns the seconds on the clock as an integer
getTime: Returns the number of seconds elapsed (till the current time on clock) since the "beginning of time" at 00:00:00 on 1 January 1970
now: Returns the time on the clock as a string in the format hh:mm:ss dd:mm:yyyy
setDay: Sets the day on the clock to the given integer value
setHour: Sets the hours on the clock to the given integer value
setMinute: Sets the minutes on the clock to the given integer value
setMonth: Sets the month on the clock to the given integer value
setNotation: Sets the notation of the clock to the given integer value (12 or 24)
setSecond: Sets the seconds on the clock to the given integer value
setTime: Sets the time on the clock to the given time (represented by a Clock object)
setYear: Sets the year on the clock to the given integer value
tick: Increments the time on the clock by 1 second
time: Alias for getTime()
Explanation / Answer
#include<iostream>
#include<iomanip.h>
using namespace std;
void convert(int,int&,int&,int&);
class Time
{
public:
int hour,minute,second;
Time()
{hour=0;
minute=0;
second=0;
}
~Time()
{hour=0;
minute=0;
second=0;
}
Time(int h,int m,int s)
{hour=h;
minute=m;
second=s;
}
void get()
{cout<<"Enter the time ";
cout<<"Hour: ";
cin>>hour;
cout<<"Minutes: ";
cin>>minute;
cout<<"Seconds: ";
cin>>second;
}
void show()
{cout<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<minute<<":"<<setw(2)<<setfill('0')<<second<<endl;
}
Time operator + ( Time t)
{int h,m,s;
int totalsec;
totalsec=(hour*3600+minute*60+second)+(t.hour*3600+t.minute*60+t.second);
convert(totalsec,h,m,s);
return Time(h,m,s);
}
Time operator - ( Time t)
{int h,m,s,totalsec;
totalsec=(hour*3600+minute*60+second)-(t.hour*3600+t.minute*60+t.second);
if(totalsec<0)
totalsec=-totalsec;
convert(totalsec,h,m,s);
return Time(h,m,s);
}
void convert(int totalsec,int& h,int& m,int& s)
{ s=totalsec%60;
totalsec=totalsec/60;
m=totalsec%60;
h=(totalsec/60)%12;
if(h==0)
h=12;
return;
}
};
int main()
{ Time time1,time2,added,subtracted;
cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);
time1.get();
time2.get();
added=time1+time2;
subtracted=time1-time2;
cout<<"Time 1 : ";
time1.show();
cout<<"Time 2 : ";
time2.show();
cout<<" Added : ";
time1.show();
cout<<"+";
time2.show();
cout<<"---------- ";
added.show();
cout<<" Subtracted: ";
time1.show();
cout<<"-";
time2.show();
cout<<"---------- ";
subtracted.show();
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.