Results 1 to 4 of 4
  1. #1
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default Having problem in calculating leap year

    A year is a leap year if it is divisible by 4, unless it is also divisible 100 but not 400;eg, year 1900 is not a leap year because it is divisible by 100, but year 2000 is a leap year because even though it is divisible by 100 , it is also divisible by 400. produce an error message for input value less than 1582.
    Below is my main class code.
    Java Code:
    import java.util.Scanner;
    
    public class LeapYear
    {
    	public static void main(String[] args) throws OutOfRangeException
    	{
    		int year;
    		Gregorian gCalendar = new Gregorian();
    		Scanner scan = new Scanner(System.in);
    		System.out.print("Enter a year:    ");
    		year = scan.nextInt();
    		year.calculate();
    		System.out.println(year);
    		
    	}
    }
    This below code is the constructor code.
    Java Code:
    public class Gregorian
    {
    	private int year;
    	
    	public Gregorian()
    	{
    		calculate();
    	}
    	public void calculate()
    	{
    		OutOfRangeException problem = new OutOfRangeException("Input value is out of range.");
    		if (year !=1582 || year<1582)
    			throw problem;
    	}
    	public int getYear()
    	{
    		return year;
    	}
    	public String toString()
    	{
    		String result;
    		if (year/4.0)
    			{	result = "This is a leap year." ;
    				if((year/100)&&(!=(year/400)))
    					result = "This is consider a leap year.";
    			}
    		return result;
    	}
    	public class OutOfRangeException extends Exception
    	{	OutOfRangeException( String message)
    		{super(message);}
    	}
    }
    for my constuctor class, i am having problem with the String there, if((year/100)&&(!=(year/400)))
    how i solve it.

  2. #2
    pkang is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    Your main method has syntax error. You may want to invoke calendar gCalendar.calculate() instead of year.calculate() :)

    You are in right track anyway. Your steps look good. But your main should pass the keyed-in year to the gCalendar object before calling the calculate method. You may consider to provide new method setYear(int year) in your Gregorian class.
    Java Code:
    public void setYear(int year){
       this.year = year;
    }


    for my constuctor class, i am having problem with the String there, if((year/100)&&(!=(year/400)))
    how i solve it.[/
    This is a syntax error. You can use % operator.
    Java Code:
    if((year%100 !=0) && (year %400)!= 0)
    if (year/4.0)
    it is another syntax error. Again you may want to use % operator.


    Since you have not pass in the year value yet, your call of calculated method from the constructor does always throw error. You may consider to move this call.

    In the side the calculate mothod you throw exception, but you did not declare the exception in the signature. This is another syntax error.
    Consider this:

    Java Code:
    public void calculate() throws [COLOR="Red"]OutOfRangeException[/COLOR]
    {
     ....
    }
    After you resolve all syntax error, you may need to debug your code. Look like it does not function as you expected.

  3. #3
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    I have corrected accordingly, but the public calculate and the public String seem having problem.
    Below is my main class code and the constructor code.
    Java Code:
    import java.util.Scanner;
    
    public class LeapYear
    {
    	public static void main(String[] args) 
    	{
    		int year;
    		Gregorian gCalendar = new Gregorian();
    		Scanner scan = new Scanner(System.in);
    		System.out.print("Enter a year:    ");
    		year = scan.nextInt();
    		gCalendar.calculate();
    		System.out.println(gCalendar);
    		
    	}
    }
    public class Gregorian
    {
    private int year;

    public Gregorian()
    {
    calculate();
    }
    public void calculate() throws OutOfRangeException
    {
    OutOfRangeException problem = new OutOfRangeException("Input value is out of range.");
    if (year !=1582 || year<1582)
    throw problem;
    }
    public void setYear(int year)
    {
    this.year = year;
    }
    public int getYear()
    {
    return year;
    }
    public String toString()
    {
    String result;
    if (year%4.0 !=0)
    { result = "This is a leap year." ;
    if((year%100 !=0)&&((year/400 !=0)))
    result = "This is consider a leap year.";
    }
    return result;
    }
    public class OutOfRangeException extends Exception
    { OutOfRangeException( String message)
    {super(message);}
    }
    }


    Java Code:
      

  4. #4
    pkang is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    Here some more works to do:

    1- You did not set the value to gCalendar yet.
    You need to set it after year is read: gCalendar.setYear(year);. Since the calculate throws exception, you have to handle the exception in the main too.

    2- You constructor in Gregorian class calls the calcuate method, which I think is not right. You can remove it if you agree, otherwise, you have to handle the exception (catch it or throw it)

    3- In your toString() method, the result is not instantiated yet. You have to explicitly give its default value (null or something).

    4- Again, after cleaning up your syntax errors, you may need to review your code. It may now provide what you need.

Similar Threads

  1. Happy New Year
    By sanjeevtarar in forum Entertainment
    Replies: 7
    Last Post: 12-14-2011, 05:12 AM
  2. Date of first day, given the week in the year and the year...
    By Lee.J.Baxter in forum Advanced Java
    Replies: 1
    Last Post: 08-26-2009, 08:48 AM
  3. Wishing a very Happy New Year
    By CJSLMAN in forum Forum Lobby
    Replies: 4
    Last Post: 01-05-2009, 05:38 AM
  4. PROBLEM - calculating with array elements
    By ella in forum New To Java
    Replies: 13
    Last Post: 12-04-2008, 12:36 AM
  5. Leap Year Program
    By busdude in forum New To Java
    Replies: 3
    Last Post: 10-16-2008, 03:46 AM

Posting Permissions

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