Inheritance
Constructors
Accessibility
private
accessible within the same class.
unspecified (friendly) (default)
additionally accessible to classes within the same package (folder).
protected
additionally accessible to child classes.
public
accessible to all classes.
Part I.
Write a program that would determine the day number in a non-leap year. For example, in a non-leap year, the day number for Dec 31 is 365; for Jan 1 is 1, and for February 1 is 32. This program will ask the user to input day and month values. Then it will display the day number day number corresponding to the day and month values entered assuming a non-leap year. (See part II to this exercise below).
Optional:
Optionally provide the date validation code in the main method. The validation code will verify that the values entered by the user for the day and month are valid.
Implementation
For this exercise, create the following classes:
class Day
Create a public class Day that provides the following:
(This class keeps day and month value. It does not deal with a year value).
class TestDay
Create a class TestDay containing the main ( ) method. In the main( ) method, do the following:
Testing:
Run the program twice. Use the following data:
Test Run 1:
Input:
Enter day
10
Enter month
3
Output:
Day Number for 3/10 is 69
Description
Enhance the above exercise so that it can determine the day number for both leap and non-leap years. This program will ask the user to enter the day, month, and year. Then it will display the day number corresponding to the date entered taking into account the leap year.
Implementation
Create the following two additional classes:
class LeapDay
Create a LeapDay that extends the class Day and additionally provides the following:
class TestLeapDay
Create a class TestLeapDay containing the main ( ) method. In the main( ) method, do the following:
//The code snippet below determines a leap year.
if ( ((year % 400) == 0) || ( ((year % 4) == 0) && ((year%100) != 0) ) ) )
{
//Leap year
}
else
{
//Not a leap year
}
Run the program twice. Use the following data:
Test Run 2:
Input
Enter day
10
Enter month
3
Enter year
2001
Output:
Day Number for 3/10/2001 is 69
Test Run 3:
Input
Enter day
10
Enter month
3
Enter year
2004
Output:
Day Number for 3/10/2004 is 70