Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 10-27-2004, 01:39 PM   #1 (permalink)
UnusualGameBoy
Registered User
 
UnusualGameBoy's Avatar
 
Join Date: Dec 2002
Location: Canada (for now...)
Posts: 2
UnusualGameBoy is on a distinguished road
Cool Brain Melting: Trouble With If Statements.....

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.
UnusualGameBoy is offline   Reply With Quote
Old 10-27-2004, 01:40 PM   #2 (permalink)
UnusualGameBoy
Registered User
 
UnusualGameBoy's Avatar
 
Join Date: Dec 2002
Location: Canada (for now...)
Posts: 2
UnusualGameBoy is on a distinguished road
Sorry for the lack of indentation....
Maybe someone should reprogram the forums to accept them
UnusualGameBoy is offline   Reply With Quote
Old 10-27-2004, 01:43 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,487
sde is on a distinguished road
Quote:
Originally Posted by UnusualGameBoy
Sorry for the lack of indentation....
Maybe someone should reprogram the forums to accept them
lol, ..use [cod e] [/co de] (without the space in the word)
__________________
Mike
sde is offline   Reply With Quote
Old 10-27-2004, 02:10 PM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Your February check doesn't seem right. You check to see if the day is 29, but don't check to see if it is, in fact, a leap year. Plus, you then go on to increment the day, but I can't think of an instance (apart from old Roman calendars) when February had 30 days.

Further, your leap-year check seems off, as you're checking to see if the year is a multiple of 4 AND 400. Needless to say that 2004, while being a leapyear, isn't a multiple of 400.

For the record, the way that leap-years work is that it's a leap-year every 4 years, except every 100 years, except every 400 years. That's why 1900 and 2100 won't be leap-years, but 2000 was.

Finally, your December check is off, as you set the next year to the current year. Hence, 12/31/04 will turn into 1/1/04.

Those are the mistakes I see off the top of my head. But, you didn't mention what was actually going wrong, so I'm not sure if I answered your question or not.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 06:54 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting