Question

In: Computer Science

In Java INSTRUCTIONS Write a java program called InputValidation.java that will use Scanner to ask the...

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

Solutions

Expert Solution

Short Summary:

  • Implemented the program in java as per the requirement
  • Attached source code and output

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!******************


Related Solutions

3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
Write a java program that will ask for a Star Date and then convert it into...
Write a java program that will ask for a Star Date and then convert it into the corresponding Calendar date. without using array and methods.
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside of main declare two static final variables and integer for number of days in the week and a double for the revenue per pizza (which is $8.50). Create a method called main Inside main: Declare an integer array that can hold the number of pizzas purchased each day for one week. Declare two additional variables one to hold the total sum of pizzas sold...
Write a java program using the following instructions: Write a program that determines election results. Create...
Write a java program using the following instructions: Write a program that determines election results. Create two parallel arrays to store the last names of five candidates in a local election and the votes received by each candidate. Prompt the user to input these values. The program should output each candidates name, the votes received by that candidate, the percentage of the total votes received by the candidate, and the total votes cast. Your program should also output the winner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT