# Having problem in calculating leap year

• 09-25-2009, 03:28 PM
lclclc
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.
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.
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.
• 09-25-2009, 04:14 PM
pkang
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.
Code:

```public void setYear(int year){   this.year = year; }```

Quote:

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.
Code:

`if((year%100 !=0) && (year %400)!= 0)`
Quote:

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:

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.
• 09-25-2009, 04:28 PM
lclclc
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.
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);}
}
}

Code:

• 09-25-2009, 08:50 PM
pkang
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.