Haven't been here for a looooooong time.
I'm taking a Java course now...
This is what I gotta do:
Quote:
"Write an applet to calculate the date following a given date. You will have to take into account leap years. Make sure that your program detects wrong date inputs (ex. month = 13) and displays an error message.
Hint: Create a method that calculates the maximum number of days in a given month.
Months 1, 3,5,7,8,10,12 - have 31 days
Months: 4,6,9,11 - 30 days
Month: 2 - 28 (not a leap year), 29 (leap year)
(use a switch statement to determine maximum number of days)
A leap year is a year that is divisible by 4 except that century years also need to be divisible by 400 to be a leap years
Your output should look something like this:
Enter the month for today's date: 12
Enter the day for today's date: 31
Enter the year for today's date: 1886
Tomorrow's date is 1/1/1887 "
|
And here is my code:
Code:
private void checkDay(int day, int month, int year){
switch (month){
case 1: case 3: case 5: case 7: case 8: case 10: longMonth(month); break;
case 2: if ((day>0)&&(day<27)){
tday=day++;
}else if (day==28){
boolean leap =checkLeap(year);
if (leap==true){
tday=1;
tmonth=month++;
tyear=year;
}else if (leap==false){
tday=day++;
tmonth=month;
tyear=year;
}
}else if (day==29){
tday=day++;
tmonth=month;
tyear=year;
}
break;
case 4: case 6: case 9: case 11: shortMonth (month); break;
case 12: if ((day>0)&&(day<31)){
tday=day++;
tmonth=month;
tyear=year;
}else if (day==31){
tday=1;
tmonth=1;
tyear=year;
}
}//end of switch
}// end of checkDay
//Checking Leap Year
private boolean checkLeap(int year){
boolean leap;
if ((year%4==0)&&(year%400==0))
leap=true;
else
leap=false;
return leap;
}
//Long and Short Months
private void longMonth(int month){
int tday,tmonth,tyear;
if ((day>0)&&(day<31)){
tday=day++;
tmonth=month;
tyear=year;
}else if (day==31){
tday=1;
tmonth=month++;
tyear=year;
}
}
private void shortMonth (int month){
int tday,tmonth,tyear;
if ((day>0)&&(day<30)){
tday=day++;
tmonth=month++;
tyear=year;
}else if (day==30){
tday=1;
tmonth=month++;
tyear=year;
}
}
My invocation is:
checkDay(12,31,1886);
g.drawString("Enter the month for today's date: 31",200,400);
g.drawString("Enter the day for today's date: 12", 200,425);
g.drawString("Enter the year for today's date: 1886", 200,450);
g.drawString("Tomorrow's date is "+tday+"/"+tmonth+"/"+tyear,200,475);
I put it in the paint method...
I don't see any mistakes so... I don't really know what to do.
Can anyone help?
Thx.