Question

In: Computer Science

Programming Exercise 11-2 QUESTION: In this chapter, the class dateType was designed to implement the date...

Programming Exercise 11-2

QUESTION: In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check whether a year is a leap year. Moreover, write a test program to test your class.

Input should be format month day year with each separated by a space. Output should resemble the following:

Date #: month-day-year

An example of the program is shown below:

Date 1: 3-15-2008
this is a leap year

If the year is a leap year, print the date and a message indicating it is a leap year, otherwise print a message indicating that it is not a leap year.

The header file for the class dateType has been provided for you.

GIVEN FILE: dateType.h

FILES NEEDED: dateTypeImp.cpp, main.cpp

~~~~~~dateType.h~~~~~~

#ifndef date_H

#define date_H

class dateType

{

public:

    void setDate(int month, int day, int year);

      //Function to set the date.

      //The member variables dMonth, dDay, and dYear are set

      //according to the parameters

      //Postcondition: dMonth = month; dDay = day;

      //               dYear = year

    int getDay() const;

      //Function to return the day.

      //Postcondition: The value of dDay is returned.

    int getMonth() const;

      //Function to return the month.  

      //Postcondition: The value of dMonth is returned.

    int getYear() const;

      //Function to return the year.     

      //Postcondition: The value of dYear is returned.

    void printDate() const;

      //Function to output the date in the form mm-dd-yyyy.

    bool isLeapYear();

      //Function to determine whether the year is a leap year.

    dateType(int month = 1, int day = 1, int year = 1900);

      //Constructor to set the date

      //The member variables dMonth, dDay, and dYear are set

      //according to the parameters

      //Postcondition: dMonth = month; dDay = day;

      //               dYear = year

      //If no values are specified, the default values are

      //used to initialize the member variables.

private:

    int dMonth;      //variable to store the month

    int dDay;        //variable to store the day

    int dYear;       //variable to store the year

};

#endif

Solutions

Expert Solution

Comment down for any queries
Please give a thumbs up if you are satisfied with the answer

//FIRST HEADER FILE OF dateType.h

#include<string>
#include<iostream>
using namespace std;


class dateType
{
   private:

       int dMonth; //variable to store the month

       int dDay; //variable to store the day

       int dYear; //variable to store the year

   public:

       dateType();

       dateType(int m, int d, int y);


           void setDate(int month, int day, int year);
      

       //Function to return the day.

       //Postcondition: The value of dDay is returned.
       int getDay() const;

       //Function to return the month.

       //Postcondition: The value of dMonth is returned.

       int getMonth() const;
      


       //Function to return the year.   

       //Postcondition: The value of dYear is returned.

       int getYear() const;
      

       //Function to output the date in the form mm-dd-yyyy.
       void printDate();
      

       //Function to determine whether the year is a leap year.
       bool isLeapYear();
      
      

};

//dateType.cpp FIRST CPP FILE

#include "DataType.h"

dateType::dateType()
{
   dMonth = 1;
   dDay = -1;
   dYear = 1900;
}

dateType::dateType(int m, int d , int y )
{
   if (m > 0 && m <= 12)
   {
       dMonth = m;
   }

   else
   {
       cout << "Month entered is wrong" << endl;
       dMonth = 1;
   }
   if (d > 0 && d <= 31)
   {
       dDay = d;
   }

   else
   {
       cout << "Day entered is wrong" << endl;
       dDay = 1;
   }

   dYear = y;
}


//Function to set the date.

//The member variables dMonth, dDay, and dYear are set

//according to the parameters

//Postcondition: dMonth = month; dDay = day;

// dYear = year


void dateType::setDate(int month, int day, int year)
{
   if (month > 0 && month <= 12)
   {
       dMonth = month;
   }
   if (day > 0 && day <= 31)
   {
       dDay = day;
   }

   dYear = year;
}


//Function to return the day.

//Postcondition: The value of dDay is returned.
int dateType::getDay() const
{

   return dDay;
}


//Function to return the month.

//Postcondition: The value of dMonth is returned.

int dateType::getMonth() const
{
   return dMonth;
}


//Function to return the year.   

//Postcondition: The value of dYear is returned.

int dateType::getYear() const
{
   return dYear;
}

