Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Visual Studio : Create a class called Time that includes three pieces of informa

ID: 3870512 • Letter: V

Question

Visual Studio :

Create a class called Time that includes three pieces of information as data members – an hour (type int), a minute (type int), and a year (type int). Your class should have a constructor with three parameters that uses the parameters to initialize the three data members. For the purpose of this project: • Ensure that the hour value is in the range 0 – 23; if it isn’t, set the hour to 12. • Ensure that the minute value is in the range 0 – 59; if it isn’t, set the minute to 0. • Ensure that the second value is in the range 0 – 59; if it isn’t, set the second to 0. Provide a set and get function for each data member. Provide a member function displayTime that displays the hour, minute and second separated by colons (:). displayTime Function should display hour, minute and second as two digits each. For example, if the value is 11, it should be displayed as 11, and if the value is 1, it should be displayed as 01. Write a test program that demonstrates class Time’s capabilities.

Explanation / Answer

#include<iostream>
#include <iomanip>
using namespace std;
class Time {
private:
int hour, minute, second;
public:
Time(int h, int m, int s) {
hour = h;
if(hour < 0 || hour > 23) {
hour = 12;
}
minute = m;
if(minute < 0 || minute > 59) {
minute = 0;
}
second = s;
if(second < 0 || second > 59) {
second = 0;
}
}
int getMinute() {
return minute;
}
void setMinute(int m){
minute = m;
if(minute < 0 || minute > 59) {
minute = 0;
}
}
int getSecond(){
return second;
}
void setSecond(int s) {
second = s;
if(second < 0 || second > 59) {
second = 0;
}
}
int getHour() {
return hour;
}
void setHour(int h){
hour = h;
if(hour < 0 || hour > 23) {
hour = 12;
}
}
void displayTime() {
cout << setfill('0') << setw(2) << hour<<":"<< setfill('0') << setw(2)<<minute<<":"<< setfill('0') << setw(2)<<second<<endl;
}
};
int main() {
Time t(11,23,45);
t.displayTime();
t.setSecond(66);
t.displayTime();

return 0;
}

Output: