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

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 =...
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++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data:  Account holder’s name (string)  Account number (int)  Account type (string, check/savings/business)  Balance (double)  Interest rate (double) – store interest rate as a decimal number.  Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. 1.2 Implement...
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...
Repl.it Python Ch 07 #5: Rock, Paper, Scissors Modification Programming Exercise 11 in Chapter 6 asked...
Repl.it Python Ch 07 #5: 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. I demonstrated the implementation of the program here: https://repl.it/@profeldridge/week06assignment06part2of2#main.py Using the repl.it link above,...
The goal of this exercise is to implement the shuffling algorithm from this chapter. 1. In...
The goal of this exercise is to implement the shuffling algorithm from this chapter. 1. In the repository for this book, you should find the file named Deck.java. Check that you can compile it in your environment. 2. Implement the randomInt method. You can use the nextInt method provided by java.util.Random, which we saw in Section 7.6. Hint: To avoid creating a Random object every time randomInt is invoked, consider defining a class variable. 3. Write a swapCards method that...
Using the class Date that you defined in Exercise O3, write the definition of the class...
Using the class Date that you defined in Exercise O3, write the definition of the class named Employee with the following private instance variables: first name               -           class string last name                -           class string ID number             -           integer (the default ID number is 999999) birth day                -           class Date date hired               -           class Date base pay                 -           double precision (the default base pay is $0.00) The default constructor initializes the first name to “john”, the last name to “Doe”, and...
Write program for the exercise 11(Sales Analysis) as explained at the end of the chapter. 11....
Write program for the exercise 11(Sales Analysis) as explained at the end of the chapter. 11. sales Analysis The file SalesData.txt, in this chapter’s source code folder, contains the dollar amount of sales that a retail store made each day for a number of weeks. Each line in the file contains seven numbers, which are the sales numbers for one week. The numbers are separated by a comma. The following line is an example from the file: 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36 Write a...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program 2) Create a class called TestScore that has a constructor that accepts an array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT