Question

In: Computer Science

Given the class Date that can prints the date in numerical form. Some applications might require...

Given the class Date that can prints the date in numerical form. Some applications might require the date to be printed in another form, such as October 1, 2020. Please do the following:

  1. Design the child class ExtDate based on the class Date (as a parent class) so that the date can be printed either form.
  2. Add a data member to the class ExtDate so that the month can also be stored in string form. Add a method to output the month in the string format followed by the year, for instance, in the form March 2005.
  3. Write the definition of the methods to implement the operations for the class ExtDate and write a test program to test your program.

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;
}
}

Solutions

Expert Solution

Short Summary:

  • Provided the source code and sample output as per the requirements.
  • Since you have given the Date class, I have the given you have other classes.

Source Code:

ExtDate.java:

/**
*
* ExtDate is the child class extends from the Date class
*
*/
public class ExtDate extends Date {
   // Data member of the ExtDate to store the month name
   public String month_string;

   // Default constructor
   public ExtDate() {
       super();
   }

   // Parameterized constructor
   public ExtDate(int month, int day, int year) {
       super(month, day, year);
   }

   // function that finds the month name
   public void assign_string_month() {
       // checking for the month name and storing it in a variable
       switch (super.getMonth()) {
           case 1:
               month_string = "January";
               break;
           case 2:
               month_string = "February";
               break;
           case 3:
               month_string = "March";
               break;
           case 4:
               month_string = "April";
               break;
           case 5:
               month_string = "May";
               break;
           case 6:
               month_string = "June";
               break;
           case 7:
               month_string = "July";
               break;
           case 8:
               month_string = "August";
               break;
           case 9:
               month_string = "September";
               break;
           case 10:
               month_string = "October";
               break;
           case 11:
               month_string = "November";
               break;
           case 12:
               month_string = "December";
               break;
       }
   }

   @Override
   public String toString() {
       // return the month name with the year
       return month_string + " " + super.getYear();
   }

}

DateDemo.java:

/**
*
* Main function
*
*/
public class DateDemo {
   public static void main(String[] args) {
       // creating the instance for child class
       ExtDate extDate = new ExtDate(3,11,2005);
       // calling the method of child class
       extDate.assign_string_month();
       // printing the result
       System.out.print(extDate.toString());
   }
  
}

Sample Run:

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

Feel free to rate the answer and comment your questions, if you have any.

Please upvote the answer and appreciate our time.

Happy Studying!!!

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


Related Solutions

In matlab.write a program that converts a numerical date given by the user into a month,...
In matlab.write a program that converts a numerical date given by the user into a month, day, and year. Specifically, the user should input a sequence of eight numbers 05141956. the program should print out the same date in complete format. In this example it would print "The date is May 14th, 1956". Notice the "th" addition after the day. This changes depending on the number. Make sure this feature is included. MATLAB has built-in date conversion functions. You cannot...
Write a C program, char.c, which prints the numerical value of the special character constants given...
Write a C program, char.c, which prints the numerical value of the special character constants given below. Don’t just look up the codes and print them directly from your program. You should have your program output the values of the character constants by using them as string literals within your printf() statements. Your output should be presented in a neat, orderly, tabular format as shown below: Char Constant Description Value '\n' newline '\t' horizontal tab '\v' vertical tab '\b' backspace...
Require some hint to approach the problem How can we identifying whether or not the given...
Require some hint to approach the problem How can we identifying whether or not the given measurements of maxima and minima belonged to a single slit or double slit experiment ? How a magnet behaved in earths magnetic field ? What formula should be used to dentify the charge of a mystery material?(electrostatics)
List procedures or treatments that require a consent form. As many as you can.
List procedures or treatments that require a consent form. As many as you can.
I require some assistance with the following assignment as I belive I might be doing it...
I require some assistance with the following assignment as I belive I might be doing it wrong especially in step 2. Your assistance is much appreciated. Linux Shell Scripting Practice: Note: The following excercise requires the use of the nano text editor. Begin by starting a script called “m5a2_part2b” by issuing the following at the command in a open terminal: $ script m5a2_part2b Step 1: Using the nano text editor, edit the following “Hello World” program, save as hello.sh, change...
Can you explain "Applications of synchrotron radiation" basically? And please give some examples about this applications.
Can you explain "Applications of synchrotron radiation" basically? And please give some examples about this applications.
side note: these are checkboxes so multiple answers can be chosen In what everyday applications might...
side note: these are checkboxes so multiple answers can be chosen In what everyday applications might some of the geometric problems discussed above (such as finding the volume of a frustum of a pyramid) be useful? A. Trading B. The temples called ziggurats C. System of canals for irrigation D. Eating
What are some lean applications you can use in a medical facility? Explain…
What are some lean applications you can use in a medical facility? Explain…
Phosphoric acid, H3PO4, has some important applications. It can be used to produce fertilizers and it...
Phosphoric acid, H3PO4, has some important applications. It can be used to produce fertilizers and it is present in soft drinks. Phosphoric acid can be made from phosphorus in a two step process: Reaction 1: P4+ 5O2 → P2O10 Reaction 2: P4H10 + 6H2O → 4H3PO4 1. What is the molar mass of phosphoric acid, H3 PO4? Report your answer to 3 significant figures. 2. How many moles of H3PO4 can be produced from one mole of P4? 3. A...
Buchanan (1965) provides us with a reference for why clubs might form and how they can...
Buchanan (1965) provides us with a reference for why clubs might form and how they can provide goods that are on the public-private scale. Does this mean governments are simply clubs? Why or why not? Is WVU a club or government? How does the public-ness or private-ness of goods provided potentially relate to your answers?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT