SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Classes
Exercise:
Programming Projects
Question:2 | ISBN:9780136091813 | Edition: 2

Question

Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the
following methods:
public Date(int year, int month, int day)
Constructs a new Date object to represent the given date.
public void addDays(int days)
Moves this Date object forward in time by the given number of days.
public void addWeeks(int weeks)
Moves this Date object forward in time by the given number of seven-day weeks.

public int daysTo(Date other)
Returns the number of days that this Date must be adjusted to make it equal to the given other Date.
public int getDay()
Returns the day value of this date; for example, for the date 2006/07/22, returns 22.
public int getMonth()
Returns the month value of this date; for example, for the date 2006/07/22, returns 7.
public int getYear()
Returns the year value of this date; for example, for the date 2006/07/22, returns 2006.
public boolean isLeapYear()

Returns true if the year of this date is a leap year. A leap year occurs every four years, except for multiples of 100
that are not multiples of 400. For example, 1956, 1844, 1600, and 2000 are leap years, but 1983, 2002, 1700, and
1900 are not.
public String toString()
Returns a String representation of this date in year/month/day order, such as "2006/07/22".

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

public class Date
{
	private int year;
	private int month;
	private int day;

	public Date(int year, int month, int day)
	{
		if (year < 0 || month < 0 || day < 0)
		{
			throw new IllegalArgumentException();
		}

		this.year = year;
		this.month = month;
		this.day = day;
	}

	public void addDays(int days)
	{
		if (days < 0)
		{
			throw new IllegalArgumentException();
		}

		while (days > 0)
		{
			if (day == 28 && month == 2 && !isLeapYear())
			{
				day = 1;
				month++;
			}
			else if (day == 29 && month == 2 && isLeapYear())
			{
				day = 1;
				month++;
			}
			else if (day == 30 && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11))
			{
				day = 1;
				month++;
			}
			else if (day == 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10
					|| month == 12))
			{
				day = 1;
				month++;
			}
			else
			{
				day++;
			}

			if (month == 13)
			{
				month = 1;
				year++;
			}

			days--;
		}
	}

	public void addWeeks(int weeks)
	{
		addDays(weeks * 7);
	}

	public int daysTo(Date other)
	{
		if ((this.year > other.year) || (this.year == other.year && this.month > other.month)
				|| (this.year == other.year && this.month == other.month && this.day >= other.day))
		{
			return 0;
		}

		int result = 0;
		Date currDate = new Date(this.year, this.month, this.day);
		while(currDate.year != other.year || currDate.month != other.month || currDate.day != other.day)
		{
			currDate.addDays(1);			
			result++;
		}

		return result;
	}

	public int getDay()
	{
		return day;
	}

	public int getMonth()
	{
		return month;
	}

	public int getYear()
	{
		return year;
	}

	public boolean isLeapYear()
	{
		if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public String toString()
	{
		String strDate = "";

		strDate += year;
		strDate += "/";

		if (month < 10)
			strDate += "0";
		strDate += month;
		strDate += "/";

		if (day < 10)
			strDate += "0";
		strDate += day;

		return strDate;
	}
}

 

0 0

Discussions

Post the discussion to improve the above solution.