In: Computer Science
How would I write this driver program in C++??
We just started learning C++ and I still don't understand how to use classes or randomly generate numbers, and I'm struggling to complete this assignment. The premise of the whole assignment is to randomly generate dates within a range and format the dates in 3 different ways.
Calendar.cpp
Create a driver program with a main function. Use symbolic
constants or "const" declarations here, also.
In particular, define local constant SIZE to be a constant with
value 5.
In the main function, create an array of Date objects of size SIZE.
Use a loop to set specific dates for all Date objects in the array.
In each iteration of the loop do the following:
1) Generate a year between 1950 (inclusive) and 2049 (inclusive)
using random number generations (generate year as a random number
between 1950 and 2049 inclusive). Use constants Date:: MIN_YEAR and
Date:: MAX_YEAR instead of numbers.
2) Prompt the user to enter month and day in the generated year.
Then, call the mutators to set those data members.
Print the elements of the array in the US format mm/dd/yyyy.
Print the elements of the array again in the format m/d/yy.
Print the elements of the array again in the standard International
format YYYY-MM-DD.
Add calls to the other member functions and constructors as
needed to test them thoroughly.
Also, add calls with invalid arguments placed in try / catch blocks
to test that invalid_argument exception is thrown for all functions
that could throw exceptions. When checking for a value that is
outside of a specified range, e.g., less than MIN value or greater
than MAX values, make sure to have separate tests for the case when
the value is less than MIN and when the values is greater than
MAX.
CODE FOR THE PROGRAMS THAT CONTAIN THE METHODS
********************************* code for Date.h *****************************
class Date
{
private:
unsigned int day;
unsigned int month;
unsigned int year;
public:
static const int MIN_DAY = 1;
static const int MAX_DAY = 12;
static const int MIN_MONTH = 1;
static const int MAX_MONTH = 31;
static const int MIN_YEAR = 1950;
static const int MAX_YEAR = 2049;
public:
// default constructor
Date();
// parameterised constructor
Date(int, int, int);
// creating mutators
void SetDay(unsigned int);
void SetMonth(unsigned int);
void SetYear(unsigned int);
// creatinng accessors
unsigned int getDay();
unsigned int getMonth();
unsigned int getYear();
// methods for printing date format
void printUS();
void printShort();
void printInternational();
};
*************************** end of Date.h ********************************
***************************** code for Date.cpp ****************************
#include<iostream>
#include<iomanip>
#include "Date.h"
using namespace std;
Date::Date()
{
day = MIN_DAY;
month = MIN_MONTH;
year = MIN_YEAR;
}
Date::Date(int m, int d, int y){
day = d;
month = m;
year = y;
}
void Date::SetDay(unsigned int d){
day = d;
}
void Date::SetMonth(unsigned int m){
month = m;
}
void Date::SetYear(unsigned int y){
year = y;
}
unsigned int Date::getDay(){
return day;
}
unsigned int Date::getMonth(){
return month;
}
unsigned int Date::getYear(){
return year;
}
void Date::printUS(){
cout.fill(0);
cout<<setw(2)<<getMonth()<<"/";
cout<<setw(2)<<getDay()<<"/";
cout<<setw(4)<<getYear();
}
void Date::printShort(){
cout.fill(0);
cout<<getMonth()<<"/";
cout<<getDay()<<"/";
cout<<setw(2)<<getYear();
}
void Date::printInternational(){
cout.fill(0);
cout<<setw(4)<<getYear()<<"-";
cout<<setw(2)<<getMonth()<<"-";
cout<<setw(2)<<getDay();
}
Program: In this program, we have implemented Date class and its methods, inside the Calender.cpp we test out date class by creating an array of Date class objects, we generate the year randomly in the range [1950,2049] and take user inputs for month and day. We validate the input given by user using try catch blocks and then print the output.
Date.h:
Code:
#ifndef DATE_H #define DATE_H class Date { private: unsigned int day; unsigned int month; unsigned int year; public: static const int MIN_DAY = 1; static const int MAX_DAY = 31; static const int MIN_MONTH = 1; static const int MAX_MONTH = 12; static const int MIN_YEAR = 1950; static const int MAX_YEAR = 2049; public: // default constructor Date(); // parameterized constructor Date(int, int, int); // creating mutators void SetDay(unsigned int); void SetMonth(unsigned int); void SetYear(unsigned int); // creating accessors unsigned int getDay(); unsigned int getMonth(); unsigned int getYear(); // methods for printing date format void printUS(); void printShort(); void printInternational(); }; #endif // DATE_H
Date.cpp:
Code:
#include<iostream> #include<iomanip> #include "Date.h" using namespace std; // Constructor Date::Date() { day = MIN_DAY; month = MIN_MONTH; year = MIN_YEAR; } // Parametrized constructor Date::Date(int m, int d, int y){ day = d; month = m; year = y; } // Mutators void Date::SetDay(unsigned int d){ day = d; } void Date::SetMonth(unsigned int m){ month = m; } void Date::SetYear(unsigned int y){ year = y; } // Accessors unsigned int Date::getDay(){ return day; } unsigned int Date::getMonth(){ return month; } unsigned int Date::getYear(){ return year; } // US format void Date::printUS(){ cout << setfill('0') << setw(2) << getMonth()<<"/"; cout << setfill('0') << setw(2) << getDay()<<"/"; cout << setfill('0') << setw(4) << getYear(); } // Short format void Date::printShort(){ cout<<getMonth() << "/"; cout<<getDay() << "/"; cout<< setfill('0') << setw(2) << getYear()%100; } // International format void Date::printInternational(){ cout<< setfill('0') << setw(4) << getYear()<<"-"; cout<< setfill('0') << setw(2) << getMonth()<<"-"; cout<< setfill('0') << setw(2) << getDay(); }
Calender.cpp:
Code:
#include <iostream> #include <time.h> #include "Date.h" using namespace std; static const int SIZE = 5; int main(){ // Seed time to generate numbers srand(time(0)); // Create an array of size SIZE Date dates[SIZE]; // Create a loop for (int i = 0; i < SIZE; ++i) { // Generate a random year between [1950 and 2049] int year = (rand() % (dates[i].MAX_YEAR - dates[i].MIN_YEAR + 1)) + dates[i].MIN_YEAR; // Prompt user take month input, day input cout << "Please enter month: "; int month; cin >> month; cout << "Please enter day: "; int day; cin >> day; // Set year dates[i].SetYear(year); // Try catch blocks for month, validate month try { if(month >= dates[i].MIN_MONTH && month <= dates[i].MAX_MONTH) { dates[i].SetMonth(month); } else{ throw(month); } } catch (int invalidMonth) { cout << "Invalid Month!" << endl; if(invalidMonth < dates[i].MIN_MONTH){ cout << "Values must be greater than 0" << endl; } else{ cout << "Value must me less than equal to 12" << endl; } } // Validate day try { if(day >= dates[i].MIN_DAY && day <= dates[i].MAX_DAY) { dates[i].SetDay(day); } else{ throw(day); } } catch (int invalidDay) { cout << "Invalid Month!" << endl; if(invalidDay < dates[i].MIN_DAY){ cout << "Values must be greater than 0" << endl; } else{ cout << "Value must me less than equal to 31" << endl; } } } // Print output on console cout << "\nDate in US Format" << endl; for(int i=0;i<SIZE;i++){ dates[i].printUS(); cout << endl; } cout << "\nDate in Short Format" << endl; for(int i=0;i<SIZE;i++){ dates[i].printShort(); cout << endl; } cout << "\nDate in International Format" << endl; for(int i=0;i<SIZE;i++){ dates[i].printInternational(); cout << endl; } cout << endl; return 0; }
Output:
#Please ask for any doubts. Thanks.