spenceapalooza
Newbie
Joined: 06/23/2009 11:22:23
Messages: 1
Offline
|
// Save contents of this file as Date.Java .
// Default treatment of disallowed values is to change disallowed date to 1(st of month), disallowed month to January, disallowed year to 1900.
public class Date
{
private int dMonth; //variable to store the month
private int dDay; //variable to store the day
private int dYear; //variable to store the year
//Method to set the date
//Data members dMonth, dDay, and dYear are set
//according to the parameters
//Postcondition: dMonth = month; dDay = day;
// dYear = year;
public void setDate(int month, int day, int year)
{
if(year >= 1)
dYear = year;
else
dYear = 1900; //default value
// Insert code here to use the month provided if it is valid, otherwise default to January (dmonth = 1).
// Checking that the date is allowable for the month:
switch(dMonth)
{
case 1: case 3: case 5: case 7:
case 8: case 10: case 12: if(1 <= day && day <= 31)
dDay = day;
else
dDay = 1;
break;
case 4: case 6: case 9: case 11:
if(1 <= day && day <= 30)
dDay = day;
else
dDay=1;
break;
case 2: if(isLeapYear())
{
if(Year%4=0)
if(1 <= day && day >=29)
dDay = day;
}
else
{
if(1 <= day && day <= 2
dDay= day;;
break;
}
}
}
//Method to return the month
//Postcondition: The value of dMonth is returned
public int getMonth()
{
return dMonth;
}
//Method to return the day
//Postcondition: The value of dDay is returned
public int getDay()
{
return dDay;
}
//Method to return the year
//Postcondition: The value of dYear is returned
public int getYear()
{
return dYear;
}
//Method to return the date in the form mm-dd-yyyy
public String toString()
{
return (dMonth + "-" + dDay + "-" + dYear);
}
//Constructor to set the date
//Data members dMonth, dDay, and dYear are set
//according to the parameters
//Postcondition: dMonth = month; dDay = day;
// dYear = year;
public Date(int month, int day, int year)
{
setDate(month, day, year);
}
//Default constructor
//Data members dMonth, dDay, and dYear are set to
//the default values
//Postcondition: dMonth = 1; dDay = 1; dYear = 1900;
public Date()
{
dMonth = 1;
dDay = 1;
dYear = 1900;
}
public boolean isLeapYear()
{
// Insert LeapYear test logic here.
}
}
i cant figure out how to make the leap year logic work... and also how do i make a test at the bottom?
|