Question

C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep...

C++ problem 11-6

In Programming Exercise 2, the class dateType was designed and implemented to keep track of a date, but it has very limited operations. Redefine the class dateType so that it can perform the following operations on a date, in addition to the operations already defined:

  1. Set the month.
  2. Set the day.
  3. Set the year.
  4. Return the month.
  5. Return the day.
  6. Return the year.
  7. Test whether the year is a leap year.
  8. Return the number of days in the month. For example, if the date is 3-12-2019, the number of days to be returned is 31 because there are 31 days in March.
  9. Return the number of days passed in the year. For example, if the date is 3-18-2019, the number of days passed in the year is 77. Note that the number of days returned also includes the current day.
  10. Return the number of days remaining in the year. For example, if the date is 3-18-2019, the number of days remaining in the year is 288.
  11. Calculate the new date by adding a fixed number of days to the date. For example, if the date is 3-18-2019 and the days to be added are 25, the new date is 4-12-2019.

For dataType.h

//dataType.h

#ifndef dateType_H

#define dateType_H

class dateType{

public:

void setDate(int, int, int);

void setMonth(int);

void setDay(int);

void setYear(int);

void print() const;

int numberOfDaysPassed();

int numberOfDaysLeft();

void incrementDate(int nDays);

int getMonth();

int getDay();

int getYear();

int getDaysInMonth();

bool isLeapYear();

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

private:

int dMonth;

int dDay;

int dYear;

};

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//dateType.h

#ifndef dateType_H

#define dateType_H

class dateType{

public:

void setDate(int, int, int);

void setMonth(int);

void setDay(int);

void setYear(int);

void print() const;

int numberOfDaysPassed();

int numberOfDaysLeft();

void incrementDate(int nDays);

int getMonth();

int getDay();

int getYear();

int getDaysInMonth();

bool isLeapYear();

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

private:

int dMonth;

int dDay;

int dYear;

};

#endif

//dateType.cpp

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

void dateType::setDate(int month, int day, int year){
   dMonth = month;
   dDay = day;
   dYear = year;
}

void dateType::setMonth(int month){
   dMonth = month;
}

void dateType::setDay(int day){
   dDay = day;
}

void dateType::setYear(int year){
   dYear = year;
}

void dateType::print() const{
   cout<<dMonth<<"-"<<dDay<<"-"<<dYear<<endl;
}

int dateType::numberOfDaysPassed(){
   int numDays=0;
   int monthDays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
   for(int i=1;i<dMonth;i++){
       if(i==2 && this->isLeapYear())numDays++;
       numDays+=monthDays[i];
   }
   numDays+=dDay;
   return numDays;
}

int dateType::numberOfDaysLeft(){
   if(this->isLeapYear()){
       if(dMonth == 1 || (dMonth==2 && dDay<29))return 366 - this->numberOfDaysPassed();
   }
   return 365 - this->numberOfDaysPassed();
}

void dateType::incrementDate(int nDays){
   bool endofMonth;
   int monthDays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
   for(int i=0;i<nDays;i++){
       endofMonth = false;
        if (dMonth == 2 && this->isLeapYear()) endofMonth = (dDay == 29);
        else endofMonth = (dDay == monthDays[dMonth]);

        if (!endofMonth) ++dDay;
  
        else{
        if (dMonth < 12)
        {
        ++dMonth;
        dDay = 1;
        }
        else
        {
        ++dYear;
        dMonth = 1;
        dDay = 1;
        }
        }
   }  
  
}

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

int dateType::getDay(){
   return dDay;
}

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

int dateType::getDaysInMonth(){
   int monthDays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
   return monthDays[dMonth];
}

bool dateType::isLeapYear(){
   if ((dYear % 4 == 0 && dYear % 100 != 0) || dYear % 400 == 0) return true;

   return false;
}

dateType::dateType(int month , int day , int year ){
   dMonth = month;
   dDay = day;
   dYear = year;  
}

//main.cpp

#include"dateType.cpp"

int main(){
   dateType date1(3,12,2019);
   cout<<"number of days in month : "<<date1.getDaysInMonth()<<"\n";
   dateType date2(3,18,2019);
  
   cout<<"number of days passed : "<<date2.numberOfDaysPassed()<<"\n";
   cout<<"number of days remaining : "<<date2.numberOfDaysLeft()<<"\n";
   date2.incrementDate(25);
   date2.print();
   return 0;
}

//sample output

Add a comment
Know the answer?
Add Answer to:
C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical...

    Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2019. Derive the class extDateType so that the date can be printed in either form. Add a member variable to the class extDateType so that the month can also be stored in string form. Add a member function to output the month in the string format, followed...

  • Can someone please help me. i keep getting an error in my code. I need it...

    Can someone please help me. i keep getting an error in my code. I need it to be on three seperate files! attached is my assignment and my code. c++ The class date Type implements the date in a program. Write the class functions as specified in the UML class diagram. The values for the month, day, and year should be validated (in the class functions) before storing the date into the data members. Set the invalid member data to...

  • The class dateType is designed to implement the date in a program, but the member function...

    The class dateType is 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 data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and the year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • Hello, I have a bug in my code, and when I run it should ask the...

    Hello, I have a bug in my code, and when I run it should ask the user to enter the month and then the year and then print out the calendar for the chosen month and year. My problem with my code is that when I run it and I enter the month, it doesn't ask me for the year and it prints all the years of the month I chose. Please help! Code: #include "calendarType.h" #include <iostream> using namespace...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • 12.2 LAB 12.2: Classes (Date) C++ Rewrite the class definition of Date class (From Lab 12.1)...

    12.2 LAB 12.2: Classes (Date) C++ Rewrite the class definition of Date class (From Lab 12.1) including 4 added member functions: bool isLeapYear(): this function will return true if the year of the date is a leap year. int daysPassed(): This function will return how many days has been passed in this year. int daysLeft(): This function will return how many days has left of this year. int diff(Date AnotherDate): This function will return the difference between the calling object...

  • Create a class named Date in C++ Class has 3 private data items (name the data...

    Create a class named Date in C++ Class has 3 private data items (name the data items month, day, year) int month int day int year Member functions ‘getters’   a getter for each data item Use ‘get’ and the data items name The data item name must be capitalized Return the data item Example: int getMonth()        {return month;} ‘setters’   a setter for each data item Use ‘set’ and the data items name The data item name must be capitalized Change...

  • Please create a C++ class called myDate. It should have the following operations: myDate() – default...

    Please create a C++ class called myDate. It should have the following operations: myDate() – default constructor. This will set the date to May 10, 1959. myDate(int M, int D, int Y) – overloaded constructor. This will set the date to the values passed in through the parameter list represented by Month, Day and Year. void display() – display the date in the following format (May 11, 1959) Do NOT print a linefeed after the date. void incrDate(int N) –...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT