60.1K

Verified Solution

Question

Programming

public class Date{  private int dMonth;     //variable to store the month  private int dDay;      //variable to store the day  private int dYear;     //variable to store the year        //Default constructor        //Data members dMonth, dDay, and dYear are set to        //the default values        //Postcondition: dMonth = 1; dDay = 1; dYear = 1900;  public Date()  {        dMonth = 1;        dDay = 1;        dYear = 1900;  }    //Constructor to set the date        //Data members dMonth, dDay, and dYear are set    //according to the parameters        //Postcondition: dMonth = month; dDay = day;    //        dYear = year;  public Date(int month, int day, int year)  {        setDate(month, day, year);  }       //Method to set the date       //Data members dMonth, dDay, and dYear are set       //according to the parameters       //Postcondition: dMonth = month; dDay = day;       //             dYear = year;  public void setDate(int month, int day, int year)  {        if (year >= 1)            dYear = year;    else            dYear = 1900;        if (1 <= month && month <= 12)            dMonth = month;        else            dMonth = 1;        switch (dMonth)        {        case 1: case 3: case 5: case 7:        case 8: case 10: case 12: if (1 <= day && day <= 31)                                     dDay = day;                                 else                                     dDay = 1;                                 break;        case 4: case 6:        case 9: case 11: if (1 <= day && day <= 30)                             dDay = day;                         else                             dDay = 1;                         break;        case 2: if (isLeapYear())                {                    if (1 <= day && day <= 29)                           dDay = day;                      else                           dDay = 1;                }                else                {                    if (1 <= day && day <= 28)                        dDay = day;                    else                        dDay = 1;                }        }  }       //Method to return the month       //Postcondition: The value of dMonth is returned  public int getMonth()  {        return dMonth;  }       //Method to return the day       //Postcondition: The value of dDay is returned  public int getDay()  {    return dDay;  }       //Method to return the year       //Postcondition: The value of dYear is returned  public int getYear()  {        return dYear;  }       //Method to return the date in the form mm-dd-yyyy  public String toString()  {            return (dMonth + \"-\" + dDay + \"-\" + dYear);  }  public boolean isLeapYear()  {        if ((dYear % 4 == 0 && dYear % 100 != 0) || (dYear % 400 == 0))            return true;        else            return false;  }  public void setMonth(int m)  {        dMonth = m;  }  public void setDay(int d)  {        dDay = d;  }  public void setYear(int y)  {        dYear = y;  }  public int getDaysInMonth()  {        int noOfDays = 0;        switch (dMonth)        {        case 1: case 3: case 5:        case 7: case 8: case 10: case 12: noOfDays = 31;                                     break;    case 4: case 6: case 9: case 11: noOfDays = 30;                                         break;        case 2: if (isLeapYear())                    noOfDays = 29;            else                noOfDays = 28;        }        return noOfDays;  }  public int numberOfDaysPassed()  {        int[] monthArr = {0,31,28,31,30,31,30,31,31,30,31,30,31};        int sumDays = 0;        int i;        for (i = 1; i < dMonth; i++)            sumDays = sumDays + monthArr[i];        if (isLeapYear() && dMonth > 2)            sumDays = sumDays + dDay + 1;    else            sumDays = sumDays + dDay;        return sumDays;  }  int numberOfDaysLeft()  {        if (isLeapYear())            return 366 - numberOfDaysPassed();        else            return 365 - numberOfDaysPassed();  }  public void incrementDate(int nDays)  {        int[] monthArr = {0,31,28,31,30,31,30,31,31,30,31,30,31};        int daysLeftInMonth;        daysLeftInMonth = monthArr[dMonth] - dDay;        if (daysLeftInMonth >= nDays)            dDay = dDay + nDays;        else        {            dDay = 1;            dMonth++;            nDays = nDays - (daysLeftInMonth + 1);        while (nDays > 0)                if (nDays >= monthArr[dMonth])            {                nDays = nDays - monthArr[dMonth];                if ((dMonth == 2) && isLeapYear())                    nDays--;                dMonth++;                if (dMonth > 12)                {                    dMonth = 1;                    dYear++;                }            }            else            {                dDay = dDay+nDays;                nDays = 0;            }        }  }  public void makeCopy(Date otherDate)    {    dMonth = otherDate.dMonth;    dDay = otherDate.dDay;    dDay = otherDate.dDay;  }  public Date getCopy()    {        Date temp = new Date();        temp.dMonth = dMonth;        temp.dDay = dDay;        temp.dYear = dYear;        return temp;  }}

*************************************************************************************************************************************************************************

Given: the class Date thatcan prints the date in numerical form. Some applications mightrequire the date to be printed in another form, such as March 24,2005. Please do the following:

  1. Derive the class ExtDate so that the date can beprinted either form.
  2. Add a data member to the class ExtDate so that the month canalso be stored in string form. Add a method to output the month inthe string format followed by the year, for instance, in the formMarch 2005.
  3. Write the definition of the methods to implement the operationsfor the class ExtDate and write a test program to test yourprogram.

Answer & Explanation Solved by verified expert
3.7 Ratings (453 Votes)
Thanks for the questionHere is the completed code for this problem Comments are included go through it learn how things work and let me know if you have any doubts or if you need anything to change If you are satisfied with the solution please rate the answer ThanksDerive the class ExtDate so that the date can be printed either formpublic class ExtDate extends Date Add a data member to the class ExtDate so that the month can also be stored in string form private String monthName public ExtDate super    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students