Need comments added please. public class TenDate { private int NMnth; private in
ID: 3665894 • Letter: N
Question
Need comments added please.
public class TenDate
{
private int NMnth;
private int NDy;
private int NYr;
private String [] MnthNm =
{"", "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
private int [] MnthD =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public TenDate()
{
NMnth = 1;
NDy = 1;
NYr = 1900;
}
public TenDate(int Mnth, int Dy, int Yr)
{
NMnth = Mnth;
NDy = Dy;
NYr = Yr;
}
public void SetDate(int Mnth, int Dy, int Yr)
{
NMnth = Mnth;
NDy = Dy;
NYr = Yr;
}
public int GetMonth()
{
return NMnth;
}
public void SetMonth(int IMnth)
{
if(IMnth > 0 && IMnth < 13)
NMnth = IMnth;
else
NMnth= 1;
}
public int GetDay()
{
return NDy;
}
public int GetYear()
{
return NYr;
}
public void SetDay(int IDy)
{
if(IDy > 0 && IDy <= MnthD[NMnth])
NDy= IDy;
if(NMnth == 2 && IDy == 29 && LeapYr())
NDy= IDy;
else
NDy = 1;
}
public void SetYear(int IYr)
{
NYr = IYr;
}
public boolean LeapYr()
{
if((NYr % 400 == 0) || (NYr % 4 == 0 && NYr % 100 != 0))
return true;
else
return false;
}
public String DateString()
{
return(NMnth + "-" + NDy + "-" + NYr);
}
public int DyInMnth()
{
if(LeapYr() && NMnth == 2)
return MnthD[NMnth] + 1;
else
return MnthD[NMnth];
}
public int ConvDOY()
{
int DOY = 0;
for(int i = 1; i < NMnth; i++)
{
if(LeapYr() && i == 2)
DOY += 29;
else
DOY += MnthD[i];
}
DOY += NDy;
return DOY;
}
public int DyLft()
{
return 365 - ConvDOY();
}
public void NewDy(int Days)
{
int DLoop = Days / 30 + 3;
int j, DoneDy = 0, DyInMnth, MinusDy;
for(j = 1; j <= DLoop; j++)
{
if(DoneDy == 1)
return;
switch(NMnth)
{
case 9:
case 4:
case 6:
case 11: DyInMnth = 30;
break;
case 2: DyInMnth = 28;
if(NYr % 4 == 0)
{
if(NYr % 100 != 0)
DyInMnth = 29;
else
if(NYr % 400 == 0)
DyInMnth = 29;
}
break;
default: DyInMnth = 31;
}
MinusDy = DyInMnth - NDy;
if(Days > MinusDy)
{
NMnth =++ NMnth % 13;
if(NMnth == 0)
{
NYr++;
NMnth++;
}
Days -= MinusDy;
NDy = 0;
}
else
{
NDy += Days;
DoneDy = 1;
}
}
}
}
Explanation / Answer
public class TenDate
{
// declaring instance variables
private int NMnth;
private int NDy;
private int NYr;
// declaring and initializing MnthNm array(names of all the months)
private String [] MnthNm =
{"", "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
// declaring amd initializing MnthD array(Days each month has)
private int [] MnthD =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// constructor with no arguments to initialize month, day and year to 1-1-1900
public TenDate()
{
NMnth = 1;
NDy = 1;
NYr = 1900;
}
// constructor to set month, day, year to argument values
public TenDate(int Mnth, int Dy, int Yr)
{
NMnth = Mnth;
NDy = Dy;
NYr = Yr;
}
// this method takes three arguments Mnth, Dy, and Yr to set it's instance variables
public void SetDate(int Mnth, int Dy, int Yr)
{
NMnth = Mnth;
NDy = Dy;
NYr = Yr;
}
// GetMonth Method returns month(number)
public int GetMonth()
{
return NMnth;
}
// SetMonth sets the month with the input argument(parameter)
public void SetMonth(int IMnth)
{
if(IMnth > 0 && IMnth < 13)
NMnth = IMnth;
else
NMnth= 1;
}
// GetDay Method returns day(number)
public int GetDay()
{
return NDy;
}
// GetYear Method returns year(number)
public int GetYear()
{
return NYr;
}
// SetDay sets the day with the input argument(parameter)
public void SetDay(int IDy)
{
// if input argument IDy(day) is greater than 0 and it is less than the number of days it's month has
// it sets the NDy(day) instance variable with input parameter
if(IDy > 0 && IDy <= MnthD[NMnth])
NDy= IDy;
// if month is feb and it's year is a leap year and if the input parameter day is 29
// It sets the instance variable day with input parameter
if(NMnth == 2 && IDy == 29 && LeapYr())
NDy= IDy;
// If it is not any one of the above cases. It sets the instance variable day with 1
else
NDy = 1;
}
// SetYear sets the year with the input argument(parameter)
public void SetYear(int IYr)
{
NYr = IYr;
}
// returns true if instance variable year is a leap year
// A year is a leap year if it is divisible by 400 or (if it is divisible by 4 but not by 100)
public boolean LeapYr()
{
// if nYr is (divisible by 400) or (divisible by 4 but not by 100)
// return true
if((NYr % 400 == 0) || (NYr % 4 == 0 && NYr % 100 != 0))
return true;
// else return false
else
return false;
}
// return date in string format
public String DateString()
{
return(NMnth + "-" + NDy + "-" + NYr);
}
// returns the number of days available in NMnth
public int DyInMnth()
{
// if it is a leap year and month is feb, return MnthD[2] + 1(normal number of days in month two + 1)
if(LeapYr() && NMnth == 2)
return MnthD[NMnth] + 1;
// else return MnthD[NMnth](Normal number of days in month(NMnth))
else
return MnthD[NMnth];
}
// returns number of days completed till the current date
public int ConvDOY()
{
int DOY = 0;
for(int i = 1; i < NMnth; i++) // add up all the days until before the start of this month
{
if(LeapYr() && i == 2) // if leap year add 29 for feb
DOY += 29;
else
DOY += MnthD[i]; // else, add Normal Number of days in a month
}
DOY += NDy; // add today's date to finish it off
return DOY; // return this number
}
public int DyLft()
{
return 365 - ConvDOY(); // return number of days left in this year
}
// adding input parameter to current date, month, year or updating the date
public void NewDy(int Days)
{
int DLoop = Days / 30 + 3;
int j, DoneDy = 0, DyInMnth, MinusDy;
for(j = 1; j <= DLoop; j++)
{
if(DoneDy == 1)
return;
switch(NMnth)
{
case 9:
case 4:
case 6:
case 11: DyInMnth = 30;
break;
case 2: DyInMnth = 28;
if(NYr % 4 == 0)
{
if(NYr % 100 != 0)
DyInMnth = 29;
else
if(NYr % 400 == 0)
DyInMnth = 29;
}
break;
default: DyInMnth = 31;
}
MinusDy = DyInMnth - NDy;
if(Days > MinusDy)
{
NMnth =++ NMnth % 13;
if(NMnth == 0)
{
NYr++;
NMnth++;
}
Days -= MinusDy;
NDy = 0;
}
else
{
NDy += Days;
DoneDy = 1;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.