logo Practice-It logo

daysTillXmas

Language/Type: Java classes instance methods
Author: Brad Goring

Write a method named daysTillXmas that will be placed in the Date class. The daysTillXmas method returns how many days away the Date object is from Christmas, December 25, in the same year. For example:

  • Dec. 12 is 13 days away from Christmas.
  • Nov. 22 is 33 days away (the 8 remaining days in November, and 25 more to reach 12/25).
  • Sep. 3 is 113 days away (the 27 remaining days in September, the 61 days in October and November, and 25 more to reach 12/25).

Here is an example call of your method that would print 113:


Date d = new Date(9, 3);
System.out.println(d.daysTillXmas());   // 113
        

Here are more dates and the values that the method should return when called on Date objects representing them. 12/25 is 0 days away from itself, and days after 12/25 return a negative value.

  • Dec. 25 is 0 days away from Christmas.
  • Dec. 26 is -1 days away from Christmas.
  • Dec. 31 is -6 days away from Christmas.
  • Jan. 1 is 358 days away from Christmas.
  • Feb. 14 is 314 days away from Christmas.

Your method should not modify the Date object's state, such as by changing its day or month field values.

Type your solution here:


This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.