Write a program in Java to accept day number (between 1 and 366) and year (yyyy) from the user and display the corresponding date. Also accept ‘N’ from the user where 1 <= N <= 100) to compute and display the future date ‘N’ days after the given date. Display error message if the value of the day number or ‘N’ are not within the limit. Day number is calculated taking 1st January of the given year as 1.
Test your program with given set of data and some random data.
Example 1
INPUT: DAY NUMBER: 50
YEAR: 2023
N: 25
OUTPUT: ENTERED DATE: FEBRUARY 19, 2023
25 DAYS LATER: MARCH 16, 2023
Example 2
INPUT: DAY NUMBER: 321
YEAR: 2023
N: 77
OUTPUT: ENTERED DATE: NOVEMBER 17, 2023
77 DAYS LATER: FEBRUARY 2, 2024
Example 3
INPUT: DAY NUMBER: 400
YEAR: 2023
N: 125
OUTPUT: INCORRECT DAY NUMBER
INCORRECT VALUE OF ‘N’
import java.util.Scanner;
class DayNumber{
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("N: ");
int n = Integer.parseInt(in.nextLine());
boolean invalidDay = false;
boolean invalidN = false;
if((dayNum == 366 && !isLeap(year)) || dayNum > 366 || dayNum < 1){
System.out.println("INCORRECT DAY NUMBER");
invalidDay = true;
}
if(n < 1 || n > 100){
System.out.println("INCORRECT VALUE OF 'N'");
invalidN = true;
}
if(invalidDay || invalidN)
return;
String monthNames[] = {"", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};
int monthDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(isLeap(year))
monthDays[2] = 29;
int i = 1;
while(dayNum > monthDays[i]){
dayNum -= monthDays[i];
i++;
}
System.out.println("ENTERED DATE: " + monthNames[i] + " " + dayNum + ", " + year);
int num = n;
while(n > 0){
dayNum++;
n--;
if(dayNum > monthDays[i]){
dayNum = 1;
i++;
}
if(i > 12){
i = 1;
year++;
if(isLeap(year))
monthDays[2] = 29;
}
}
System.out.println(num + " DAYS LATER: " + monthNames[i] + " " + dayNum + ", " + year);
}
public static boolean isLeap(int y){
if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
return true;
return false;
}
}