Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with a static method

 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys. yeah my logic was off, Im work it it more tomorrow.

thanks again.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry did not realise that I was depriving the poster an opportunity to learn
My sincere apologies
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies accepted
 
Christian Staves
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic