Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need help with Gregorian calendar

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Need help with Gregorian calendar

    Here is my assignment.

    The Gregorian calendar was introduced in 1582. Every year divisible by four was
    declared to be a leap year, with the exception of the years ending in 00 and not divisible
    by 400. So 1200, 1600, and 2000 are leap years while 1300, 1400, 1800, and 1900 are
    not. Write a program that requests a year as input and states whether it is a leap year.


    /**
     * 
     * Author : Gregory B Shavers Jr
     * CSC 225 - Online 
     * problem 6
     */
     
    import java.util.*;
     
     
    public class problem6
    {
        public static void main( String [] args)
        {
     
        double year, leapYear, leapYear2;
     
        Scanner scan = new Scanner(System.in);
     
        System.out.print(" What is the year? ");//ask for year
        year = scan.nextDouble();
     
        leapYear = year%400;//calualates leap year   
        leapYear2 = year%4;  //calualates leap year  
     
        if ( leapYear == 0  )
        {
         System.out.println( " This is a leap year. " ); //display leap year      
        }
     
        else if ( leapYear2 == 0 )
     
        {
        System.out.println ( " This is a leap year."  ); // display not leap year
        }    
     
        else
        {
        System.out.println( " This is not a leap year.");
        }
     
        }
     
    }

    I need help writing a condition statement to finish this assignment. My condition statement should not allow a number to be a leap year if its not equal to 00 and not divisible by 400. Is there any ideas?

    Thanks Again!

    --- Update ---

    I'm sorry ending with 00.


  2. #2
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Need help with Gregorian calendar

    FG

  3. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Gregorian calendar

    Thanks for the response. I completely slacked on this homework assignment!!smh lol I have an A. But it just bothers me that I won't be able to keep the A lol Thanks Again!

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Need help with Gregorian calendar

    Quote Originally Posted by Rain_Maker View Post
    ...Write a program that requests a year as input and states whether it is a leap year...
    You could just follow your logic to the bitter end.

    Starting with what you wrote, pseudo-code for the algorithm could look something like the following:

    IF (year IS DIVISIBLE BY 400) THEN
     
        It is a leap year. // Divisible by 400 is all you need to know!
     
    ELSE IF (year IS DIVISIBLE BY 4) THEN
     
        IF (year IS DIVISIBLE BY 100)) THEN
            It is not a leap year. // Not divisible by 400. Divisible by 100.
        ELSE
            It is a leap year.     // Divisible by 4. Not divisible by 100
        END IF
     
    ELSE
     
        It is not a leap year.     // Not divisible by 4.
     
    END IF

    Of course there are other sequences of tests that also give the correct answer. (For example: Test for divisibility by 400, then by 100, then by 4, stopping wherever appropriate.)

    More experienced programmers are always tempted to compress things to one or two compound conditionals instead of using nested conditionals. Sometimes that makes things more clear, but sometimes cleverness and "elegant" terseness makes it more difficult to understand. You should lay out the logic steps in a way that is "obviously correct" from your own point of view.


    Notes:

    Why the heck do you use a double precision floating point variable for something that is, by its very nature, an integer? In Java you might get the correct output with floating point variables, but, in my opinion, it's just wrong-headed. There are situations where it is appropriate to use floating point. arithmetic This is simply not one of those.

    Why the heck do you use numerical variables for the result of tests of divisibility? I mean, you might be able to get the correct answer, but it's just an indication of muddy thinking (perhaps influenced by previous experience with C or some other language that doesn't have boolean data types, or the effect of getting help from someone like that).

    As far as that goes, why even have variables for the results of divisibility tests at all? Wherever possible I think that statements should be self explanatory and not make a program maintainer (or person trying to debug the program) have to refer back to some previous assignment statement to see what condition is being tested:

        if ((year % 400) == 0) {
            // Whatever...
        }
        else if ((year % 4) == 0) {
            // Whatever
        }

    Bottom line: Get into the habit of doing whatever you think you can do to make it "correct at a glance." You will thank me later.


    Cheers!

    Z

  5. #5
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: Need help with Gregorian calendar

    C

Similar Threads

  1. Gregorian to Julian and vise versa
    By heythisgreg in forum Java Theory & Questions
    Replies: 4
    Last Post: January 27th, 2013, 07:01 PM
  2. Gregorian Calendar - Problems
    By BobDole6395 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 9th, 2012, 02:24 PM
  3. Replies: 9
    Last Post: March 16th, 2011, 11:35 AM
  4. Calendar help
    By moonieass13 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 31st, 2009, 12:43 AM
  5. which calendar to use?
    By rptech in forum AWT / Java Swing
    Replies: 6
    Last Post: August 30th, 2009, 03:18 PM