logo Practice-It logo


Language/Type: Java classes instance methods
Author: Jason Ganzhorn

Write a method named subtractWeeks that will be placed in the Date class. The subtractWeeks method accepts an integer as a parameter and shifts the date represented by the Date object backward by that many weeks. A week is considered to be exactly 7 days. You may assume the value passed is non-negative. Note that subtracting weeks might cause the date to wrap into previous months or years.

For example, if the following Date is declared in client code:

Date d = new Date(9, 19);

The following calls to the subtractWeeks method would modify the Date object's state as indicated in the comments. Remember that Date objects do not store the year. The date before January 1st is December 31st. Date objects also ignore leap years.

Date d = new Date(9, 19);
d.subtractWeeks(1);    // d is now 9/12
d.subtractWeeks(2);    // d is now 8/29
d.subtractWeeks(5);    // d is now 7/25
d.subtractWeeks(20);   // d is now 3/7
d.subtractWeeks(110);  // d is now 1/26 (2 years prior)
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.