Question: 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
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 Java 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.
Test Run:
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 its 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 objects accessor methods.
Test Run :
Input:
Enter day
10
Enter month
3
Enter year
2001
Output:
Day Number for 3/10/2001 is 69
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
