Topics Inheritance Constructors Accessibility Accessibility Rules private access
ID: 3914513 • Letter: T
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
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
Submit
Copy the following in a file and submit that file:
Output dialog boxes containing the complete output in a single dialog box for each of test runs
The source code from all the source java file
Explanation / Answer
Day.java
public class Day{
private int day;
protected int month;
public Day(int d , int m){
day = d;
month = m;
}
public int getDay(){
return day;
}
public int getMonth(){
return month;
}
public int findDayNum(){
int[] mon = { 31,28,31,30,31,30,31,31,30,31,30,31};
int sum = 0;
for (int i = 0; i<(month-1); i++)
sum = sum + mon[i];
return (sum + day);
}
}
TestDay.java
import java.io.*;
import java.util.*;
public class TestDay {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter day:");
int d = Integer.parseInt(sc.nextLine());
System.out.println("Enter month:");
int m = Integer.parseInt(sc.nextLine());
//System.out.println("Enter day:");
//int y = Integer.parseInt(sc.nextLine());
if (m >= 1 || m <= 12){
if ( (m == 1 || m ==3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) && (d > 31)){
System.out.println("Invalid month");
}
else if ( (m == 4 || m ==6 || m == 9 || m == 11) && (d > 30)){
System.out.println("Invalid day");
}
else if ( m == 2 && d > 28){
System.out.println("Invalid day");
}
else {
Day dd = new Day(d,m);
int n = dd.findDayNum();
System.out.println("Day number of " + m + "/" + d + " is " + n);
}
}
else {
System.out.println("Invalid month");
}
}
}
LeapDay.java
public class LeapDay extends Day{
private int year;
public LeapDay(int d , int m, int y){
super(d,m);
year = 7;
}
public int getYear(){
return year;
}
public int findDayNum(){
int[] mon = { 31,28,31,30,31,30,31,31,30,31,30,31};
int sum = 0;
for (int i = 0; i<(month-1); i++)
sum = sum + mon[i];
if ((year % 400 == 0 || year % 4 == 0) && (year % 100 != 0)){
if (month > 2)
return (sum + getDay() + 1);
}
return (sum + getDay());
}
}
TestLeapDay.java
import java.io.*;
import java.util.*;
public class TestLeapDay {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter day:");
int d = Integer.parseInt(sc.nextLine());
System.out.println("Enter month:");
int m = Integer.parseInt(sc.nextLine());
System.out.println("Enter year:");
int y = Integer.parseInt(sc.nextLine());
int leap = 0;
if ((y % 400 == 0 || y % 4 == 0) && (y % 100 != 0))
leap = 1;
if (m >= 1 || m <= 12){
if ( (m == 1 || m ==3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) & (d > 31)){
System.out.println("Invalid day");
}
else if ( (m == 4 || m ==6 || m == 9 || m == 11) && (d > 30)){
System.out.println("Invalid day");
}
else if ( m == 2 && d > 28 && leap == 0){
System.out.println("Invalid day");
}
else if ( m == 2 && d > 29 && leap == 1){
System.out.println("Invalid day");
}
else {
Day dd = new Day(d,m);
int n = dd.findDayNum();
System.out.println("Day number of " + m + "/" + d + " is " + n);
}
}
else {
System.out.println("Invalid month");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.