Topics

Inheritance
Constructors
Accessibility

Accessibility Rules

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.

Description

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).

  • A private variable for holding day value.
  • A protected variable for holding month value.
  • A two parameter public constructor to initialize day and month values
  • A public accessor methods getDay ( ) that returns day value.
  • A public accessor method getMonth ( ) that returns month value.
  • A public method findDayNum ( ) that calculates the day number corresponding to the day and the month values stored in the object. It calculates the day number assuming that it is a non-leap year.

class TestDay

Create a class TestDay containing the main ( ) method. In the main( ) method, do the following:

  • Prompt the user for entering day and month values.
  • Create an object of class Day and pass its constructor the day and month values entered by the user.
  • Call the method findDayNum( ) of Day object to obtain day number. (This does not take into account the leap year).
  • Display the original date and the day number for that date assuming that it is a non leap year.

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

Part II

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:

  • A private variable year to hold the year value.
  • A public constructor with three parameters to initialize day, month and year values. This constructor initializes the day and month values by calling the parent constructor.
  • A public accessor method getYear ( ) that returns the year value.
  • A public method findDayNum ( ) that calculate the day number of the date stored in the object. This method over-rides the same method in the parent class. In calculating the day number, this method takes into account the leap year. In order to calculate the day number, this method calls the parent method findDayNum and adds one to the value received when it's a leap year and the month is greater than 2. This method accesses the month variable directly. Since month is defined protected in the parent class, it can be accessed directly from the child class.

class TestLeapDay

Create a class TestLeapDay containing the main ( ) method. In the main( ) method, do the following:

  • Prompt the user to enter day, month and year values.
  • Create an object of class LeapDay and pass its constructor the day and month values entered by the user.
  • Call the method findDayNum( ) of LeapDay object to obtain the day number. (This method takes into account the leap year).
  • Display the original date and the day number for that date. For displaying the original date, call the object's accessor methods.

Coding

//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
}

Testing:

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
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.