On BlueJ create a Java programm (Attached Files: File newDateExample.zip (3.68 K
ID: 3590908 • Letter: O
Question
On BlueJ create a Java programm (Attached Files: File newDateExample.zip (3.68 KB) Complete the Date class (provided in the newDateExample project provided as follows: The Date() constructor is completed. You do not need to modify. Modify the Date(int day, int month, int year) so that it works as explained in the comments. You will need to use the this keyword. The setDate method is completed. The equals method is completed. Complete the lessThan method so that it checks to see if one Date object is less than another. Here you must compare the dates using their fields. The notEquals method is completed. Complete the greaterThan, greaterThanOrEqual, and lessThanOrEqual methods by making internal method calls only. You may not compare any fields directly (See the notEquals method). Complete the isValidMonth method so that it returns true if the month provided is an integer from 1 to 12. Complete the isValidDay method do that it returns true if the day is valid for the given month. For your first try, assume that February always has 28 days. Once you get the method to work, add a check so that it allows the day for February to be 29 in a leap year.)
This is what's in File newDateExample.zip (3.68 KB)
[
/**
* Class to store a date as three intgers: day, month and year.
*
* @author
* @version
*/
public class Date
{
// instance variables - replace the example below with your own
private int day;
private int month;
private int year;
/**
* Constructor for objects of class Date to intitialize each member to 0
*/
public Date()
{
// initializee instance variables
}
/**
* Constructor for objects of class Date to intitialize each member to the specified parameters
* @param day the value to which the day field will be set
* @param month the value to which the month field will be set
* @param year the value to which the year field will be set
*/
public Date(int day, int month, int year)
{
// initialize instance variables
}
/**
* method to set the Date to the specified parameters
*
* @param d the value to which the day field will be set
* @param m the value to which the month field will be set
* @param y the value to which the year field will be set
*/
public void setDate(int d, int m, int y)
{
if (isValidMonth(m))
{
if (isValidDay(d,m,y))
{
day = d;
month = m;
year = y;
}
else
{
System.out.println("The day " + d + " is not valid for the month " + m);
System.out.println("The date has not been changed");
}
}
else
{
System.out.println("The month must be an integer from 1 to 12");
System.out.println("The date has not been changed");
}
}
/**
* method to determine if two Date objects are equal
*
* @param theDate: the Date object to compare
* @return true if this date is equal to theDate and false otherwise
*/
public boolean equals(Date theDate)
{
return ((day == theDate.day) && (month == theDate.month) && (year == theDate.year));
}
/**
* method to determine if one Date object is less than another
*
* @param theDate: the Date object to compare
* @return true if this date is less than theDate and false otherwise
*/
public boolean lessThan(Date theDate)
{
return true;
}
/**
* method to determine if two Date objects are not equal
*
* @param theDate: the Date object to compare
* @return true if this date is not equal to theDate and false otherwise
*/
public boolean notEquals(Date theDate)
{
return (!(this.equals(theDate)));
}
/**
* method to determine if one date object is greater than or equal to another
*
* @param theDate: the Date object to compare
* @return true if this date is greater than or equal to theDate and false otherwise
*/
public boolean greaterThanOrEqual(Date theDate)
{
return true;
}
/**
* method to determine if one Date object is greater than another
*
* @param theDate: the Date object to compare
* @return true if this date is greater than theDate and false otherwise
*/
public boolean greaterThan(Date theDate)
{
return true;
}
/**
* method to determine if one date object is less than or equal to another
*
* @param theDate: the Date object to compare
* @return true if this date is less than or equal to theDate and false otherwise
*/
public boolean lessThanOrEqual(Date theDate)
{
return true;
}
/**
* method to determine if a given month value is a valid value (1-12)
*
* @param theMonth: the value supplied for the month
* @return true if the month is an integer from 1 to 12
*/
private boolean isValidMonth(int theMonth)
{
return true;
}
/**
* method to determine if a given day value is a valid value (1-12)
*
* @param theday: the value supplied for the day
* @param theMonth: the value supplied for the month
* @return true if the day is a valid integer for the given month
*/
private boolean isValidDay(int theDay,int theMonth, int theYear)
{
return true;
}
}
]
Explanation / Answer
Hi,
i have filled in all the functions and added comments everywhere, here is the code,
public class Date
{
// instance variables - replace the example below with your own
private int day;
private int month;
private int year;
/**
* Constructor for objects of class Date to intitialize each member to 0
*/
public Date()
{
this.day=0;//setting all variables to 0 as function defines it
this.month=0;
this.year=0;
// initializee instance variables
}
/**
* Constructor for objects of class Date to intitialize each member to the specified parameters
* @param day the value to which the day field will be set
* @param month the value to which the month field will be set
* @param year the value to which the year field will be set
*/
public Date(int day, int month, int year)
{
this.day=day;//setting variables to function params given
this.month=month;
this.year=year;
// initialize instance variables
}
/**
* method to set the Date to the specified parameters
*
* @param d the value to which the day field will be set
* @param m the value to which the month field will be set
* @param y the value to which the year field will be set
*/
public void setDate(int d, int m, int y)
{
if (isValidMonth(m))
{
if (isValidDay(d,m,y))
{
day = d;
month = m;
year = y;
}
else
{
System.out.println("The day " + d + " is not valid for the month " + m);
System.out.println("The date has not been changed");
}
}
else
{
System.out.println("The month must be an integer from 1 to 12");
System.out.println("The date has not been changed");
}
}
/**
* method to determine if two Date objects are equal
*
* @param theDate: the Date object to compare
* @return true if this date is equal to theDate and false otherwise
*/
public boolean equals(Date theDate)
{
return ((day == theDate.day) && (month == theDate.month) && (year == theDate.year));
}
/**
* method to determine if one Date object is less than another
*
* @param theDate: the Date object to compare
* @return true if this date is less than theDate and false otherwise
*/
public boolean lessThan(Date theDate)
{
if(year>=theDate.year && month>=theDate.month && day>theDate.day)
{//if year,month and day are less than given data
return true;
}
return false;
}
/**
* method to determine if two Date objects are not equal
*
* @param theDate: the Date object to compare
* @return true if this date is not equal to theDate and false otherwise
*/
public boolean notEquals(Date theDate)
{
return (!(this.equals(theDate)));
}
/**
* method to determine if one date object is greater than or equal to another
*
* @param theDate: the Date object to compare
* @return true if this date is greater than or equal to theDate and false otherwise
*/
public boolean greaterThanOrEqual(Date theDate)
{
if(year<=theDate.year && month<=theDate.month && day<=theDate.day)
{//if year,month and day are greater than equal to given data
return true;
}
return false;
}
/**
* method to determine if one Date object is greater than another
*
* @param theDate: the Date object to compare
* @return true if this date is greater than theDate and false otherwise
*/
public boolean greaterThan(Date theDate)
{
if(year<=theDate.year && month<=theDate.month && day<theDate.day)
{//if year,month and day are greater than given data
return true;
}
return false;
}
/**
* method to determine if one date object is less than or equal to another
*
* @param theDate: the Date object to compare
* @return true if this date is less than or equal to theDate and false otherwise
*/
public boolean lessThanOrEqual(Date theDate)
{
if(year>=theDate.year && month>=theDate.month && day>=theDate.day)
{//if year,month and day are less than equal to given data
return true;
}
return false;
}
/**
* method to determine if a given month value is a valid value (1-12)
*
* @param theMonth: the value supplied for the month
* @return true if the month is an integer from 1 to 12
*/
private boolean isValidMonth(int theMonth)
{
if(theMonth>=1 && theMonth<=12)//if month is in range 1-12
return true;
return false;
}
/**
* method to determine if a given day value is a valid value (1-12)
*
* @param theday: the value supplied for the day
* @param theMonth: the value supplied for the month
* @return true if the day is a valid integer for the given month
*/
private boolean isValidDay(int theDay,int theMonth, int theYear)
{
if ((thetheMonth == 1 || thetheMonth == 3 || thetheMonth == 5 || theMonth == 7 ||
theMonth == 8 || theMonth == 10 || theMonth == 12) && ( theDay>31 || theDay<1) )
{//if a month with 31 days
return false;
}
else if ((theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11) && (theYear>30 || theYear<1) )
{//if a month with 30 days
return false;
}
else if ((theMonth == 2) && (theYear % 4 == 0) && (theYear>29 || theYear<1))
{//if a leap year and feb month has 29 days
return false;
}
else if ((theMonth == 2) && (theYear % 4 != 0) && (theYear>28 || theYear<1))
{
//if NOT a leap year and feb month has 28 days
return false;
}
return true;
}
}
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.