Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. main reads d, m and y with a Scanner and creates ob = new DayOfYear(d, m, y). The constructor copies them into dd, mm and yyyy and fills the array days with 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 (index 0 is January).
  2. ob.isValid() returns false if yyyy is not from 1 to 9999 or mm is not from 1 to 12. Otherwise it sets last to days[mm - 1], changes it to 29 when mm is 2 and isLeap(yyyy) is true, and returns whether dd is from 1 to last.
  3. isLeap(y) returns true when y is divisible by 4 and not by 100, or divisible by 400.
  4. If the date is valid, main prints ob.dayNumber(), which:
    • adds days[i] for i from 0 to mm - 2;
    • adds 1 more if mm is greater than 2 and the year is a leap year (the extra day of February);
    • adds dd and returns the total.
  5. If the date is not valid, main prints INVALID 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

VariableData TypePurpose
ddintData member: day of the month
mmintData member: month (1 to 12)
yyyyintData member: year
daysint[]Data member: days in each month, January at index 0, February taken as 28
dintConstructor parameter and main variable: day typed by the user
mintConstructor parameter and main variable: month typed by the user
yintConstructor parameter and main variable: year typed by the user; also the parameter of isLeap(), the year to test
lastintLocal variable of isValid(): number of days in the given month
totalintLocal variable of dayNumber(): running total of days
iintLoop counter in dayNumber(): index of the month being added
scScannerReads the day, month and year typed by the user
obDayOfYearObject of the class created in main
argsString[]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: 61

Sample 2

Enter day (dd): 31
Enter month (mm): 12
Enter year (yyyy): 2023
Day number of the year: 365

Sample 3

Enter day (dd): 29
Enter month (mm): 2
Enter year (yyyy): 2023
INVALID DATE

One value per line

Dry 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:

Stepidays[i]total
Start--0
Loop03131
Loop12859
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

  1. Date Plus N Days "Read a date and a number N and print the date after N days."
  2. Days left in the year "Print the number of days remaining in the year after the given date."
  3. Date from day number "Read a day number and a year and print the date in dd-mm-yyyy form."