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

Design a class called dateType to implement the date in a program as a set of in

ID: 3882640 • Letter: D

Question

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

header file.: dataType.h

// declares the variables and functions

#ifndef DATATYPE_H
#define DATATYPE_H

class dataType{
private:
int year;
int month;
int day;
public:
// consstructor for private variables
dataType();
~dataType();
dataType(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();

};

#endif /* DATATYPE_H */

// declaration of header file functions

program dataType.cpp


#include <iostream>
#include <string>

#include "dataType.h"

using namespace std;
int arr[] = {31,28,31,30,31,30,31,31,30,31,30,31};
dataType ::dataType(){}
dataType::~dataType(){}
//constructor assigning the values year, month and date
dataType::dataType(int y, int m, int d){
year = y;
month = m;
day = d;
}
// setter year
void dataType :: setyear(int y)
{
year = y;
}
//function for setter month
void dataType::setmonth(int m)
{
month = m;
}
// function for setter day
void dataType ::setday(int d)
{
day = d;
}
int dataType::getyear()
{
return(year);
}
int dataType::getmonth()
{
return(month);
}
int dataType::getday()
{
return(day);
}
// function for checking leapyear
void dataType::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 dataType::month_days(){
// if the year is leap year total day should added with 1
if((year%4==0) && month<2)
cout<<" Total number of days in the month : "<<arr[month];
else
cout<<" Total number of days in the month : "<<arr[month]+1;
}
// to calculate the number of days passed
void dataType::passed_days(){
int tot=0;
int i;
for(i = 0; i<month;i++)
tot = tot+arr[i];
tot = tot+day;
if((year%4==0) && month<3)
tot = tot+1;
cout<<" Total no. of days passed is : "<<tot;
}
// to calculate the remaining days
void dataType::remain_days()
{
int tot = 0;
int i;
for(i = month+1; i<=12;i++)
tot = tot+arr[i];
tot = tot+(arr[month]-day);
if((year%4==0) && month>2)
tot = tot+1;
cout<<" total no. of days remaining is :"<<tot;
  
}
// to validate the date
bool dataType::validate(){
bool flag;
if (day>arr[month])
flag = false;
else
flag = true;
return flag;
}

program main.cpp

#include <cstdlib>
# include<iostream>
# include "dataType.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 :";
cin>>yr;
cout<<" Enter the Month :";
cin>>mn;
cout<<" Enter the day :";
cin>>dy;

// constructor
dataType d(yr, mn, dy);

// check for the validation
if (d.validate()){
d.leapyear();
d.month_days();
d.passed_days();
d.remain_days();
}

// invalidate
else
cout<<" Date is invalidate";
return 0;
}

sample output:


Enter the year :2010

Enter the Month :4

Enter the day :34

Date is invalidate
RUN SUCCESSFUL (total time: 8s)

ouput 2#


Enter the year :2016

Enter the Month :3

Enter the day :20

2016 is a leap year
Total number of days in the month : 31
Total no. of days passed is : 110
total no. of days remaining is :256
RUN SUCCESSFUL (total time: 6s)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote