public static return-type method-name (0 + parameters) {
statement(s)
}
Note: For now we will always have the keys words public static in the order declared in all method declarations.
Later we will that methods can be private (instead of public) and non-static i.e. without the keyword static. > LeapYear.leapYear1(2000);
> 2000 is a Leap Year //This will be the output to screen in "Interactions Tab"
To use a method we simply call its name and provide the number of inputs it needs. E.g. leapYear1(2000) is call to method leapYear1 with input as 2000. However, since we working interactions pane, we need tell Dr Java where the method is located. Hence we have syntax LeapYear.leapYear1(2000) which basically means the leapYear1 method is in file LeapYear.leapYear2. This is similar to method leapYear1 except the method returns boolean value true or false. So instead print statement we get result true or false. Note the special keyword return that indicates method returns.
> LeapYear.leapYear2(2000); Note that nothing happens, as the semicolon inhibits the output in Dr Java Interactions pane. To view output we do > LeapYear.leapYear2(2000) //without semicolon > true However, in java programs all statements must end with a semicolon so to really see or use the outcome you would do any of the following: > System.out.println(LeapYear.leapYear2(2001)); > false //this outcome you should get or > boolean result = LeapYear.leapYear2(2001);
main method calls method leapYear2. So one method can call other methods to do part of the work. However in this case it directly calls it (i.e. leapYear(2000)). main and leapYear2 methods are in the same file. If the they were in different files then we have to use call notation used in step 2 i.e. filename.method-name(input(s)).
if(leapYear2(year)){
System.out.println(year + " is a leap year");
}
else{
System.out.println(year + " is a leap year");
}
Write another method called leapYearBet in file called LeapYear.java which takes in two inputs a starting year and ending year and prints out the leap years between those years (start and end year is inclusive) provides the following sample outcome when the method is called:
Leap years between 2000 and 2008: 2000 2004 2008 To test your method do the following in the interactions pane: > LeapYear.leapYearBet(2000, 2008);
Hint: The method does not return anything. Also you already know how to determine if year is a leap year, so you can resuse already written method (to avoid re-inventing wheel). Also you need some kind loop structure.
Submit: file LeapYear.java with the leapYearBet method to Blackboard. Please follow submission rules as stated on Programming Info page.