Results 1 to 4 of 4
  1. #1
    busdude is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default Leap Year Program

    I wrote a program with 2 classes that determines whether a given year is a leap year or not, when i compiled it says no syntax errors, but it doesn't run properly. I run it in BlueJ but it doesnt give me the option to get a void main thing.



    Java Code:
    public class Year
    {
    
    
    public Year(int year1)
    {
        year = year1;
    
    }
        public boolean isLeapYear()
        
    {
        boolean answer = false;
        
        
        
    
            if (year % 400 == 0) 
    {
    
            answer = true;
        }
            else if (year % 100 == 0)
            {
            answer = false;
        }
            else if (year % 4 == 0)
            {
            answer = true;
        }
            else
            answer = false;
    
    
    
            return answer;
    
    }
    
    
    int year;
    
    
    }


    Java Code:
    import java.util.Scanner;
    
    
    public class YearTester
    {
    
        public void main(String[] args)
            {
    
    
                Scanner in = new Scanner(System.in);
                
                System.out.println("Enter a year." );
                int t = in.nextInt();
    
                Year x = new Year(t);
    
    
    
                if (x.isLeapYear()){
    
                    System.out.println("That is a leap year.");
                }
                
                else 
                System.out.println("That is not a leap year.");
    
    }
    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You have define the main method in wrong way. This is wrong,

    Java Code:
    public void main(String[] args)
    should be like this,

    Java Code:
    public static void main(String[] args)

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    You don't need to make a second class to test your Year class. Just create the same main method in the Year class (with the change Eranga mentioned).

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Month and year program code!!!!HELP PLEASE!!!
    By chitwood in forum New To Java
    Replies: 4
    Last Post: 10-13-2010, 02:21 PM
  2. calendar of 12months when year is entered
    By veena in forum New To Java
    Replies: 16
    Last Post: 05-06-2008, 06:03 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  4. Merry Christmas and happy New year
    By rgbosque in forum Forum Lobby
    Replies: 3
    Last Post: 12-31-2007, 03:44 AM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •