View, | Enable Editing EGR 140: Computer Programming Assignment#5: Assigned: Wed
ID: 3881205 • Letter: V
Question
View, | Enable Editing EGR 140: Computer Programming Assignment#5: Assigned: Wednesday, 6 September 2017 Due: Monday, 11 September 2017 Points: 20 Write an error-free Java program to do the following things. 1. Inputs the month (I for Jan, 2 for Feb, etc.), day of the month and the year. The program can ask the user for the month/day/year in any order The program should inform the user when a valid date has been entered. You may assume that the user will (correctly) enter integers but the inputs may be negative, too small or too big. If an invalid date has been entered, then print a brief message explaining the problem and the program terminates gracefully. For the purposes of this program you may assume that February has 28 days each year, even during leap years. (I know that this does not agree with the calendar, but it will simplify your program. Trust me on this.) 2· 3. If the entered date is a valid date, the program informs the user whether the date is part of a new century. That is, the program prints a "Welcome to the new Century" any time the year is a multiple of 100 (1900, 2000, 2100, 2200 etc.). You can assume that the year will - be greater than 1 but less than 3100 for this step. Sample output (2 separate computer runs): Input day: 31 Input day: 30 >>| Input month (1 -Jan, 2 = Feb, etc): 2 >>| Input month (1 -Jan, 2 = Feb, etc): 8Explanation / Answer
Please find my implementation.
import java.util.Scanner;
public class ValidateDate
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int month;
int day;
int year;
System.out.print("Input day:");
day = sc.nextInt();
System.out.print("Input month (1 = Jan, 2 = Feb, etc) :");
month = sc.nextInt();
System.out.print("Input year:");
year = sc.nextInt();
sc.close();
if(year <= 0){
System.out.println("Year is incorrect. Please correct");
return;
}
if(month > 12 || month < 1)
{
if(month > 12)
System.out.println("Month is too large. Please correct");
else
System.out.println("Month is too low. Please correct");
return;
}
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if (day != 31)
{
if(day > 31)
System.out.println("Day is too large. Please correct");
else if(day < 31)
System.out.println("Day is too low. Please correct");
return;
}
}
if (month == 4 || month == 6 || month == 9 || month == 11)
{
if(day != 30) {
if(day > 30)
System.out.println("Day is too large. Please correct");
else
System.out.println("Day is too low. Please correct");
return;
}
}
if (month == 2) // February check
{
if (year % 400 == 0 ||year%4 == 0 ) // Leap year check for February
{
if(day != 29) {
if(day > 29)
System.out.println("Day is too large. Please correct");
else
System.out.println("Day is too low. Please correct");
return;
}
}
else
{
if(day != 28) {
if(day > 28)
System.out.println("Day is too large. Please correct");
else
System.out.println("Day is too low. Please correct");
return;
}
}
}
// Everything checks out
System.out.println("You have entered correct date.");
if(year % 100 == 0) {
System.out.println("Welcome to the "+(year/100+1)+"th century.");
}
}
}
/*
Sample run:
Input day:30
Input month (1 = Jan, 2 = Feb, etc) :2
Input year:2010
Day is too large. Please correct
Input day:31
Input month (1 = Jan, 2 = Feb, etc) :8
Input year:1900
You have entered correct date.
Welcome to the 20th century.
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.