Due Thursday, Nov 3 before 11-59 pm There are quite a few files for this lab ass
ID: 3935035 • Letter: D
Question
Due Thursday, Nov 3 before 11-59 pm There are quite a few files for this lab assignment. Zip them up and submit a zip file. makefile Time.h Time.cpp Date.h Date.cpp Calendar.cpp (this is the driver program) Programs must compile. Programs that have syntax errors receive a grade of zero. Programs must have header comments, inline comments, consistent spacing, and consistent indentation (no less than 3 spaces per indent and no more than 5 spaces per indent). The only exception is variables that are used to Programs must declare ALL variables at the top of the function. control a for loop. int main ( void ) { // all variable declarations first // then executable statements Programs cannot have any "magic numbers" in the executable statements. be declared as symbolic constants or as variables with initial values. All numeric literals other than 0 or 1 must Creating a symbolic constant (pg 222 in the text). * include #define SIZE 10 Declaring a constant using"const" void checkHour (int x) i . const int HOUR LIMIT 12: // executable statements begin here if (xExplanation / Answer
Time.h
class Time{
private:
unsigned int hour, minute, second;
public:
//default constructor
Time();
//parameterize constructor
Time(int, int, int);
//accessor declaration
unsigned int getHour();
unsigned int getMinute();
unsigned int getSecond();
//mutators
void setHour(int);
void setMinute(int);
void setSecond(int);
//print the time in 24 hour format
void print();
//print the time in 12 hour format
void print12();
};
Date.h
class Date{
private:
unsigned int month, day, year;
public:
// default constructor
Date();
//paramterize constructor
Date(int, int, int);
//accessor
unsigned int getMonth();
unsigned int getDay();
unsigned int getYear();
//mutators
void setMonth(int);
void setDay(int);
void setYear(int);
//print the date in mm/dd/yy format
void print();
};
Time.cpp
#include "Time.h"
#include <iostream>
using namespace std;
const unsigned int MAX_HOUR = 12;
const unsigned int MAX_BIG_HAND = 60;
const unsigned int MAX_SMALL_HAND = 24;
//defining default constructor and setting every value to 0
Time::Time(){
hour = 0;
minute = 0;
second = 0;
}
//defining parametrize constructor and setting every value to the corresponding arguments using mutators
Time::Time(int h, int m, int s){
//calling mutators to set the values
hour = 0;
minute = 0;
second = 0;
//if hour is invalid
if(h < 0 || h >= MAX_SMALL_HAND)return;
//if minute is invalid
if(m < 0 || m >= MAX_BIG_HAND)return;
//if second is invalid
if(s < 0 || s >= MAX_BIG_HAND)return;
setHour(h);
setMinute(m);
setSecond(s);
}
//get hour
unsigned int Time::getHour(){
return hour;
}
//get minute
unsigned int Time::getMinute(){
return minute;
}
//get second
unsigned int Time::getSecond(){
return second;
}
//set hour
void Time::setHour(int h){
//if hour is invalid
if(h < 0 || h >= MAX_SMALL_HAND)return;
hour = h;
}
//set minute
void Time::setMinute(int m){
//if minute is invalid
if(m < 0 || m >= MAX_BIG_HAND)return;
minute = m;
}
//set second
void Time::setSecond(int s){
//if second is invalid
if(s < 0 || s >= MAX_BIG_HAND)return;
second = s;
}
//print the time in 24 hour format
void Time::print(){
//if hour or minute or second is less than 10 so we need to append 0 like instead of 9:9:12 we will show 09:09:12
if(hour < VALUE)cout << "0";
cout << hour << ":";
if(minute < VALUE)cout << "0";
cout << minute << ":";
if(second < VALUE)cout << "0";
cout << second << " ";
}
//print the time in 12 hour format
void Time::print12(){
//if hour or minute or second is less than 10 so we need to append 0 like instead of 9:9:12 we will show 09:09:12
char c[] = {'A','M',''}; // assign AM or PM
if(hour >= MAX_HOUR){
c[0] = 'P'; // as hour is greater than or equal to 12 set it PM
}
hour %= MAX_HOUR; // we need from 0 11 only so
if(hour < VALUE){
cout << "0";
}
cout << hour << ":";
if(minute < VALUE)cout << "0";
cout << minute << ":";
if(second < VALUE)cout << "0";
cout << second <<" "<< c <<" ";
}
Date.cpp
#include "Date.h"
#include <iostream>
using namespace std;
const unsigned int DEFAULT_YEAR = 1980;
const unsigned int MAX_YEAR = 9999;
const unsigned int MAX_MONTH = 12;
const unsigned int MAX_DAY = 31;
const unsigned int DAY_IN_FEB = 29;
const unsigned int IS_FEB_MONTH = 2;
const unsigned int THIRTY_DAYS_MONTH[] = {4, 6, 9, 11};
const unsigned int VALUE = 10;
//defining default constructor and setting as required
Date::Date(){
month = 1;
day = 1;
year = DEFAULT_YEAR;
}
//defining parametrize constructor and setting every value to the corresponding arguments using mutators
Date::Date(int m, int d, int y){
//calculating leap
int leap = y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
month = 1;day = 1;year = DEFAULT_YEAR; // setting default in case above parameter is wrong
// checking if value is valid or not
if(y < DEFAULT_YEAR || y > MAX_YEAR || m < 1 || m > MAX_MONTH ||
(leap == 1 && m == IS_FEB_MONTH && d > DAY_IN_FEB) ||
(leap == 0 && m == IS_FEB_MONTH && d >= DAY_IN_FEB) ||
d < 1 || d > MAX_DAY ||
((m == THIRTY_DAYS_MONTH[0] || m == THIRTY_DAYS_MONTH[1] ||
m == THIRTY_DAYS_MONTH[2] || m == THIRTY_DAYS_MONTH[3] ) && d >= (MAX_DAY)))return;
//calling mutators to set the values
setMonth(m);
setYear(y);
setDay(d);
}
//get day
unsigned int Date::getDay(){
return day;
}
//get month
unsigned int Date::getMonth(){
int y = getYear();
int leap = y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
return month;
}
//get year
unsigned int Date::getYear(){
return year;
}
//set day
void Date::setDay(int d){
int y = getYear(), m = getMonth();
int leap = y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
// check if day is valid or not
if((leap == 1 && m == IS_FEB_MONTH && d > DAY_IN_FEB) ||
(leap == 0 && m == IS_FEB_MONTH && d >= DAY_IN_FEB) ||
d < 1 || d > MAX_DAY ||
((m == THIRTY_DAYS_MONTH[0] || m == THIRTY_DAYS_MONTH[1] ||
m == THIRTY_DAYS_MONTH[2] || m == THIRTY_DAYS_MONTH[3] ) && d >= (MAX_DAY)))return;
day = d;
}
//set month
void Date::setMonth(int m){
// check if month is valid or not
if(m < 1 || m > MAX_MONTH)return;
month = m;
}
//set year
void Date::setYear(int y){
// check if year is valid or not
if(y < DEFAULT_YEAR || y > MAX_YEAR)return;
year = y;
}
//print the Date mm/dd/yyyy format
void Date::print(){
//if day or month has not of proper length we need to append 0 in start
if(month < VALUE)cout << "0";
cout << month << "/";
if(day < VALUE)cout << "0";
cout << day << "/";
cout << year << " ";
}
Calendar.cpp
#include <iostream>
#include "Date.cpp"
#include "Time.cpp"
#include <ctime>
#include <cstdlib>
using namespace std;
const int MAX_SIZE = 5;
int main(){
srand(time(NULL));
Time *time[MAX_SIZE];
Date *date[MAX_SIZE];
for(int i = 0; i < MAX_SIZE; ++i){
unsigned int hour = rand(),
minute = rand(), second = rand(), day = rand(), month = rand(), year = rand();
// Now get into their limit means hour can be 0 to 24
hour %= MAX_SMALL_HAND;
minute %= MAX_BIG_HAND;
second %= MAX_BIG_HAND;
day %= MAX_DAY;
day += 1;
month %= MAX_MONTH;
month += 1;
year %= (MAX_YEAR - DEFAULT_YEAR + 1);
year += DEFAULT_YEAR;
time[i] = new Time(hour, minute, second);
date[i] = new Date(month, day, year);
}
// time in 24 hour format
cout << "Time in 24 hour format ";
for(int i = 0;i < MAX_SIZE;++i){
time[i]->print();
}
cout << endl;
//time in 12 hour format
cout << "Time in 12 hour format ";
for(int i = 0;i < MAX_SIZE; ++i){
time[i]->print12();
}
cout << endl;
// Date print
cout << "Date of the Objects ";
for(int i = 0;i < MAX_SIZE;++i){
date[i]->print();
}
return 0;
}
I have commented where it was necessary. If you want you can ask me anything or improve the formating as required.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.