//Function to output the date in the form mm-dd-yyyy.
void dateType::printDate()
{
   cout << "Month-Day-Year" << endl;
   if (isLeapYear() == true)
   {
       cout << "It's a Leap Year" << endl;
      
   }

   else
   {
       cout << "Not a leap year" << endl;
   }
   cout << dMonth << "-" << dDay << "-" << dYear << endl;

}

//Function to determine whether the year is a leap year.
bool dateType::isLeapYear()
{

   cout << "Year is:" << dYear << endl;


   if (dYear % 4 == 0)
   {
       if (dYear % 100 == 0)
       {
           if (dYear % 400 == 0)
               return true;
           else
               return false;
       }
       else
           return true;
   }
   else
       return false;
}

//2nd CPP FILE MAINCPP

#include"DataType.h"
#include<string>
#include<cmath>
using namespace std;
int main()
{

   int day, month, year;
   cout << "Month:";
   cin >> month;
   cout << "Day:";
   cin >> day;
   cout << "Year:";
   cin >> year;

   dateType obj(month, day, year);
   obj.printDate();

}

Comment down for any queries
Please give a thumbs up if you are satisfied with the answer


Related Solutions

C++ problem 11-2 In this chapter, the class dateType was designed to implement the date in...
C++ problem 11-2 In this chapter, the class dateType was designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the member variables. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and year are checked before storing the date into the member variables. Add a member function, isLeapYear, to check whether...
Modified from Chapter 07 Programming Exercise 5 Original Exercise: Rock, Paper, Scissors Modification Programming Exercise 11...
Modified from Chapter 07 Programming Exercise 5 Original Exercise: Rock, Paper, Scissors Modification Programming Exercise 11 in Chapter 6 asked you to design a program that plays the Rock, Paper, Scissors game. In the program, the user enters one of the three strings—"rock", "paper", or "scissors"—at the keyboard. Add input validation (with a case-insensitive comparison) to make sure the user enters one of those strings only. Modifications: Allow the user to input "r", "p", "s" or the full strings "Rock",...
Instructions Write a program to implement the algorithm that you designed in Exercise 19 of Chapter...
Instructions Write a program to implement the algorithm that you designed in Exercise 19 of Chapter 1. Your program should allow the user to buy as many items as the user desires. Instructions for Exercise 19 of Chapter 1 have been posted below for your convenience. TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Exercise 19 Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at one time, is $200...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by overloading the operators as nonmembers of the class rectangleType. The header and implementation file from Exercise 1 have been provided. Write a test program that tests various operations on the class rectangleType. I need a main.cpp file Given: **************rectangleType.cpp******************** #include <iostream> #include <cassert> #include "rectangleType.h" using namespace std; void rectangleType::setDimension(double l, double w) { if (l >= 0) length = l; else length =...
I need this in C# with pseudocode Exercise #2: Design and implement a programming (name it...
I need this in C# with pseudocode Exercise #2: Design and implement a programming (name it EvenOdd) that uses a while loop to determine and print out all even numbers between 50 and 100 on a single line, separated by commas. Then another while loop in the same program to print out all odd numbers between 50 and 100 on a new line, separated by commas. Document your code and properly label the outputs as shown below. Even numbers between...
I need this in C# please. Exercise #2: Design and implement a programming (name it NextMeeting)...
I need this in C# please. Exercise #2: Design and implement a programming (name it NextMeeting) to determine the day of your next meeting from today. The program reads from the user an integer value representing today’s day (assume 0 for Sunday, 1 for Monday, 2 for Tuesday, 3 for Wednesday, etc…) and another integer value representing the number of days to the meeting day. The program determines and prints out the meeting day. Format the outputs following the sample...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
C++ PROGRAM Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However,...
C++ PROGRAM Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However, in that exercise, the program could add only integers of, at most, 20 digits. This chapter explains how to work with dynamic integers. Design a class named largeIntegers such that an object of this class can store an integer of any number of digits. Add operations to add, subtract, multiply, and compare integers stored in two objects. Also add constructors to properly initialize objects...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class in the person class.    First, Create a date class.    Which has integer month, day and year    Each with getters and setters. Be sure that you validate the getter function inputs:     2 digit months - validate 1-12 for month     2 digit day - 1-3? max for day - be sure the min-max number of days is validate for specific month...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT