KnowledgeBoat Logo
|

Computer Science

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

Java

User Defined Methods

ICSE Prac 2019

42 Likes

Answer

import java.util.Scanner;

public class DateCalculator
{
    public static boolean isLeapYear(int y) {
        boolean ret = false;
        
        if (y % 400 == 0) {
            ret = true;
        }
        else if (y % 100 == 0) {
            ret = false;
        }
        else if (y % 4 == 0) {
            ret = true;
        }
        else {
            ret = false;
        }
        
        return ret;
    }
    
    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"};
                                
        boolean leap = isLeapYear(year);
        
        if (leap) {
            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;
        
        StringBuffer sb = new StringBuffer();
        sb.append(date);
        sb.append("TH ");
        sb.append(monthNames[i]);
        sb.append(", ");
        sb.append(year);
        
        return sb.toString();
    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("DAY NUMBER: ");
        int dayNum = in.nextInt();
        System.out.print("YEAR: ");
        int year = in.nextInt();
        System.out.print("DATE AFTER (N DAYS): ");
        int n = in.nextInt();
        
        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 dateStr = computeDate(dayNum, year);
        
        int nDays = dayNum + n;
        int nYear = year;
        boolean leap = isLeapYear(year);
        
        if (leap && nDays > 366) {
            nYear = nYear + 1;
            nDays = nDays - 366;
        }
        else if (nDays > 365) {
            nYear = nYear + 1;
            nDays = nDays - 365;
        }
        
        String nDateStr = computeDate(nDays, nYear);
        
        System.out.println();
        System.out.println("DATE: " + dateStr);
        System.out.println("DATE AFTER " + n 
                            + " DAYS: " + nDateStr);
    }
}

Output

BlueJ output of 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 RANGEBlueJ output of 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 RANGEBlueJ output of 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 RANGEBlueJ output of 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

Answered By

11 Likes


Related Questions