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 6 of 6

Thread: I need help writing this program

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help writing this program

    In general terms the algorithm for calculating a leap year is as follows...
    A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.
    Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years.
    Write a program that asks the user how many years they want to check.
    For that number of times the program accepts as data a year. The program will then determine if the year is a leap year or not. The program will keep doing this until the correct number of years is checked.
    Display the percentage of input years that are leap years, as well as the percentage that are not leap years. Confirm these add to 100.
    Use a sentinel instead of asking how much data you have
    using formatting as described in the text, make a table of your output. Real numbers should have 2 decimal places, and decimal points should line up
    can you put the comments on each code for what they do. Thank you if you can help me


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: I need help writing this program

    Hello kev2000, welcome to the forums.

    Have you started any of this assignment yet? Please post what you have so far and we can guide you in the right direction.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help writing this program

    I haven't started yet, I don't know where to start. It will be nice if you can help me understand.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: I need help writing this program

    Quote Originally Posted by kev2000 View Post
    I haven't started yet, I don't know where to start. It will be nice if you can help me understand.
    Here is some code to help you get started.

    It will read in the year submitted by the user in the console. Then it will work out if the year is divided by 4. See if you can work out the rest...

    import java.util.Scanner;
     
    public class Kev2000 {
     
        /**
         * JavaProgrammingForums.com
         */
        private static int year;
        private static boolean isLeapYear = false;
     
        public void divisibleBy4() {
     
            String myYear = Integer.toString(year);
            myYear = myYear.substring(2, 4);
            int myYearInt = Integer.parseInt(myYear);
     
            for (int a = 0; a < myYearInt; a++) {
                int calc = (4 * a);
                if (calc == myYearInt) {
                    System.out.println(year + " is divisible by 4");
                    //Next part of the calculations needed here
                    break;
                }
     
            }
     
            isLeapYear = false;
     
        }
     
     
        public static void main(String[] args) {
     
            Kev2000 k = new Kev2000();
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a year: ");
            year = sc.nextInt();
            k.divisibleBy4();
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: I need help writing this program

    Quote Originally Posted by JavaPF View Post
    Here is some code to help you get started.

    It will read in the year submitted by the user in the console. Then it will work out if the year is divided by 4. See if you can work out the rest...

    import java.util.Scanner;
     
    public class Kev2000 {
     
        /**
         * JavaProgrammingForums.com
         */
        private static int year;
        private static boolean isLeapYear = false;
     
        public void divisibleBy4() {
     
            String myYear = Integer.toString(year);
            myYear = myYear.substring(2, 4);
            int myYearInt = Integer.parseInt(myYear);
     
            for (int a = 0; a < myYearInt; a++) {
                int calc = (4 * a);
                if (calc == myYearInt) {
                    System.out.println(year + " is divisible by 4");
                    //Next part of the calculations needed here
                    break;
                }
     
            }
     
            isLeapYear = false;
     
        }
     
     
        public static void main(String[] args) {
     
            Kev2000 k = new Kev2000();
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a year: ");
            year = sc.nextInt();
            k.divisibleBy4();
        }
     
    }
    A quick suggestion: wouldn't it be simplier to use mod function?

    boolean divBy4 = myYearInt % 4 == 0

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: I need help writing this program

    Quote Originally Posted by Dalisra
    A quick suggestion: wouldn't it be simplier to use mod function?

    boolean divBy4 = myYearInt % 4 == 0
    It would indeed be a lot simpler to use that function! Thank you Dalisra.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Writing java applications for the iPod Touch
    By JavaPF in forum Java ME (Mobile Edition)
    Replies: 8
    Last Post: June 14th, 2011, 04:34 PM
  2. How to start writing Java Apps for Cell Phones?
    By Jchang504 in forum Java ME (Mobile Edition)
    Replies: 7
    Last Post: January 10th, 2011, 05:11 AM

Tags for this Thread