lab4-if - Modifying and Extending lab2-weekday to include OOP, if, and switch

Recall lab2-weekday, in which we developed a program to evaluate the day of the week in the Gregorian calendar. In this lab we wish to convert the program weekday.cpp to an Object Oriented Program (OOP) format, having a class Weekday which includes a constructor (to construct an object of the class) and a method find_day() in order to evaluate the weekday of the object. Once this is done then we can proceed with extending the program as follows:

The main purpose of this lab is to reinforce your knowledge on OOP as well as this week's topic, being the use of the selection constructs if-else and switch-break.

The problem statement is embedded in a program given in the week4/lab4-if directory. After logging in, change directory and list the various files as follows:

cd /home/condor/et181/week4/lab4-if
ls

Notice that there are the usual 3 files shown, the file containing the problem statement weekday2.cpp, the (hidden) solution weekday2soln.cpp, and the compiled solution weekday2*. The problem statement can be displayed with either the more command (page by page) or the cat command (the entire file) as follows:

cat weekday2.cpp

This should result in the following listing:

/* et181 Lab 4 - weekday2.cpp
   We wish to modify our solution for lab 2 - weekday.cpp in 3
   successive steps as follows:
   1. Convert the program weekday.cpp to an Object Oriented 
      Program (OOP) having a class Weekday including a 
      Constructor and a void method find_day() as shown below. 
      Once an object of class Weekday has been constructed, then 
      the method find_day() should be invoked to determine and 
      display the day of the week as was done in Lab 2.
   2. After reading in the year and day_in_year, the program
      should first determine if the input data is valid (year
      > 0) or (day_in_year lies between 1 and 366) before
      continuing to construct an object and evaluate the weekday. 
      If invalid data then the program should be aborted with a 
      suitable message.
   3. The void method find_day() should be augmented with a switch
      statement in order to display the actual day (eg Monday)
      rather than an integer number (0 - 6). 
*/
//***************************************************************
// weekday2.cpp
// Determine the day of the week from the day_in_year and the year
//  PROGRAMMER:   {your name}  {today's date}
//  USAGE:  user enters the year and day_in_year, and the day of
//     the week {eg Saturday} is evaluated and displayed
#include <iostream>
using namespace std;
class Weekday{
   private:
      int year, // the year in question - must be positive
          day_in_year; // the julian day - between 1 and 366
   public:
      Weekday(int yr = 2008, int day = 1)
      { //Constructor - default is Jan 1, 2008
         year = yr;
         day_in_year = day;
         cout << "year is " << year
              << ", and the day is " << day_in_year << endl;
      }
     //************************************************************
     void find_day() //to evaluate and display the day of the week
     {
        //  pre:  year and day_in_year have specified integer values 
        //  post:     weekday has been displayed (0 - 6)               
        int fours, hundreds, four_hundreds;
        int weekday;
/* 
    The method should use the algorithm developed in lab2-weekday
    to evaluate the day of the week "weekday". 
*/
        cout << "\nThe day of the week is " << weekday << endl;
        cout << "   Note: 0 = Saturday, 6 = Friday\n";
/*
    For step 3, replace the above two statements with the switch-break
    statement which will display the actual weekday (eg. Saturday)
*/
     }
     //************************************************************
};

int main()
{
   int year, day_in_year;
   cout << "Program will evaluate day of the week (0 - 6, 0 = Saturday)\n";
   cout << "  enter the year in question (all 4 digits)\n";
   cin >> year;
   cout << "  value entered is " << year << endl;
   cout << "  enter the day_in_year (1 - 366)\n";
   cin >> day_in_year;
   cout << "  value entered is " << day_in_year << endl;
/*
   For Step 2 the program should test if both year and day_in_year 
   are valid, else abort with a suitable message
*/
/*
   Replace the following default constructor with one that constructs
   the object myday(year, day_in_year) and then invoke the method
   find_day() to display the day in the week.
*/
   Weekday myday; // default constructor (Jan 1, 2008)
   return 0;
}

Notice that as before the problem statement is followed by a partially completed program including the class declaration and various comment statements indicating the modifications needed.

Once you have read and understood the problem and required tasks you can try running the program of a typical solution as follows:

weekday2soln

In order to modify weekday2.cpp and solve the lab, you need to first copy it to your home directory, and return to your home directory as follows:

cp weekday2.cpp ~
cd

As always, we strongly suggest that you complete the program in many steps. This cannot be overemphasized! This program does compile and execute, however we see that after entering the two data items it constructs the default date (Jan 1, 2008) and that's all. As you add statements, compile and test the response. This is the only way to retain sanity in this game.

Be sure to show what you have done to the lab tutor in order to obtain a grade.

Telnet: "condor"