Question

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) – increment the date by N days.

void decrDate(int N) – decrement the date by N days.

int daysBetween(myDate D) – return the number of days between this date and the date D. If date D is a future date, the return value will be a positive int. If date D is in the past, the return value will be a negative int.

int getMonth() – return the month in integer form

int getDay() – return the day of the month

int getYear() – return the year

int dayOfYear() - return the number of days since the current year began. Example Jan 1 is 1, Feb 1 is 32.

string dayName() – returns Monday, Tuesday, etc according to the day of the week.

Consider the overloaded constructor, if any invalid data is passed to this constructor, ignore all data and set the values to the default date.

Here is the code I have so far! Please help me finish code BASED ON what I have.

#include<iostream>

#include<string>

#include "myDate.hpp"

using namespace std;

int Greg2Julian(int month, int day, int year){

int I=year;

int J=month;

int K=day;

int temp=(K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12)/12-3*((I+4900+(J-14)/12)/100/4));

  

return temp; //return julian number

}

void Julian2Greg(int JD, int&month, int&day, int&year){

  

int L = JD+68569;

int N = 4*L/146097;

L = L-(146097*N+3)/4;

int I = 4000*(L+1)/1461001;

L = L-1461*I/4+31;

int J = 80*L/2447;

int K = L-2447*J/80;

K = J/11;

J = J+2-12*L;

I = 100*(N-49)+I+L;

  

year=I;

month=J;

day=K;

}

myDate::myDate(){

m = 5;

d = 11;

y = 1959;

}

myDate::myDate(int M, int D, int Y){

d=D;

m=M;

y=Y;

//check validation!!

//-convert date to julian and than convert to gregorian again and see if they match.

}

void display(){

string month;

if(int m=1){

month = "January";

}

else if(int m=2){

month = "February";

}

else if(int m=3){

month = "March";

}

else if(int m=4){

month = "April";

}

else if(int m=5){

month = "May";

}

else if(int m=6){

month = "June";

}

else if(int m=7){

month = "July";

}

else if(int m=8){

month = "August";

}

else if(int m=9){

month = "September";

}

else if(int m=10){

month = "October";

}

else if(int m=11){

month = "November";

}

else if(int m=12){

month = "December";

}

printf("(%s %d, %d)",month,day,year);

  

}

void increaseDate(int N){

  

}

void decreaseDate(int N){

  

}

int daysBetween(myDate D){

  

}

int getMonth(){

return month;

}

int getDay(){

return day;

}

int getYear(){

return year;

}

int dayOfYear(){//return number of days since the current year began. ex)Feb1 is 32

}

string dayName(){

//return Mon,Tue,etc

}

#ifndef myDate_hpp

#define myDate_hpp

#include <stdio.h>

#include <iostream>

using namespace std;

class myDate{

public:

myDate();

myDate(int M, int D, int Y);

void display(); //format(May 11, 1959)

void increaseDate(int N); //increment the date by N days

void decreaseDate(int N); //decrement the date by N days

int daysBetween(myDate D); //return number of days between this date and date D. if date D is in past, return value will be negative int.

int getMonth();

int getDay();

int getYear();

int dayOfYear();

string dayName();

private:

int m;

int d;

int y;

};

#endif /* myDate_hpp */

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

// myDate.hpp

#ifndef myDate_hpp
#define myDate_hpp

#include <stdio.h>
#include <iostream>
using namespace std;

class myDate{

public:

   myDate();
   myDate(int M, int D, int Y);
   void display(); //format(May 11, 1959)
   void increaseDate(int N); //increment the date by N days
   void decreaseDate(int N); //decrement the date by N days
   int daysBetween(myDate D); //return number of days between this date and date D. if date D is in past, return value will be negative int.
   int getMonth();
   int getDay();
   int getYear();
   int dayOfYear();
   string dayName();

private:

   int m;
   int d;
   int y;

};

#endif /* myDate_hpp */

//end of myDate.hpp

// main.cpp


#include<iostream>
#include<string>
#include "myDate.hpp"
using namespace std;

int Greg2Julian(int month, int day, int year){

   int I=year;

   int J=month;

   int K=day;

   int temp=(K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12)/12-3*((I+4900+(J-14)/12)/100/4));
   return temp; //return julian number

}

