Java Class Date Purpose: To Practice designing a class that can be used in many
ID: 665811 • Letter: J
Question
Java Class Date
Purpose: To Practice designing a class that can be used in many applications. To Write and implement a thorough test plan. You are NOT to use any of the Java API date classes (Gregorian calendar, Date...)
Write a class to hold data and perform operations on dates. Ie: January 2, 2015 or 3/15/2015
On Paper, design the cass to hold a valid date. First Decide what fields you will need.
Then decide what operation you will perform on the fields, what functionality the class Date will have.
You must include a toString(), an equals() and compareTo() method.
Methods required in addition to the constructor methods, the toString(), equals(), and compareTo() methods.
Public Date add(int numDays)
Public Date subtract(int numDays)
Public int daysBetween(Date anotherDate)
Public String getDate(char format)
Please Help!
Explanation / Answer
As per question, date class is being created
public class Date
{
private int day;
private int month;
private int year;
public Date(int day, int month, int year)
{
this.day = day;
this.month = month;
this.year = year;
}
public string toString()
{
return (this.day + "-" + this.month + "-" + this.year);
}
public bool equals(Date date)
{
return (this.day == date.day &&
this.month == date.month &&
this.year == date.year);
}
public bool equals(Date date, Date date1)
{
return (date.day == date1.day &&
date.month == date1.month &&
date.year == date1.year);
}
public Date add(int numDays)
{
var tempDays = this.GetDaysInCurrentMonth();
while (numDays > tempDays)
{
this.month = this.GetNextMonth();
if (this.month == 1)//Again got Jan, year shouls be incremeneted
{
this.year = this.year + 1;
}
tempDays = this.GetDaysInCurrentMonth();
numDays = numDays - tempDays;
}
this.day = numDays;
return this;
}
public int daysBetween(Date anotherDate)
{
if(this.year > anotherDate.year)
{
return (this.year - anotherDate.year) * 365;//in year 365 days are there
}
else if(this.day > anotherDate.day)
{
return this.day - anotherDate.day;
}
return this.day - anotherDate.day;
}
public string getDate(char format)
{
if(format == 's')
{
return this.day + "//" + this.month + "//" + this.year;
}
else if(format == 'l')
{
return this.day + "of " + this.month + ", " + this.year;
}
return this.day + "//" + this.month + "//" + this.year;
}
public Date subtract(int numDays)
{
var tempDays = this.GetDaysInCurrentMonth();
while (numDays > tempDays)
{
this.month = this.GetPrevMonth();
if (this.month == 12)//Again got Jan, year shouls be incremeneted
{
this.year = this.year - 1;
}
tempDays = this.GetDaysInCurrentMonth();
numDays = numDays - tempDays;
}
this.day = this.GetDaysInCurrentMonth() - numDays;
return this;
}
private int GetDaysInCurrentMonth()
{
int noOfDaysInMonth = -1;
switch(this.month)
{
case 1://Jan
noOfDaysInMonth = 31;
break;
case 2://Feb
if(this.year % 400 == 0)//If leapyear
{
noOfDaysInMonth = 29;
}
else
{
noOfDaysInMonth = 28;
}
break;
case 3://March
noOfDaysInMonth = 31;
break;
case 4://April
noOfDaysInMonth = 30;
break;
case 5://May
noOfDaysInMonth = 30;
break;
case 6://Jun
noOfDaysInMonth = 30;
break;
case 7://Jul
noOfDaysInMonth = 31;
break;
case 8://Aug
noOfDaysInMonth = 31;
break;
case 9://Sept
noOfDaysInMonth = 30;
break;
case 10://Oct
noOfDaysInMonth = 31;
break;
case 11://Nov
noOfDaysInMonth = 30;
break;
case 12://Dec
noOfDaysInMonth = 31;
break;
}
return noOfDaysInMonth;
}
private int GetNextMonth()
{
int nextMonth = -1;
switch(this.month)
{
case 1:
nextMonth = 2;
break;
case 2:
nextMonth = 3;
break;
case 3:
nextMonth = 4;
break;
case 4:
nextMonth = 5;
break;
case 5:
nextMonth = 6;
break;
case 6:
nextMonth = 7;
break;
case 7:
nextMonth = 8;
break;
case 8:
nextMonth = 9;
break;
case 9:
nextMonth = 10;
break;
case 11:
nextMonth = 12;
break;
case 12:
nextMonth = 1;
break;
}
return nextMonth;
}
private int GetPrevMonth()
{
int nextMonth = -1;
switch (this.month)
{
case 1:
nextMonth = 12;
break;
case 2:
nextMonth = 1;
break;
case 3:
nextMonth = 2;
break;
case 4:
nextMonth = 3;
break;
case 5:
nextMonth = 4;
break;
case 6:
nextMonth = 5;
break;
case 7:
nextMonth = 6;
break;
case 8:
nextMonth = 7;
break;
case 9:
nextMonth = 8;
break;
case 11:
nextMonth = 10;
break;
case 12:
nextMonth = 11;
break;
}
return nextMonth;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.