publicclass Date {
privatestatic String DATE_SEPARATOR = \"/\";
privatestaticintDAYS_PER_WEEK = 7;
//Attributes
privateint day;
privateint month;
privateint year;
/**
* Default Constructor
* Instantiates an object of type Date to 1/1/2000
*/
public Date() {
this.day = 1;
this.month = 1;
this.year = 2000;
}
/**
* Constructs a new date object to represent the given date.
* @param day
* @param month
* @param year
*/
public Date(int day, int month, int year) {
if(isValid(day, month, year)) {
this.day = day;
this.month = month;
this.year = year;
} else
System.out.println(\"Invalid Date.\");
}
/**
* Returns the day value of this date for example, for the date 2006/07/22, returns 22.
* @return
*/
publicint getDay() {
return day;
}
/**
* Returns the month value of this date ,for example, for the date 2006/07/22, returns 7.
* @return
*/
publicint getMonth() {
return month;
}
/**
* Returns the year value of this date, for example , the date 2006/07/22, returns 2006.
* @return
*/
publicint getYear() {
return year;
}
/**
* @param day the day to set
*/
publicvoid setDay(int day) {
this.day = day;
}
/**
* @param month the month to set
*/
publicvoid setMonth(int month) {
this.month = month;
}
/**
* @param year the year to set
*/
publicvoid setYear(int year) {
this.year = year;
}
/**
* Returns true if the year of this date is a leap year.
* A leap year occurs every 4 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.
* @return
*/
publicboolean isLeapYear(){
if(((this.year % 400) == 0) || (((this.year % 4) == 0) && ((this.year % 100) != 0)))
returntrue;
else
returnfalse;
}
/**
* Checks if the date is valid
* @param day
* @param month
* @param year
* @return
*/
publicboolean isValid(int day, int month, int year) {
if((month < 1) || (12 < month))
returnfalse;
else {
if(((month == 1) || (month == 3) || (month == 5) || (month == 7) ||
(month == 8) || (month == 10) || (month == 12)) && ((day < 1) || (31 < day)))
returnfalse;
elseif(((month == 4) || (month == 6) || (month == 9) || (month == 11)) && ((day < 1) || (30 <
day)))
returnfalse;
elseif(month == 2) {
if(isLeapYear() && ((day < 1) || (29 < day)))
returnfalse;
elseif((day < 1) || (28 < day))
returnfalse;
}
}
returntrue;
}
/**
* Returns the maximum number of days in a month
* @return
*/
publicint maxMonthDays() {
if(this.month == 2) {
if(isLeapYear())
return 29;
else
return 28;
} elseif((this.month == 1) || (this.month == 3) || (this.month == 5) || (this.month == 7) ||
(this.month == 8) || (this.month == 10) || (this.month == 12))
return 31;
else
return 30;
}
/**
* Checks if this dat is same as other date
* @param other
* @return
*/
publicboolean isEqual(Date other) {
if((this.day == other.day) && (this.month == other.month) && (this.year == other.year))
returntrue;
else
returnfalse;
}
/**
* Moves this Date object forward in time by the given number of days .
* @param days
*/
publicvoid addDays(int days).
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf
1. publicclass Date {
privatestatic String DATE_SEPARATOR = "/";
privatestaticintDAYS_PER_WEEK = 7;
//Attributes
privateint day;
privateint month;
privateint year;
/**
* Default Constructor
* Instantiates an object of type Date to 1/1/2000
*/
public Date() {
this.day = 1;
this.month = 1;
this.year = 2000;
}
/**
* Constructs a new date object to represent the given date.
* @param day
* @param month
* @param year
*/
public Date(int day, int month, int year) {
if(isValid(day, month, year)) {
this.day = day;
this.month = month;
this.year = year;
} else
System.out.println("Invalid Date.");
}
/**
* Returns the day value of this date for example, for the date 2006/07/22, returns 22.
* @return
*/
publicint getDay() {
2. return day;
}
/**
* Returns the month value of this date ,for example, for the date 2006/07/22, returns 7.
* @return
*/
publicint getMonth() {
return month;
}
/**
* Returns the year value of this date, for example , the date 2006/07/22, returns 2006.
* @return
*/
publicint getYear() {
return year;
}
/**
* @param day the day to set
*/
publicvoid setDay(int day) {
this.day = day;
}
/**
* @param month the month to set
*/
publicvoid setMonth(int month) {
this.month = month;
}
/**
* @param year the year to set
*/
publicvoid setYear(int year) {
this.year = year;
}
/**
* Returns true if the year of this date is a leap year.
3. * A leap year occurs every 4 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.
* @return
*/
publicboolean isLeapYear(){
if(((this.year % 400) == 0) || (((this.year % 4) == 0) && ((this.year % 100) != 0)))
returntrue;
else
returnfalse;
}
/**
* Checks if the date is valid
* @param day
* @param month
* @param year
* @return
*/
publicboolean isValid(int day, int month, int year) {
if((month < 1) || (12 < month))
returnfalse;
else {
if(((month == 1) || (month == 3) || (month == 5) || (month == 7) ||
(month == 8) || (month == 10) || (month == 12)) && ((day < 1) || (31 < day)))
returnfalse;
elseif(((month == 4) || (month == 6) || (month == 9) || (month == 11)) && ((day < 1) || (30 <
day)))
returnfalse;
elseif(month == 2) {
if(isLeapYear() && ((day < 1) || (29 < day)))
returnfalse;
elseif((day < 1) || (28 < day))
returnfalse;
}
}
returntrue;
}
4. /**
* Returns the maximum number of days in a month
* @return
*/
publicint maxMonthDays() {
if(this.month == 2) {
if(isLeapYear())
return 29;
else
return 28;
} elseif((this.month == 1) || (this.month == 3) || (this.month == 5) || (this.month == 7) ||
(this.month == 8) || (this.month == 10) || (this.month == 12))
return 31;
else
return 30;
}
/**
* Checks if this dat is same as other date
* @param other
* @return
*/
publicboolean isEqual(Date other) {
if((this.day == other.day) && (this.month == other.month) && (this.year == other.year))
returntrue;
else
returnfalse;
}
/**
* Moves this Date object forward in time by the given number of days .
* @param days
*/
publicvoid addDays(int days) {
while(days != 0){
int maxDays = maxMonthDays();
if((maxDays - this.day) >= days) {
this.day += days;
5. days = 0;
}
else{
days -= (maxDays - this.day);
this.day = 0;
if(month == 12) {
this.month = 1;
this.year += 1;
} else
this.month += 1;
}
}
}
/**
* Moves this date object forward in time by the given amount of seven day weeks
* @param weeks
*/
publicvoid addWeeks(int weeks) {
addDays(weeks * DAYS_PER_WEEK);
}
/**
* Returns the number of days that this Date must be adjusted to make it equal to the given other
Date.
* @param other
* @return
*/
publicint daysTo(Date other) {
Date temp = new Date(this.day, this.month, this.year);
if(temp.isEqual(other))
return 0;
else{
int noOfDays = 0;
do {
if((temp.year == other.year) && (temp.month == other.month)) {
noOfDays += (other.day - temp.day);
temp.day = other.day;
6. }
else{
int maxDays = temp.maxMonthDays();
noOfDays += (maxDays - temp.day);
temp.day = 0;
if(temp.month == 12) {
temp.month = 1;
temp.year += 1;
}
else
temp.month += 1;
}
} while(!temp.isEqual(other));
return noOfDays;
}
}
/**
* Returns a String representation of this date in year/month/day order, such as "2006/07/22"
*/
@Override
public String toString() {
returnthis.day + DATE_SEPARATOR + this.month + DATE_SEPARATOR + this.year;
}
}
publicclass Driver {
publicstaticvoid main(String[] args) {
Date d1 = new Date(29, 1, 2016);
Date d2 = new Date(29, 12, 2015);
//Add 5 days to d1
System.out.println(" Add 5 days to " + d1 + " : ");
d1.addDays(5);
System.out.println(d1);
//Add 5 days to d2
System.out.println(" Add 5 days to " + d2 + " : ");
d2.addDays(5);
System.out.println(d2);
7. Date d3 = new Date(28, 8, 2016);
//Add 1 week to d3
System.out.println(" Add 1 week to " + d3 + " : ");
d3.addWeeks(1);
System.out.println(d3);
Date d4 = new Date(31, 8, 2016);
Date d5 = new Date(1, 10, 2016);
System.out.println(" The number of days that " + d4 + " must be adjusted to make it equal to
" + d5 + " : " + d4.daysTo(d5) + " days.");
}
}
SAMPLE OUTPUT :
Add 5 days to 29/1/2016 :
3/2/2016
Add 5 days to 29/12/2015 :
3/1/2016
Add 1 week to 28/8/2016 :
4/9/2016
The number of days that 31/8/2016 must be adjusted to make it equal to 1/10/2016 : 31 days.
Solution
publicclass Date {
privatestatic String DATE_SEPARATOR = "/";
privatestaticintDAYS_PER_WEEK = 7;
//Attributes
privateint day;
privateint month;
privateint year;
/**
* Default Constructor
* Instantiates an object of type Date to 1/1/2000
*/
public Date() {
this.day = 1;
this.month = 1;
8. this.year = 2000;
}
/**
* Constructs a new date object to represent the given date.
* @param day
* @param month
* @param year
*/
public Date(int day, int month, int year) {
if(isValid(day, month, year)) {
this.day = day;
this.month = month;
this.year = year;
} else
System.out.println("Invalid Date.");
}
/**
* Returns the day value of this date for example, for the date 2006/07/22, returns 22.
* @return
*/
publicint getDay() {
return day;
}
/**
* Returns the month value of this date ,for example, for the date 2006/07/22, returns 7.
* @return
*/
publicint getMonth() {
return month;
}
/**
* Returns the year value of this date, for example , the date 2006/07/22, returns 2006.
* @return
*/
publicint getYear() {
return year;
9. }
/**
* @param day the day to set
*/
publicvoid setDay(int day) {
this.day = day;
}
/**
* @param month the month to set
*/
publicvoid setMonth(int month) {
this.month = month;
}
/**
* @param year the year to set
*/
publicvoid setYear(int year) {
this.year = year;
}
/**
* Returns true if the year of this date is a leap year.
* A leap year occurs every 4 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.
* @return
*/
publicboolean isLeapYear(){
if(((this.year % 400) == 0) || (((this.year % 4) == 0) && ((this.year % 100) != 0)))
returntrue;
else
returnfalse;
}
/**
* Checks if the date is valid
* @param day
* @param month
* @param year
11. }
/**
* Checks if this dat is same as other date
* @param other
* @return
*/
publicboolean isEqual(Date other) {
if((this.day == other.day) && (this.month == other.month) && (this.year == other.year))
returntrue;
else
returnfalse;
}
/**
* Moves this Date object forward in time by the given number of days .
* @param days
*/
publicvoid addDays(int days) {
while(days != 0){
int maxDays = maxMonthDays();
if((maxDays - this.day) >= days) {
this.day += days;
days = 0;
}
else{
days -= (maxDays - this.day);
this.day = 0;
if(month == 12) {
this.month = 1;
this.year += 1;
} else
this.month += 1;
}
}
}
/**
* Moves this date object forward in time by the given amount of seven day weeks
12. * @param weeks
*/
publicvoid addWeeks(int weeks) {
addDays(weeks * DAYS_PER_WEEK);
}
/**
* Returns the number of days that this Date must be adjusted to make it equal to the given other
Date.
* @param other
* @return
*/
publicint daysTo(Date other) {
Date temp = new Date(this.day, this.month, this.year);
if(temp.isEqual(other))
return 0;
else{
int noOfDays = 0;
do {
if((temp.year == other.year) && (temp.month == other.month)) {
noOfDays += (other.day - temp.day);
temp.day = other.day;
}
else{
int maxDays = temp.maxMonthDays();
noOfDays += (maxDays - temp.day);
temp.day = 0;
if(temp.month == 12) {
temp.month = 1;
temp.year += 1;
}
else
temp.month += 1;
}
} while(!temp.isEqual(other));
return noOfDays;
}
13. }
/**
* Returns a String representation of this date in year/month/day order, such as "2006/07/22"
*/
@Override
public String toString() {
returnthis.day + DATE_SEPARATOR + this.month + DATE_SEPARATOR + this.year;
}
}
publicclass Driver {
publicstaticvoid main(String[] args) {
Date d1 = new Date(29, 1, 2016);
Date d2 = new Date(29, 12, 2015);
//Add 5 days to d1
System.out.println(" Add 5 days to " + d1 + " : ");
d1.addDays(5);
System.out.println(d1);
//Add 5 days to d2
System.out.println(" Add 5 days to " + d2 + " : ");
d2.addDays(5);
System.out.println(d2);
Date d3 = new Date(28, 8, 2016);
//Add 1 week to d3
System.out.println(" Add 1 week to " + d3 + " : ");
d3.addWeeks(1);
System.out.println(d3);
Date d4 = new Date(31, 8, 2016);
Date d5 = new Date(1, 10, 2016);
System.out.println(" The number of days that " + d4 + " must be adjusted to make it equal to
" + d5 + " : " + d4.daysTo(d5) + " days.");
}
}
SAMPLE OUTPUT :
Add 5 days to 29/1/2016 :
3/2/2016
Add 5 days to 29/12/2015 :
14. 3/1/2016
Add 1 week to 28/8/2016 :
4/9/2016
The number of days that 31/8/2016 must be adjusted to make it equal to 1/10/2016 : 31 days.