Goals

Question 1

  1. Download LeapYear.java file. Open it in Dr Java. Turn on line numbers (Edit > Preferences > Display Option > Show All Line Numbers)
  2. Observe the method name leapYear1. This is pretty performing your hw 1 question problem, except that I have defined the statements that test for leap year into method body i.e. { }. Also notice that the year (which of type int) is used the input to method. Also notice that return-type is void as the method is really not returning anything to us but just printing to screen the outcome.
    1. Compile the file. In Dr Java Interactions pane type the following:
      > 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.

      Now call the method with different intputs such as 1900, 2003, 2004, 2020 etc..

    2. Now observe the method 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);
      
      
    3. Now observe the method named main. Main is special method that provides Java programs to be independent of any IDE (example Dr Java). It the starting point of computer program or application for programs written in Java. The main method does not return anything (i.e return type is void) and takes in input String [] which array of Strings which we will discuss later.

      Note that 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)).
      This is because 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)).

      Further notice how the if statement's condition is the return value of the leapYear2 method. This is because call to leapYear2 methods results in boolean value which then determines which statements should get executed.
          if(leapYear2(year)){
      		System.out.println(year + " is a leap year");
      	}
      	else{
      	   System.out.println(year + " is a leap year");
      	}
      

      Now compile the file, hit the RUN button. You should observe the output to the screen in the Interactions pane.

Question 2

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.