You will create a Java class named Date that is used to work with date operation
ID: 3553929 • Letter: Y
Question
You will create a Java class named Date that is used to work with date operations. Then you will write a Driver to test each method you wrote in the Dare class and display, on the screen, a clear explanation and result of each test.
For documentation, use inline comments AND javadoc comments above each class and above each method. Use the tags : @author, @version, @param, @return
Mandatory methods for the Date class:
- At least three constructor methods, including the default constructor and the copy constructor.
- A toString( ) method.
- An equals( ) method.
- A compareTo( ) method.
- an add( ) method that adds an integer number of days to an existing Date and returns the new Date object:
Date myDate = new Date(3, 20, 2014);
Date result;
result = Date.add(15);
System.out.println(result);
- A subtract( ) method that subtracts an integer number of days from an exisiting Date and returns a new Date object.
Date myDate = new Date(3, 20, 2014);
Date result;
result = myDate.subtract(5);
System.out.println(result);
A dateDiff( ) methods that calculates and returns the number of days between the parameters and the Date object.
Date dateOne = new Date(3,15, 2014);
Date dateTwo = new Date(4, 18, 2014);
int difference;
difference = dateOne.difference(dateTwo);
System.out.println(
Explanation / Answer
Sample Output:
Testing add() method: 04/04/2014
Testing subtract() method: 03/15/2014
The difference between 03/15/2014 and 04/18/2014 is 33 days
-----------------------------------------------------------------------------------------------------------------
// Date.java
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Date
{
private int month;
private int day;
private int year;
private int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// default constructor
public Date()
{
// take the default date as 01/01/2014
month = 1;
day = 1;
year = 2014;
}
// parameterized constructor
public Date(int mm, int dd, int yyyy)
{
month = mm;
day = dd;
year = yyyy;
}
// copy constructor
public Date(Date date)
{
month = date.month;
day = date.day;
year = date.year;
}
// toString() method
public String toString()
{
String result = "";
if(month < 10)
result += "0" + month + "/";
else
result += month + "/";
if(day < 10)
result += "0" + day + "/";
else
result += day + "/";
result += year;
return result;
}
// equals() method
public boolean equals(Date date)
{
if(date == null)
return false;
else if(year != date.year || month != date.month || day != date.day)
return false;
else
return true;
}
// compareTo() method
public int compareTo(Date date)
{
if(year < date.year)
return -1;
else if(month < date.month)
return -1;
else if(day < date.day)
return -1;
else if(year == date.year && month == date.month && day == date.day)
return 0;
else
return 1;
}
// add() method
public Date add(int daysToAdd)
{
if(month == 2 && isLeapYear(year) && (day + daysToAdd) <= (days[month] + 1))
return new Date(month, (day + daysToAdd), year);
else if((day + daysToAdd) <= days[month])
return new Date(month, (day + daysToAdd), year);
else if(month == 2 && isLeapYear(year))
return new Date(month + 1, (day + daysToAdd) - (days[month] + 1), year);
else if(month < 12)
return new Date(month + 1, (day + daysToAdd) - days[month], year);
else
return new Date(1, (day + daysToAdd) - days[month], year + 1);
}
// subtract() method
public Date subtract(int daysToSubtract)
{
if((day - daysToSubtract) > 0)
return new Date(month, (day - daysToSubtract), year);
else if(month == 3 && isLeapYear(year) && (day - daysToSubtract) <= 0)
return new Date(month - 1, (days[month - 1] + 1 - (daysToSubtract - day)), year);
else if(month > 1)
return new Date(month - 1, (days[month - 1] - (daysToSubtract - day)), year);
else
return new Date(12, (days[month] - (daysToSubtract - day)), year - 1);
}
private boolean isLeapYear(int yyyy)
{
if(yyyy % 400 == 0)
return true;
else if(yyyy % 100 == 0)
return false;
else if(yyyy % 4 == 0)
return true;
else
return false;
}
// difference method
public int difference(Date date)
{
Calendar firstDate = new GregorianCalendar(year, month, day);
Calendar secondDate = new GregorianCalendar(date.year, date.month, date.day);
return (int)((secondDate.getTimeInMillis() - firstDate.getTimeInMillis()) / (1000 * 60 * 60 * 24));
}
}
------------------------------------------------------------------------------------------------------------------------------------------
// DateDriver.java
public class DateDriver
{
public static void main(String[] args)
{
Date myDate = new Date(3, 20, 2014);
Date result;
result = myDate.add(15);
System.out.println("Testing add() method: " + result);
result = myDate.subtract(5);
System.out.println("Testing subtract() method: " + result);
Date dateOne = new Date(3, 15, 2014);
Date dateTwo = new Date(4, 18, 2014);
int difference;
difference = dateOne.difference(dateTwo);
System.out.println("The difference between " + dateOne + " and " + dateTwo + " is " + difference + " days");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.