Write a program using C++ Would be nice if it could be done ASAP. Thanks! This i
ID: 3752975 • Letter: W
Question
Write a program using C++
Would be nice if it could be done ASAP. Thanks!
This is what it should like when it works (Sample Run):
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
I have constructed the constructor function, date, month, year, day_of_week, == and < functions.
HEADER FILE (date.h)
#include <iostream>
using namespace std;
class Date {
private:
int d;
int m;
int y;
public:
Date(int date = 1, int month = 1, int year = 2018);
bool validDate(int d, int m, int y);
int month();
int date();
int year();
string day_of_week();
bool operator ==(Date other);
bool operator <(Date other);
};
IMPLEMENTATION FILE (date.cpp)
#include "date.h"
Date::Date(int date, int month, int year) {
if (validDate(date, month, year)) {
d = date;
m = month;
y = year;
}
else {
d = 1;
m = 1;
year = 2018;
}
}
bool isLeap(int year)
{
// Return true if year
// is a multiple pf 4 and
// not multiple of 100.
// OR year is multiple of 400.
return (((year % 4 == 0) &&
(year % 100 != 0)) ||
(year % 400 == 0));
}
bool Date::validDate(int d, int m, int y)
{
// If year, month and day
// are not in given range
if (y < 0)
return false;
if (m < 1 || m > 12)
return false;
if (d < 1 || d > 31)
return false;
// Handle February month
// with leap year
if (m == 2)
{
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}
// Months of April, June,
// Sept and Nov must have
// number of days less than
// or equal to 30.
if (m == 4 || m == 6 ||
m == 9 || m == 11)
return (d <= 30);
return true;
}
int Date::month() {
return m;
}
int Date::date() {
return d;
}
int Date::year() {
return y;
}
// Formulas are taken from the question itself
string Date::day_of_week() {
// a is 1 for march, 2 for april and so on. For jan and feb, a is 11 and 12 resp.
int a = (m + 9) % 12 + 1;
int b = d;
int year = y;
if (m <= 2) {
year = y - 1;
}
int c = year % 100;
int d = year / 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;
}
switch (r) {
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
}
}
bool Date::operator ==(Date other) {
if (d != other.date()) {
return false;
}
if (m != other.month()) {
return false;
}
if (y != other.year()) {
return false;
}
return true;
}
bool Date::operator <(Date other) {
if (y < other.year()) {
return true;
} else if (y > other.year()) {
return false;
}
// if year is same
if (m < other.month()) {
return true;
} else if (m > other.month()) {
return false;
}
// if year and month are same
if (d < other.date()) {
return true;
} else {
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.