Write a program in Java to enter the date in (dd/mm/yyyy) format and day of the week on which a project was given. If a college allots 90 days (including the project giving day) to complete the work, then calculate the date on which the project is to be submitted along with the day name. If the final submission day is Sunday, then the students get one extra day and submits the project on the next day that is Monday.
Also check the validity of the date entered and display your output in the format given below.
Test your program with the given sample data and some random data.
Example 1
INPUT:
ENTER PROJECT DATE: 1/9/2022
DAY OF THE WEEK: THURSDAY
OUTPUT:
PROJECT SUBMISSION DATE: 29/11/2022
DAY OF THE WEEK: TUESDAY
Example 2
INPUT:
ENTER PROJECT DATE: 25/11/2022
DAY OF THE WEEK: FRIDAY
OUTPUT:
PROJECT SUBMISSION DATE: 22/02/2023
DAY OF THE WEEK: WEDNESDAY
Example 3
INPUT:
ENTER PROJECT DATE: 28/6/2022
DAY OF THE WEEK: TUESDAY
OUTPUT:
PROJECT SUBMISSION DATE: 25/9/2022
DAY OF THE WEEK: SUNDAY
PROJECT TO BE SUBMITTED ON 26/9/22 MONDAY
Example 4
INPUT:
ENTER PROJECT DATE: 31/9/2022
DAY OF THE WEEK: THURSDAY
OUTPUT: INVALID DATE
import java.util.Scanner;
class ProjectDate{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("ENTER PROJECT DATE: ");
String projectDate = in.nextLine();
System.out.print("DAY OF THE WEEK: ");
String dayName = in.nextLine().toUpperCase();
if(!valid(projectDate, dayName)){
System.out.println("INVALID DATE");
return;
}
int delimiter1 = projectDate.indexOf('/');
int delimiter2 = projectDate.lastIndexOf('/');
int date = Integer.parseInt(projectDate.substring(0, delimiter1));
int month = Integer.parseInt(projectDate.substring(delimiter1 + 1, delimiter2));
int year = Integer.parseInt(projectDate.substring(delimiter2 + 1));
String w[] = {"", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"};
int m[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(leap(year))
m[2] = 29;
int i = 1;
while(i <= 7){
if(dayName.equals(w[i]))
break;
i++;
}
int j = 89;
while(j > 0){
i++;
if(i > 7)
i = 1;
date++;
if(date > m[month]){
date = 1;
month++;
if(month > 12){
month = 1;
year++;
if(leap(year))
m[2] = 29;
else
m[2] = 28;
}
}
j--;
}
System.out.println("PROJECT SUBMISSION DATE: " + date + "/" + month + "/" + year);
System.out.println("DAY OF THE WEEK: " + w[i]);
if(w[i].equals("SUNDAY")){
date++;
i++;
if(i > 7)
i = 1;
if(date > m[month]){
month++;
if(month > 12){
month = 1;
year++;
}
}
System.out.println("PROJECT TO BE SUBMITTED ON " + date + "/" + month + "/" + year + " " + w[i]);
}
}
public static boolean leap(int y){
if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
return true;
return false;
}
public static boolean valid(String date, String day){
int delimiter1 = date.indexOf('/');
int delimiter2 = date.lastIndexOf('/');
int dt = Integer.parseInt(date.substring(0, delimiter1));
int month = Integer.parseInt(date.substring(delimiter1 + 1, delimiter2));
int year = Integer.parseInt(date.substring(delimiter2 + 1));
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(dt > 31 || dt < 1)
return false;
case 2:
if(dt == 29 && !leap(year))
return false;
else if(dt == 29 && leap(year))
return true;
else if(dt > 28 || dt < 1)
return false;
case 4:
case 6:
case 9:
case 11:
if(dt > 30 || dt < 1)
return false;
}
switch(day){
case "MONDAY":
case "TUESDAY":
case "WEDNESDAY":
case "THURSDAY":
case "FRIDAY":
case "SATURDAY":
case "SUNDAY":
return true;
default:
return false;
}
}
}