File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help with a static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with a static method" Watch "Help with a static method" New topic
Author

Help with a static method

Christian Staves
Greenhorn

Joined: Jul 07, 2010
Posts: 26
Im writing a class LeapYearExpert with one static method:
isLeapYear(int year). This methods should return true, if the year given
as a parameter is a leap year and false otherwise.

Does this accomplish that?

Im not sure, if I'm doing it right and I also have to write a tester, but I wanna make sure my class atleast works first.




a tester that reads three years from the user using a scanner and each of these inputs is tested if it is a leap year.
Im unsure of how to call the static method in the tester ... How can I make the tester call on the static method and then tell the user they cant use a number outside of 1900-2299.



As always thanks for the help.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Static methods 'belong' to the Class and not an instance, so rather than having new LeapYearExpert() you just need to refer to teh class alone ie LeapYearExpert.isLeapYear(year)
Christian Staves
Greenhorn

Joined: Jul 07, 2010
Posts: 26
David O'Meara wrote:Static methods 'belong' to the Class and not an instance, so rather than having new LeapYearExpert() you just need to refer to teh class alone ie LeapYearExpert.isLeapYear(year)


huh.....? in what context would I use it, Im a little lost at the moment, I've been reading, trying to google for help to no avail. My book doesnt cover this stuff well at all.

Sorry if its a dumb question.
Kamalakannan Cm
Greenhorn

Joined: Jul 14, 2010
Posts: 2
You need to call the static method from your main method in the Tester class as below


public static void main (String [] args )
{
/** Scanner to read user input*/
Scanner reader = new Scanner(System.in);

System.out.println ( " Enter a Year from 1900 to 2299: ");
int newYear = reader.nextInt();
boolean leap = LeapYearExpert.isLeapYear(newYear);

if (leap)
System.out.println("It is a Leap Year");
else
System.out.println("It is not a Leap year");

}

if you want to get three years, then you need to use some loop to repeat the same thing for three times

Regards
kamal
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 50
Christian Staves wrote:Im writing a class LeapYearExpert with one static method:


I feel the logic also is not right as the check for year % c==0 will never happen as something divisible by 400 will always be divisible by 100. It should be
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

well no, the logic is not correct, but having solved the initial issue couldn't we leave this for the original poster to discover and resolve?
Christian Staves
Greenhorn

Joined: Jul 07, 2010
Posts: 26
Thanks for the help guys. yeah my logic was off, Im work it it more tomorrow.

thanks again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Rajesh Nagaraju, you hvae been around for 6½ years and not seen what it says on the beginning forum:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
All you achieve by giving such an answer is depriving the other person of the chance to learn. Don't be annoyed, but I have pulled rank and deleted the solution.
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 50
sorry did not realise that I was depriving the poster an opportunity to learn
My sincere apologies
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Apologies accepted
Christian Staves
Greenhorn

Joined: Jul 07, 2010
Posts: 26
Ok, so Ive been working on it a bit and Im trying to set it up so if you enter outside of 1900-2299 it errors on you. But as it is it keeps printing everything, it'll check if its a leap year and then it also does the error if you did enter a numder outside the range.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with a static method
 
Similar Threads
Leap Year program
please verify this code
two puzzling errors
Leap year program
unreachable statement error