Date Program ISC Computer Science 2019 Practical

Design a program to accept a day number (between 1 and 366), year (in 4 digits) from the user to generate and display the corresponding date. Also, accept 'N' (1 <= N <= 100) from the user to compute and display the future date corresponding to 'N' days after the generated date. Display an error message if the value of the day number, year and N are not within the limit or not according to the condition specified.

Test your program with the following data and some random data.

Example 1:
INPUT:
DAY NUMBER: 255
YEAR: 2018
DATE AFTER (N DAYS): 22
OUTPUT:
DATE: 12TH SEPTEMBER, 2018
DATE AFTER 22 DAYS: 4TH OCTOBER, 2018

Example 2:
INPUT:
DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYS): 45
OUTPUT:
DATE: 26TH DECEMBER 2018
DATE AFTER 45 DAYS: 9TH FEBRUARY, 2019

Example 3:
INPUT:
DAY NUMBER: 500
YEAR: 2018
DATE AFTER (N DAYS): 33
OUTPUT:
DAY NUMBER OUT OF RANGE

Example 4:
INPUT:
DAY NUMBER: 150
YEAR: 2018
DATE AFTER (N DAYS): 330
OUTPUT:
DATE AFTER (N DAYS) OUT OF RANGE

import java.util.Scanner;
class MyDate{
    public static boolean isLeap(int y){        
        if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
            return true;
        return false;
    }
    public static String computeDate(int day, int year){
        int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        String monthNames[] = {"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};
        if(isLeap(year))
            monthDays[1] = 29;
        int i = 0;
        int daySum = 0;
        for(i = 0; i < monthDays.length; i++){
            daySum += monthDays[i];
            if (daySum >= day) {
                break;
            }
        }
        int date = day + monthDays[i] - daySum;
        String suffix = "";
        if(date % 10 == 1 && date != 11)
            suffix = "ST";
        else if(date % 10 == 2 && date != 12)
            suffix = "ND";
        else if(date % 10 == 3 && date != 13)
            suffix = "RD";
        else
            suffix = "TH";
        return date + suffix + " " + monthNames[i] + ", " + year;
    }
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.print("DAY NUMBER: ");
        int dayNum = Integer.parseInt(in.nextLine());
        System.out.print("YEAR: ");
        int year = Integer.parseInt(in.nextLine());
        System.out.print("DATE AFTER (N DAYS): ");
        int n = Integer.parseInt(in.nextLine());
        if (dayNum < 1 || dayNum > 366) {
            System.out.println("DAY NUMBER OUT OF RANGE");
            return;
        }
        if (n < 1 || n > 100) {
            System.out.println("DATE AFTER (N DAYS) OUT OF RANGE");
            return;
        }
        String dateString = computeDate(dayNum, year);
        
        int nDays = dayNum + n;
        int nYear = year;
        if (isLeap(year) && nDays > 366) {
            nYear = nYear + 1;
            nDays = nDays - 366;
        }
        else if (nDays > 365) {
            nYear = nYear + 1;
            nDays = nDays - 365;
        }
        String newDateString = computeDate(nDays, nYear);
        System.out.println("DATE: " + dateString);
        System.out.println("DATE AFTER " + n + " DAYS: " + newDateString);
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory