1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Leap Year Trouble in Java

Discussion in 'Software Development' started by Iron_Dragon, Nov 18, 2007.

Thread Status:
Not open for further replies.
  1. Iron_Dragon

    Iron_Dragon Thread Starter

    Joined:
    Aug 14, 2007
    Messages:
    9
    I'm new to Jcreator and I missed about a month from my computer programming class in school, and am far behind. My assignment right now is this problem p5.12 from Computing Concepts with Java Essentials it says:
    "A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for ex.,1980). However, since the introduction of the Gregorian calender on October 15, 1582, a year is not a leap year if it is divisible by 100 (for ex., 1900); however, it is a leap year if it is divisible by 400 (for ex., 2000). Write a program that asks the user for a year and computes whether that year is a leap year., Implement a class Year with the method boolean isLeapYear

    This is the code that I came up with:
    import javax.swing.JOptionPane;

    public class LeapYear {

    public static void main(String[] args) {
    String input = JOptionPane.showInputDialog("Enter Year");
    double year = Double.parseDouble(input);
    LeapYear newYear = new LeapYear(year);
    System.out.println(newYear.boolean isLeapYear()); //line 11

    }
    }


    public class LeapYear {
    private double year;

    public LeapYear(double year)
    {
    a = year;
    }

    public boolean isLeapYear(){

    if(a % 4 == 0)
    {
    if(a % 100 != 0) System.out.println("Leap Year");
    }
    else if (a % 400 == 0) System.out.println("Leap Year");
    else System.out.println("Not Leap Year");
    }
    }

    The error I am receiving is on the 11th line of the test class. I know if there is something wrong besides that. I'm not asking for someone to fix this for me but I would like if somebody could give me advice on what to do, hints, explain where I went wrong, or know of any good tutorials I could read that could help me with this it would be much appreciated.
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi Iron_Dragon,

    Welcome to TSG ! :)

    The line 11 should be :
    System.out.println(newYear.isLeapYear());

    There's a mistake in the constructor : the variable 'a' is undefined
    Code:
    [SIZE=2]
    private double [COLOR=Red]year[/COLOR];
    
    public LeapYear(double [COLOR=Blue]year[/COLOR])
    {
       this.[COLOR=Red]year[/COLOR] = [COLOR=Blue]year[/COLOR];
    }
    [/SIZE]

    There's another mistake in isLeapYear() : the method must return a boolean.
    Code:
    [SIZE=2]
    public [COLOR=Blue]boolean[/COLOR] isLeapYear(){
        if (year % 4 == 0) {
            if (year % 100 != 0) {
                System.out.println("Leap Year");
                [COLOR=Blue]return true[/COLOR];
            } else {
                if (year % 400 == 0) {
                    System.out.println("Leap Year");
                    [COLOR=Blue]return true[/COLOR];
               } 
            }
        } else {
            System.out.println("Not Leap Year");
            [COLOR=Blue]return false[/COLOR];
        }
    }[/SIZE]
     
  3. Iron_Dragon

    Iron_Dragon Thread Starter

    Joined:
    Aug 14, 2007
    Messages:
    9
    Thanks for the help, the code you submitted helped me work out the kinks in my code.
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - Leap Trouble Java
  1. Pedro14
    Replies:
    0
    Views:
    399
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/653182

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice