ISC Practicals and Theory - Day of the Year (OOP)
Updated 21 Oct 2026
The day number of a date is its position in the year: 1 January is day 1 and 31 December is day 365, or day 366 in a leap year. This program finds it for a date typed as day, month and year, and prints INVALID DATE for an impossible date. This is the OOP version: the date is passed to a parameterised constructor, and small methods isLeap(), isValid() and dayNumber() each do one job and return a value, while main reads the input and creates the object.
Definition
The day number of a date is the number of days from 1 January of that year up to and including that date. It equals the total days in all the complete months before the given month, plus the day of the month.
A year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400. In a leap year February has 29 days.
- 1 March 2023: $31 + 28 + 1 = 60$.
- 1 March 2024 (leap year): $31 + 29 + 1 = 61$.
- 2100 is not a leap year (divisible by 100 but not by 400), so 29 February 2100 is invalid; 2000 is a leap year.
Algorithm
mainreadsd,mandywith aScannerand createsob = new DayOfYear(d, m, y). The constructor copies them intodd,mmandyyyyand fills the arraydayswith 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 (index 0 is January).ob.isValid()returnsfalseifyyyyis not from 1 to 9999 ormmis not from 1 to 12. Otherwise it setslasttodays[mm - 1], changes it to 29 whenmmis 2 andisLeap(yyyy)is true, and returns whetherddis from 1 tolast.isLeap(y)returnstruewhenyis divisible by 4 and not by 100, or divisible by 400.- If the date is valid,
mainprintsob.dayNumber(), which:- adds
days[i]forifrom 0 tomm - 2; - adds 1 more if
mmis greater than 2 and the year is a leap year (the extra day of February); - adds
ddand returns the total.
- adds
- If the date is not valid,
mainprintsINVALID DATE.
Program
import java.util.Scanner;
/**
* class DayOfYear (OOP version)
* Stores a date given to the constructor and finds its day number in the year
* using one-job methods that return values.
* Example: 1 March 2024 = 61, 1 March 2023 = 60.
*/
public class DayOfYear
{
int dd, mm, yyyy; // day, month and year of the date
int days[]; // days[0..11] = number of days in January..December (February = 28 here)
/**
* Parameterised constructor.
* @param d day, @param m month, @param y year of the date to be examined
*/
DayOfYear(int d, int m, int y)
{
dd = d;
mm = m;
yyyy = y;
days = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
}
/**
* Tells whether a year is a leap year: divisible by 4 and not by 100, or divisible by 400.
* @param y the year to test
* @return true if y is a leap year, otherwise false
*/
boolean isLeap(int y)
{
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
/**
* Checks the date stored in the object.
* @return true if year is 1 to 9999, month is 1 to 12 and day fits the month
*/
boolean isValid()
{
if (yyyy < 1 || yyyy > 9999) // year must be 1 to 9999
return false;
if (mm < 1 || mm > 12) // month must be 1 to 12
return false;
int last = days[mm - 1]; // days in this month
if (mm == 2 && isLeap(yyyy)) // February has 29 days in a leap year
last = 29;
return dd >= 1 && dd <= last;
}
/**
* Finds the day number of the date within its year (the date must be valid).
* @return days of all the months before mm, plus dd
*/
int dayNumber()
{
int total = 0; // running total of days
for (int i = 0; i < mm - 1; i++) // complete months before the given month
{
total = total + days[i];
}
if (mm > 2 && isLeap(yyyy)) // the extra day of February counts only after February
total = total + 1;
return total + dd;
}
/** main: reads the date, creates the object and prints the result. */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter day (dd): ");
int d = sc.nextInt();
System.out.print("Enter month (mm): ");
int m = sc.nextInt();
System.out.print("Enter year (yyyy): ");
int y = sc.nextInt();
DayOfYear ob = new DayOfYear(d, m, y);
if (ob.isValid())
System.out.println("Day number of the year: " + ob.dayNumber());
else
System.out.println("INVALID DATE");
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
dd | int | Data member: day of the month |
mm | int | Data member: month (1 to 12) |
yyyy | int | Data member: year |
days | int[] | Data member: days in each month, January at index 0, February taken as 28 |
d | int | Constructor parameter and main variable: day typed by the user |
m | int | Constructor parameter and main variable: month typed by the user |
y | int | Constructor parameter and main variable: year typed by the user; also the parameter of isLeap(), the year to test |
last | int | Local variable of isValid(): number of days in the given month |
total | int | Local variable of dayNumber(): running total of days |
i | int | Loop counter in dayNumber(): index of the month being added |
sc | Scanner | Reads the day, month and year typed by the user |
ob | DayOfYear | Object of the class created in main |
args | String[] | Command-line arguments of main (not used) |
Sample input and output
Sample 1
Enter day (dd): 1
Enter month (mm): 3
Enter year (yyyy): 2024
Day number of the year: 61Sample 2
Enter day (dd): 31
Enter month (mm): 12
Enter year (yyyy): 2023
Day number of the year: 365Sample 3
Enter day (dd): 29
Enter month (mm): 2
Enter year (yyyy): 2023
INVALID DATEDry run
Take the input 1, 3, 2024. The constructor stores dd = 1, mm = 3, yyyy = 2024. isValid() finds the year and month in range, last = days[2] = 31 and $1 \le 1 \le 31$, so it returns true.
dayNumber() has mm - 1 = 2, so i takes the values 0 and 1:
| Step | i | days[i] | total |
|---|---|---|---|
| Start | - | - | 0 |
| Loop | 0 | 31 | 31 |
| Loop | 1 | 28 | 59 |
mm > 2 and 2024 is a leap year | - | - | 60 |
Return total + dd | - | - | 61 |
main prints the day number 61, as in the first sample run.
Variations
- Date Plus N Days "Read a date and a number N and print the date after N days."
- Days left in the year "Print the number of days remaining in the year after the given date."
- Date from day number "Read a day number and a year and print the date in dd-mm-yyyy form."