void Julian2Greg(int JD, int&month, int&day, int&year){

   int L = JD+68569;

   int N = 4*L/146097;

   L = L-(146097*N+3)/4;

   int I = 4000*(L+1)/1461001;

   L = L-1461*I/4+31;

   int J = 80*L/2447;

   int K = L-2447*J/80;

   K = J/11;

   J = J+2-12*L;

   I = 100*(N-49)+I+L;

   year=I;

   month=J;

   day=K;

}

// default constructor
myDate::myDate(){

   m = 5;
   d = 11;
   y = 1959;
}

// parameterized constructor
// This will set the date to the values passed in through the parameter list represented by Month, Day and Year.
myDate::myDate(int M, int D, int Y){

//check validation!!
//-convert date to julian and than convert to gregorian again and see if they match.
   int julianDay = Greg2Julian(M,D,Y);
   int convDay, convMonth, convYear;
   Julian2Greg(julianDay,convMonth,convDay,convYear);

   if((convDay == D) && (convMonth == M) && (convYear == Y)) // valid
   {
       d=D;
       m=M;
       y=Y;
   }else //invalid , set it to default
   {
       m = 5;
       d = 11;
       y = 1959;
   }
}

// display the date
void myDate:: display(){

string month;

if(m==1){

month = "January";

}

else if(m==2){

month = "February";

}

else if(m==3){

month = "March";

}

else if(m==4){

month = "April";

}

else if(m==5){

month = "May";

}

else if( m==6){

month = "June";

}

else if( m==7){

month = "July";

}

else if( m==8){

month = "August";

}

else if(m==9){

month = "September";

}

else if(m==10){

month = "October";

}

else if( m==11){

month = "November";

}

else if( m==12){

month = "December";

}

cout<<month<<" "<<d<<y;

}

// helper function to return if the year is leap or not
bool isLeapYear(int year)
{
   return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)));
}

// helper function to return the days in the month
int daysInMonth(int month, int year)
{
   if(month == 2)
   {
       if(isLeapYear(year))
           return 29;
       else
           return 28;
   }else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month ==10 || month==12)
       return 31;
   else
       return 30;
}

// increment the date by N days.
void myDate::increaseDate(int N){
   d += N; // add number of days passed
   // loop that continues till the date is valid
   while(d > daysInMonth(m,y))
   {
       d -= daysInMonth(m,y);
       m++;

       if(m > 12) // if month > 12, i.e it represents the first month of next year
       {
           // increment year and set month to 1
           y++;
           m = 1;
       }
   }
}

// decrement the date by N days.
void myDate::decreaseDate(int N){

   // loop continues till number of days to add > 0
   while(N > 0)
   {
       if(N < d) // if N < number of days in date
       {
           // subtract N from d
           d -= N;
           N = 0;
       }else
       {
           m--; // decrement month
           // if m < 1, set month to 12 and decrement year
           if(m < 1)
           {
               m = 12;
               y--;
           }

           N -= d; // subtract d from N
           d = daysInMonth(m, y); // set day to number of days in m
       }

   }

}

/*
* return the number of days between this date and the date D.
* If date D is a future date, the return value will be a positive int.
* If date D is in the past, the return value will be a negative int.
*/
int myDate::daysBetween(myDate D){

   int days = 0;

   // D > this date
   if((D.getYear() > getYear()) || ((D.getYear() == getYear()) && (D.getMonth() > getMonth())) || ((D.getYear() == getYear()) && (D.getMonth() == getMonth()) && (D.getDay() > getDay())))
   {
       // add days in the year from the next year to year previous to D
       for(int i=getYear()+1;i<D.getYear();i++)
       {
           if(isLeapYear(i))
               days += 366;
           else
               days += 365;
       }

       // if same year, then add days for months from the next month in this date to month previous to D's month
       if(getYear() == D.getYear())
       {
           for(int i=getMonth()+1;i<D.getMonth();i++)
               days += daysInMonth(i,y);
           // add the days left in this month
           days += daysInMonth(m,y) - d;
           days += D.getDay(); // add the days passed in D's month
       }else // year not equal
       {
           // add the days in months from next month to end of this year
           for(int i=getMonth()+1;i<=12;i++)
               days += daysInMonth(i,y);

           // add the days left in this month
           days += daysInMonth(m,y) - d;

           // add the days in months from start of next year to month previous of D
           for(int i=1;i<D.getMonth();i++)
               days += daysInMonth(i,D.getYear());
           days += D.getDay(); // add the days passed in D's month
       }

       return days;
   }
   else // D < this
   {
       // add days in the year from the next year of D to year previous of this
       for(int i=D.getYear()+1;i<getYear();i++)
       {
           if(isLeapYear(i))
               days += 366;
           else
               days += 365;
       }

       // same year
       if(getYear() == D.getYear())
       {
           // add the days in months from next month of D to previous month of this
           for(int i=D.getMonth()+1;i<getMonth();i++)
               days += daysInMonth(i,y);

           // add the days left in D's month
           days += daysInMonth(D.getMonth(),D.getYear()) - D.getDay();
           days += getDay(); // add the days passed in this
       }
       else // year not equal
       {
           // add the days in months from next month to end of year of D
           for(int i=D.getMonth()+1;i<=12;i++)
               days += daysInMonth(i,getYear());

           // add the days left in D's month
           days += daysInMonth(D.getMonth(),D.getYear()) - D.getDay();

           // add the days from start of this year to previous month of this
           for(int i=1;i<getMonth();i++)
               days += daysInMonth(i,getYear());
           days += getDay(); // add the days passed in month of this
       }

       return -days;
   }
}

