Results 1 to 4 of 4
- 09-25-2009, 03:28 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 22
- Rep Power
- 0
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.
This below code is the constructor 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); } }
for my constuctor class, i am having problem with the String there, if((year/100)&&(!=(year/400)))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);} } }
how i solve it.
- 09-25-2009, 04:14 PM #2
Member
- Join Date
- Sep 2009
- Posts
- 6
- Rep Power
- 0
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; }
This is a syntax error. You can use % operator.for my constuctor class, i am having problem with the String there, if((year/100)&&(!=(year/400)))
how i solve it.[/
Java Code:if((year%100 !=0) && (year %400)!= 0)
it is another syntax error. Again you may want to use % operator.if (year/4.0)
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:
After you resolve all syntax error, you may need to debug your code. Look like it does not function as you expected.Java Code:public void calculate() throws [COLOR="Red"]OutOfRangeException[/COLOR] { .... }
- 09-25-2009, 04:28 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 22
- Rep Power
- 0
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.
public class GregorianJava 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); } }
{
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:
- 09-25-2009, 08:50 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 6
- Rep Power
- 0
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
-
Happy New Year
By sanjeevtarar in forum EntertainmentReplies: 7Last Post: 12-14-2011, 05:12 AM -
Date of first day, given the week in the year and the year...
By Lee.J.Baxter in forum Advanced JavaReplies: 1Last Post: 08-26-2009, 08:48 AM -
Wishing a very Happy New Year
By CJSLMAN in forum Forum LobbyReplies: 4Last Post: 01-05-2009, 05:38 AM -
PROBLEM - calculating with array elements
By ella in forum New To JavaReplies: 13Last Post: 12-04-2008, 12:36 AM -
Leap Year Program
By busdude in forum New To JavaReplies: 3Last Post: 10-16-2008, 03:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks