In C++ and preferably code blocks if you posses it. Thank you Design a class cal
ID: 3887717 • Letter: I
Question
In C++ and preferably code blocks if you posses it. Thank you
Design a class called dateType to implement the date in a program as a set of integers (year, month, day). The class will include the following operations:
Setters and getters for the private instance variables
A constructor which takes default parameters. Make the default date January 1, 1500.
A print function which outputs the date in the format mm-dd-yyyy.
A function to test whether the year is a leap year
A function which returns the number of days in the month.
A function which returns the number days passed in the year.
A function which returns the number of days remaining in the year
A function which determines if the date is valid. For example, a date of 2-29-2015 would be invalid because 2015 is not a leap year and there are only 28 days in February. Or, the date 4-31-2016 would be invalid because there are only 30 days in April.
Develop the class invariant which verifies that a date is valid.Note that the private instance variables can be set independently which could result in an invalid date. Your class must prevent that from happening. Identify the precondition/postcondition contract and document it in the header file. Write a program to test your class and show that it will not accept an invalid date.
Here is the UML diagram for this class:
Turn in your dateType.h, dateType.cpp, and test files. Also turn in one or more screen shots showing the results of your testing.
Once this project is completed, you will demonstrate that you are able to:
Develop a class invariant which takes into account dependencies among the private instance variables
Design member functions which will prevent these dependencies from invalidating the invariant.
Utilize the invariant to identify the precondition/postcondition contract
Develop test cases that evaluate the effectiveness of the precondition/postcondition contract.
Explanation / Answer
//main.cpp
#include <cstdlib>
# include<iostream>
# include "dateType.h"
using namespace std;
/*
* the program accepts the date and prints number of days passed and remain
*/
int main(int argc, char** argv) {
int yr,mn,dy;
// accepts the year,month,date
cout<<" Enter the year(yyyy) :";
cin>>yr;
cout<<" Enter the Month :";
cin>>mn;
cout<<" Enter the day :";
cin>>dy;
// constructor
dateType d(yr, mn, dy);
// check for the validation
if (d.validate()){
d.leapyear();
d.month_days();
d.passed_days();
d.remain_days();
}
// invalid
else
cout<<" Date invalid";
return 0;
}
//dateType.cpp
#include <iostream>
#include <string>
# include<iomanip>
#include "dateType.h"
using namespace std;
int arr[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
dateType::dateType() {
}
dateType::~dateType() {
}
//constructor assigning the values year, month and date
dateType::dateType(int y, int m, int d) {
year = y;
month = m;
day = d;
}
// setter year
void dateType::setyear(int y) {
year = y;
}
//function for setter month
void dateType::setmonth(int m) {
month = m;
}
// function for setter day
void dateType::setday(int d) {
day = d;
}
int dateType::getyear() {
return (year);
}
int dateType::getmonth() {
return (month);
}
int dateType::getday() {
return (day);
}
// function for checking leapyear
void dateType::leapyear() {
// if year is divided by 4 then its a leap year
if (year % 4 == 0)
cout << " " << year << " is a leap year";
else
cout << " " << year << " is not a leap year";
}
// calculating total number of days.
void dateType::month_days() {
// if the year is leap year total day should added with 1
int leapday;
if ((year % 4 == 0) && (month < 3))
leapday = 1;
else
leapday = 0;
cout << " TOTAL DAYS IN "<<month<< " IS : " << (arr[month - 1] + leapday);
}
// to calculate the number of days passed
void dateType::passed_days() {
int tot = 0;
int i;
for (i = 0; i < month - 1; i++)
tot = tot + arr[i];
tot = tot + day;
if ((year % 4 == 0) && month < 3)
tot = tot + 1;
cout << " TOTAL NUMBER OF DAYS PASSED IN THE YEAR : " << tot;
}
// to calculate the remaining days
void dateType::remain_days() {
int tot = 0;
int i;
for (i = month; i <= 12; i++)
tot = tot + arr[i];
tot = tot + (arr[month-1] - day);
if ((year % 4 == 0) && month > 2)
tot = tot + 1;
cout<<" TOTAL NUMBER OF DAYS REMAINING IN THE YEAR :"<<tot;
}
// to validate the date
bool dateType::validate() {
print();
bool flag;
if (day > arr[month - 1]){
flag = false;
if((year%4==0)&&(month =2)&&(day ==29))
flag = true;
}
else
flag = true;
return flag;
}
void dateType::print(){
cout<<setfill('0')<<setw(2)<<month<<"-"<<setfill('0')<<setw(2)<<day<<"-"<<setfill('0')<<setw(4)<<year;
}
//header file dateType.h
#ifndef DATATYPE_H
#define DATATYPE_H
class dateType{
private:
int year;
int month;
int day;
public:
// constructor for private variables
dateType();
~dateType();
dateType(int,int,int);
void setyear(int y);
int getyear();
void setmonth(int m);
int getmonth();
void setday(int d);
int getday();
// function validate for checking the date is valid date
bool validate();
// function for checking leap year
void leapyear();
// function for calculating the no. of days in the month
void month_days();
// function for calculating the number of days passed in the year
void passed_days();
// function for calculating the number of days remaining in the year
void remain_days();
void print();
};
#endif /* DATETYPE_H */
Testing #1
Enter the year(yyyy) :2016
Enter the Month :6
Enter the day :31
06-31-2016
Date invalid
Testing #2
Enter the year(yyyy) :2000
Enter the Month :2
Enter the day :29
02-29-2000
2000 is a leap year
TOTAL DAYS IN 2 IS : 29
TOTAL NUMBER OF DAYS PASSED IN THE YEAR : 61
TOTAL NUMBER OF DAYS REMAINING IN THE YEAR :305
RUN SUCCESSFUL (total time: 6s)