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

Introduction Introduction to Data Structures programming assignments can be comp

ID: 3748407 • Letter: I

Question

Introduction

Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in Java. In both cases you cannot use libraries or packages that contain pre-built data structures, other than built-in support for objects, arrays, references, and pointers.

Classes in C++ and Java can represent anything in the real world. This assignment is to write a class to represent a Date in a Calendar.

Date Class Member Variables


The following table identifies and describes the purpose of each Date class member variable:

Name  

Purpose

month  

Number corresponding to month of year (e.g., 1-12)

day  

Number corresponding to day of month (e.g, 1 to 28-31)

year  

Number corresponding to year


All of these class member variables are private. You cannot add to or otherwise change these class member variables.

Date Class Member Functions

The following table identifies and describes the purpose of each Date class member function:

Name  

Purpose

setDate  

Assigns the value of each parameter to the month, date and year member variables respectively unless the date is not valid (see next section)

printDate  

Driver File

The code for the driver file is given below except of the last two statements. You need to call setDate and printDate, but you are not told exactly how. You can't change the code for the driver file except for properly calling setDate and printDate in the last two statements.

#include <iostream>

using namespace std;

#include "date.h"

int main()

{

Date d1;

int m, d, y;

cout << "Enter month, day and year separated by spaces: ";

cin >> m >> d >> y;

// call setDate

// call printDate
return 0;

}

What the Program Does

The user is prompted to enter a month, day and year. If the date is valid, then the setDate member function assigns the month, day and year inputs to the Date instance. If the date is not valid, then the values 9, 1, and 2018 are assigned respectively to the month, day and year member variables of the Date instance. The printDate function then outputs the Date instance in the format month/day/year.

A date is valid if:

The month is between 1 and 12.

The day is between 1 and 31. Yes, I know, for some months the maximum number of days may be less than 31 (between 28 and 30), but at LAVC, our philosophy is "close enough for government work."

The year is between 1900 and 2018.

Sample Runs

Sample #1 - Valid Date

Enter month, day and year separated by spaces: 9 17 1921
9/17/1921

Sample #2 - Invalid Date (Default date printed)

Enter month, day and year separated by spaces: 13 1 2000
9/1/2018

Multiple Files

Use multiple files, separating the class declaration, class implementation and driver into separate files, as in Module #2. Thus, your project will have three files, date.h (the declaration file for the Date class), date.cpp (the implementation file for the Date class) and test.cpp (the “driver” file which tests the class). All class members (variables and functions) must be private unless they need to be public.

Name  

Data Type

Purpose

month  

int

Number corresponding to month of year (e.g., 1-12)

day  

int

Number corresponding to day of month (e.g, 1 to 28-31)

year  

int

Number corresponding to year

Explanation / Answer

test.cpp

#include <iostream>


#include <iostream>

using namespace std;

#include "date.h"

int main()

{

Date d1;

int m, d, y;

cout << "Enter month, day and year separated by spaces: ";

cin >> m >> d >> y;

// call setDate
d1.setDate(m,d,y);
d1.printDate();

system ("pause");
// call printDate
return 0;

}

date.cpp

#include <iostream>
#include "date.h"
using namespace std;

void Date::setDateP(int& mo, int& da, int& ye)
{
  
   month = mo;
   day = da;
   year = ye;

   if (ye < 1900 || ye > 2008)
   {
       year=2008;
       day= 2;
       month = 9;
   }

   if (da < 1 || da > 31)
   {
       year=2008;
       day= 2;
       month = 9;
   }
   if (mo <1 || mo > 12)
   {
       year=2008;
       day= 2;
       month = 9;
   }

}

void Date::printDateP()
{
   cout<<month;
   cout<<"/";
   cout<<day;
   cout<<"/";
   cout<<year;
   cout<<endl;
}

void Date::setDate(int& M, int& D, int& Y)
{
   Date::setDateP(M,D,Y);
}

void Date::printDate()
{
   Date::printDateP();
}


date.h


using namespace std;

class Date
{
private:
   int month, day, year;
   void setDateP(int&, int&, int&);
   void printDateP();
public:

   // call setDate
   void setDate(int&, int&, int&);
   void printDate();
   // call printDate
};

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