In: Computer Science
In Java
INSTRUCTIONS Write a java program called InputValidation.java that will use Scanner to ask the computer user to enter his/her: • Name: a string representing the user’s first name • month of birthday: an integer representing the month of the user’s birthday • day of birthday: an integer representing the day of the user’s birthday • year of birthday: an integer representing the year of the user’s birthday Consider the following aspects that your program shall perform: VALIDATE USER’S INPUT • Validate user’s data: Every time your program request information from the user the month must be between 1 –12 o Certain months have 30 days and others have 31 days. Your program must check for these cases o the day must be a number between 1 – 31 depending on the month provided previously USER INTERACTION AND CASE HANDLING • The user is allowed to make five mistakes in total while entering his/her information. • Notice that the user will continue entering his/her information in a repeatable fashion. • if the user had five mistakes in total, a comment shall be displayed as follows: Hi , you entered five mistakes. Please contact your official ID and try again later. And your program shall terminate immediately1 . • If the user successfully provided the data correctly, then your program shall display a message: Thank you for entering correct data • Design a flowchart that represents the flow of information in your entire program. You can use Microsoft Visio to develop this diagram. 1 You can use the System.exit() to terminate your program. PROGRAM OUTPUT AND RESULTS • Based on the user’s input for his/her year, check if the year provided is considered to be a leap year or not. If the year the user entered is leap, then you should write a message specifying that this person was born in a leap year. • Finally write an output message based on the decision on the leap year or not. If the year provided was leap, your message shall have “were” if the year was not, it should be “were not”, as follows: FYI , you born in a leap year
Short Summary:
Source Code:
package com.core.java;
import java.util.Scanner;
/** This class gets the user input for name and birthday and
validates the same **/
public class InputValidation {
public static void main(String[] args) {
//Get input using scanner
Scanner input=new
Scanner(System.in);
System.out.print("Enter your first name:");
String
name=input.nextLine();
//Variable to store
mistakeCount
int mistakeCount=0;
//Variable to store if the month
has 30 days
boolean thirtyDays=false;
System.out.print("Enter your month of birthday:");
int month=input.nextInt();
//Validate month
while(month<1 ||
month>12)
{
//Increment
mistakecount
mistakeCount++;
if(mistakeCount<5)
{
System.out.print("Enter your month of
birthday:");
month=input.nextInt();
}
else
{
System.out.println("Hi , you entered five
mistakes. Please contact your official ID and try again
later.");
System.exit(1);
}
}
//Check 30 day months
if(month==2 ||
month==4||month==6||month==9||month==11)
{
thirtyDays=true;
}
System.out.print("Enter your day of
birthday:");
int day=input.nextInt();
boolean inValid=true;
//Get input in loop for
birthday
while(inValid)
{
if(mistakeCount<4)
{
if(thirtyDays==true && day>30)
{
mistakeCount++;
System.out.print("Enter your
day of birthday:");
day=input.nextInt();
}
else if(thirtyDays==false &&
day>31)
{
mistakeCount++;
System.out.print("Enter your
day of birthday:");
day=input.nextInt();
}
else
{
inValid=false;
}
}
else
{
System.out.println("Hi , you entered five
mistakes. Please contact your official ID and try again
later.");
System.exit(1);
}
}
//Get year of birthday
System.out.print("Enter your year
of birthday:");
int year=input.nextInt();
System.out.println("Thank you
for entering correct data!");
//Check if it's leap year
boolean
leapYear=isLeapYear(year);
if(leapYear)
System.out.println("FYI, you were born in a leap year");
else
System.out.println("FYI, you were not born in a leap year");
input.close();
}
//This method checks if enetered year is leap year or
not
private static boolean isLeapYear(int year)
{
boolean leapYear = false;
if(year % 4 == 0)
{
if( year % 100
== 0)
{
if ( year % 400 == 0)
leapYear = true;
else
leapYear = false;
}
else
leapYear = true;
}
else
leapYear =
false;
return leapYear;
}
}
Code Screenshot:
Output:
Valid scenario:
Invalid scenario:
**************Please do upvote to appreciate our time. Thank you!******************