For the question you asked, I have a little over 8 hours for it to be done Thing
ID: 3754794 • Letter: F
Question
For the question you asked, I have a little over 8 hours for it to be done
Things to do:
1. date.h
2.Makefile
3. datedriver.cpp
4.date.cpp
Use C++ language to make the program
You have to make a date.h, date.cpp, Makefile and datedriver.cpp
Please follow ALL THE INSTRUCTIONS provided!! This is the third time I posted the question because all the things that need to be done for the project haven't been done!
Try to do it ASAP! Thank you!
Also, can you do a sample run at the end and send it with the answer
Final Result:
Consider the following specification for a Date class that models a calendar date in the Common Era. The analysis of each operation is given formally and the parameters must be declared in the order specified, but the design of each operation is given informally in English. Specification for Date Class Data Attributes: Type Name Objects month of the date day of the date year of the date int int int The class invariant (i.e., all valid objects must meet this condition) is: . month is 1 to 12 day is to 31 for January, March, May, July, August, October, and December, 1 to 30 for April, June, September, and November; 1 to 28 for February in a non-leap year, and to 29 for February in a leap year is greater than 0 . For those who do not recall the definition of a leap year, it is any year that is divisible by 4 but not by 100, or is divisible by 400. For example, 2016 is a leap year, 1900 is not a leap year, and 2000 is a leap year Operations Default/Explicit-value constructor initialize attributes from passed values Must have default argument values of 1, 1, 2018, for initial month, initial day, and initial year i.e January 1, 2018) there is no other constructor for this class. Precondition: Initial values must meet class invariant above. If they do not, the default values should be stored and an error message should be displayed saying so. Default Objects initial month initial day initial year Type Movement int int Name received received received 2018 . month Returns month of date Analysis Objects Type Movement Name month of date intExplanation / Answer
datedriver.cpp
#include <iostream>
#include <string>
#include <stdexcept>
#include "date.h"
int main () // no command-line arguments
{
using namespace std;
Date date1;
Date date2(10);
Date date3(9,19);
Date date4(9,19,2017);
cout << "The dates: " << endl;
cout << "Date 4 is " << date4 << endl;
// Check function day_of_week and string_name
cout << date4.day_of_week() << ", " << date4.string_name() << endl;
cout << date1 << endl;
// Check operator == < <= >= >
if(date1==date3)
cout << "Date 1 and Date 3 are the same!" << endl;
cout << "Input date: ";
cin >> date1;
cout << endl;
cout << date1.day_of_week() << ", " << date1.string_name() << endl;
if(date1 < date4)
cout << "Date 1 is ealier than date 4 ";
else if(date1 <= date4)
cout << "Date 1 is ealier than or the same as date 4 ";
if(date1 > date4)
cout << "Date 1 is later than date 4 ";
else if(date1 >= date4)
cout << "Date 1 is later than or equal date 4 ";
if(date1 != date4)
cout << "Date 1 is different from date 4 ";
return 0;
}
date.h
#ifndef DATE_H_
#define DATE_H_
#include <iostream>
class Date
{
public:
Date(int m=1, int d=1, int y=2017); // Explicit value constructor: mm/dd/yyyy
int month() const; // return month
int day() const; // return day
int year() const; // return year
std::string string_name() const; // return date in string form
std::string day_of_week() const; // rerurn the day of week
friend bool operator== (const Date & lhs, const Date & rhs);
friend bool operator< (const Date & lhs, const Date & rhs);
friend bool operator<= (const Date & lhs, const Date & rhs);
friend bool operator> (const Date & lhs, const Date & rhs);
friend bool operator>= (const Date & lhs, const Date & rhs);
friend bool operator!= (const Date & lhs, const Date & rhs);
// I/O operators
// ostream is the type of cout, it is the lhs parameter
// The actual ostream, object is returned, so the return type is ostream &, a reference to the returned object
friend std::istream & operator>> (std::istream & instream, Date & aDate);
friend std::ostream & operator<< (std::ostream & outstream, const Date & aDate);
private:
int mm, dd, yyyy; // dd, mm, yyyy
void reduce();
};
#endif
date.cpp
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <cassert>
#include <stdexcept>
#include <cassert>
#include "date.h"
#include <string>
using namespace std;
bool valid(int m, int d, int y)
{
bool leap_y=false;
if(y<1 || d<1 || m<1 || m>12)
return false;
// Check if that is a leap year
if((y%4==0 && y%100!=0) || y%400==0)
leap_y=true;
if((m<8 && m%2==1) || (m>7 && m%2==0))
{
if(d>31)
return false;
}
else
if(d>30)
return false;
if(m==2 && leap_y==true && d>29)
return false;
if(m==2 && leap_y==false && d>28)
return false;
return true;
}
// Explicit value constructor: mm/dd/yyyy
// Class invariant - something that must alway be true for all objects of the type
Date::Date(int m, int d, int y)
{
mm=m;
dd=d;
yyyy=y;
reduce();
}
// Function: month
// Returns: month of the date
int Date::month() const
{
return mm;
}
// Function: day
// Returns: day of the date
int Date::day() const
{
return dd;
}
// Function: year
// Returns: year of the date
int Date::year() const
{
return yyyy;
}
// Function: string_name
// Return: string name of date
string Date::string_name() const
{
string const month[]={ "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
ostringstream ostr;
ostr << month[mm-1] << ' ' << dd << ", " << yyyy;
return ostr.str();
}
// Function: day_of_week
// Return: name of the day
string Date::day_of_week() const
{
int y2=yyyy;
if(mm<3)
y2--;
int a=(mm+9)%12+1;
int b=dd;
int c=y2%100;
int d=y2/100;
int w=(13*a-1)/5;
int x=c/4;
int y=d/4;
int z=w+x+y+b+c-2*d;
int r=z%7;
if(r<0)
r+=7;
string day_name[]={"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"};
return day_name[r];
}
// Operator: ==, < , <= , > , >= , !=
// Return: T/F
bool operator== (const Date & lhs, const Date & rhs)
{
if(lhs.dd==rhs.dd && lhs.mm==rhs.mm && lhs.yyyy==rhs.yyyy)
return true;
return false;
}
bool operator< (const Date & lhs, const Date & rhs)
{
if(lhs.yyyy<rhs.yyyy)
return true;
if(lhs.yyyy>rhs.yyyy)
return false;
if(lhs.mm<rhs.mm)
return true;
if(lhs.mm>rhs.mm)
return false;
if(lhs.dd<rhs.dd)
return true;
return false;
}
bool operator<= (const Date & lhs, const Date & rhs)
{
if(lhs==rhs || lhs<rhs)
return true;
return false;
}
bool operator> (const Date & lhs, const Date & rhs)
{
if(!(lhs<=rhs))
return true;
return false;
}
bool operator>= (const Date & lhs, const Date & rhs)
{
if(!(lhs<rhs))
return true;
return false;
}
bool operator!= (const Date & lhs, const Date & rhs)
{
if(!(lhs==rhs))
return true;
return false;
}
// Operator: <<
// Return: print out the date in form mm/dd/yyyy
std::ostream & operator<< (std::ostream & outStream, const Date & a_Date)
{
outStream << a_Date.mm << '/' << a_Date.dd << '/' << a_Date.yyyy;
return outStream;
}
// Operator: >>
// Return: read in mm/dd/yyyy
std::istream & operator>> (std::istream & inStream, Date & a_Date)
{
int m, d, y; //local variable for input data
inStream >> m;
if(inStream.fail())
return inStream;
if(inStream.peek()=='/')
{
inStream.ignore();
inStream >> d;
if(inStream.fail())
return inStream;
}
if(inStream.peek()=='/')
{
inStream.ignore();
inStream >> y;
if(inStream.fail())
return inStream;
}
else
{
d=1;
y=2017;
}
if(!valid(m, d, y))
{
inStream.setstate(std::ios_base::failbit);
return inStream;
}
// everyhting is good
a_Date.dd=d;
a_Date.mm=m;
a_Date.yyyy=y;
return inStream;
}
// Terminate if the date was invalid
void Date::reduce()
{
if (!valid(mm, dd, yyyy))
throw std::range_error ("Date::reduce(): invalid date");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.