logo Practice-It logo


Language/Type: Java classes instance methods
Author: Marty Stepp

Write a instance method named bound that will be placed inside the Date class. Your method will be placed inside the Date class to become a part of each Date object's behavior. The bound method constrains a Date to within a given range of dates. It accepts two other Date objects d1 and d2 as parameters; d1's date is guaranteed to represent a date that comes no later in the year than d2's date.

The bound method makes sure that this Date object is between d1's and d2's dates, inclusive. If this Date object is not between those dates inclusive, it is adjusted to the nearest date in the acceptable range. The method returns a result of true if this Date was within the acceptable range, or false if it was shifted.

For example, given the following Date objects:

Date date1 = new Date(7, 12);
Date date2 = new Date(10, 31);
Date date3 = new Date(9, 19);
Date bound1 = new Date(8, 4);
Date bound2 = new Date(9, 26);
Date bound3 = new Date(12, 25);

The following calls to your method should adjust the given Date objects to represent the following dates and should return the following results:

call date becomes returns
date1.bound(bound1, bound2) 8/4 false
date2.bound(bound1, bound2) 9/26 false
date3.bound(bound1, bound3) 9/19 true
date2.bound(bound3, bound3) 12/25 false
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.