Problem 1
Date class
Suppose you are given a class named Date with the following contents:
// A Date stores a month and day of the (non-leap) year.
public class Date {
private int month;
private int day;
// constructs a new Date with the given month/day
public Date(int m, int d)
// returns the fields' values
public int getMonth()
public int getDay()
public int daysInMonth()
public void nextDay()
public String toString()
}
1. Requirement 1: Date daysTillXmas
\textbullet Write an instance method daysTillXmas that will be placed inside the Date class. The
method returns how many days away the Date object is from Christmas, December 25, in
the same year. For example, Nov. 22 is 33 days away, Sep. 3 is 113 days away, Dec 25 is
0 days away, and Dec 31 is -6 days away.
\textbullet Here is an example call:
Date d = new Date(9,3);
System.out.println(d.daysTillXmas()); //113
\textbullet Write a Client program to test your solution.
2. Requirement 2: Date subtractWeeks
\textbullet Write an instance method subtractWeeks that will be placed inside the Date class. The
method accepts an integer parameter and shifts the date backward by that many weeks.
(A week is exactly 7 days.) The date before 1/1 is 12/31.
\textbullet Here are some example calls on a given date object:
Date d = new Date(9, 19);
d.subtractWeeks(1); // d is now 9/12
d.subtractWeeks(2); // d is now 8/29
Lab 1
\textbullet d.subtractWeeks(5); // d is now 7/25
\textbullet d.subtractWeeks(20); // d is now 3/7
\textbullet d.subtractWeeks(110); // d is now 1/26 (2 years prior)
\textbullet Write a Client program to test your solution.
COSC 241