// return the month in integer form
int myDate::getMonth(){

return m;

}

// return the day of the month
int myDate::getDay(){

return d;

}

// return the year
int myDate::getYear(){

return y;

}

// return the number of days since the current year began. Example Jan 1 is 1, Feb 1 is 32.
int myDate::dayOfYear(){

   //return number of days since the current year began. ex)Feb1 is 32

   int days = 0;
   // loop to add the days from start of year to previous month
   for(int i=1;i<m;i++)
           days += daysInMonth(i,y);
       days += d; // add the number of days passed in this month
       return days;
}

// returns Monday, Tuesday, etc according to the day of the week.
string myDate:: dayName(){

   // calculate the day of the week using Zeller's congruence

   // check if month is January or February, then update month and year
   int month = m, day = d, year = y;
   if(month < 3)
   {
       month = month +12;
       year--;
   }

   // calculate the day of week in integer (0-6)
   int h = (day + ((13*(month+1))/5) + year + (year/4) - (year/100) + (year/400))%7;

   // display the day based on h
   switch(h)
   {
   case 0:
       return "Saturday";

   case 1:
       return "Sunday";

   case 2:
       return "Monday";

   case 3:
       return "Tuesday";

   case 4:
       return "Wednesday";

   case 5:
       return "Thursday";

   case 6:
       return "Friday";

   }

   return "";
}

//end of myDate.cpp

Add a comment
Know the answer?
Add Answer to:
Please create a C++ class called myDate. It should have the following operations: myDate() – default...
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
  • #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;...

  • Why isnt my MyCalender.java working? class MyCalender.java : package calenderapp; import java.util.Scanner; public class MyCalender {...

    Why isnt my MyCalender.java working? class MyCalender.java : package calenderapp; import java.util.Scanner; public class MyCalender { MyDate myDate; Day day; enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }    public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("Enter date below :"); System.out.println("Enter day :"); int day = sc.nextInt(); System.out.println("Enter Month :"); int month = sc.nextInt(); System.out.println("Enter year :"); int year = sc.nextInt(); MyDate md = new MyDate(day, month, year); if(!md.isDateValid()){ System.out.println("\n Invalid date . Try...

  • 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...

  • My values are not storing into my object and I keep returning zero. please explain my...

    My values are not storing into my object and I keep returning zero. please explain my mistake thanks public class CalanderDate { int year = 0 ; int day = 0; int month= 0;    public static void main(String[] args) { CalanderDate date; date = new CalanderDate( 1, 10 ,1999); System.out.print(date); }          public CalanderDate(int month, int day , int year ) { if (month < 1){ month = 1; } if (month> 12){ month = 12; }...

  • 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 )...

  • You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

    You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...

  • This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a fun...

    This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...

  • JAVA LANGUAGE create an empty Java file called Date build it to find any syntax errors...

    JAVA LANGUAGE create an empty Java file called Date build it to find any syntax errors and eliminate them identify each member of this class and write your answer in a comment There are four different types of class member: constant, instance variable, constructor, and method identify a method as class method if it is static /* * Java program: Date.java * * Define the class Date */ import java.time.LocalDateTime; public class Date { private static final int daysEachYear =...

  • 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: Set the month. Set the day. Set the year. Return the month. Return the day. Return the year. Test whether the year is a leap year. Return the number of days in...

  • General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You sho...

    General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You should create identifiers with sensible names; • You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. • Logical structures and statements are properly used for specific purposes. Objectives This assignment requires you to write...

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