In: Computer Science
This has to be in java and will be uploaded onto zybooks.
Write a Java program that asks the user for a date entered as 4 integers: dayNumber monthNumber date year. Where:
dayNumber
An integer from 1-7, where 1 = Sunday, 2 = Monday, ..., 7 = Saturday
monthNumber
An integer from 1-12, where 1 = January, 2 = February, ..., 12 = December
date
An integer from 1-31 representing the date.
year
An integer representing the year.
Your prompt to the user should be:
Enter 4 integers representing dayNumber monthNumber date year:
You program will compute the proper dayName from the specified dayNumber and the proper monthName from the specified monthNumber. Your program will print out the specified date in the following format:
dayNumber monthNumber date year is dayName monthName date, year
Please note your program will have to error check the input as follows:
Please note your program will have to error check the input as follows:
Invalid day number: dayNumber, please enter a number from 1..7.
Invalid month number: monthNumber, please enter a number from 1..12.
Invalid date number: date, please enter a number from 1..31.
Invalid date: monthName, does not have date days, please enter a valid date.
Invalid date: year is not a leap year, February does not have date days, please enter a valid date.
Please note that your class should be named DateConverter.
If you have any doubts, please give me comment...
import java.util.Scanner;
public class DateConverter{
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int dayNumber, monthNumber, date, year;
String monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
System.out.print("Enter 4 integers representing dayNumber monthNumber date year:");
dayNumber = scnr.nextInt();
monthNumber = scnr.nextInt();
date = scnr.nextInt();
year = scnr.nextInt();
if(dayNumber<1 || dayNumber>7){
System.out.println("Invalid day number: "+dayNumber+ "please enter a number from 1..7.");
System.exit(0);
}
if(monthNumber<1 || monthNumber>12){
System.out.println("Invalid month number: "+monthNumber+ "please enter a number from 1..12.");
System.exit(0);
}
if(date<1 || date>31){
System.out.println("Invalid date number: "+date+ "please enter a number from 1..31.");
System.exit(0);
}
if((date>30 && (monthNumber==4 || monthNumber==6 ||monthNumber==9 ||monthNumber==11)) || (date>29 && monthNumber==2)){
System.out.println("Invalid date: "+monthNames[monthNumber-1]+", does not have "+date+" days, please enter a valid date.");
System.exit(0);
}
if(monthNumber==2 && date>=29 && !(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))){
System.out.println("Invalid date: year is not a leap year, February does not have "+date+" days, please enter a valid date.");
System.exit(0);
}
System.out.println(dayNumber +" "+monthNumber+" "+date+" "+year+" is "+dayNames[dayNumber-1]+" "+ monthNames[monthNumber-1]+" "+date+" "+year);
}
}