Question
Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message that
Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12 (January is 1). The day value dd must be from 1 to a value that is appropriate for the given month. February has 28 days except for leap years when it has 29. A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400.
Please use the algorithm and the variable names in the images below. Thank You!
public class ValidDateBoolean { static Scanner scan = new Scanner(System.in); public static void main(String[] args) { //*** note variable names //*** Put one variable on one line. 1/*** note System.out.println statements for debugging String inputString; char firstCharacterOfMonth, Second CharacterOfMonth; char firstCharcterOfDay, secondCharcterOfDay; char firstCharcterofYear, secondCharcterOfYear, thirdCharcterOfYear, forthCharcterofYear; int integerValueOfFirstCharacterOfMonth; int integerValueOfSecond CharacterOfMonth; int integerValueOfFirstCharacterOfDay; int integerValueOfSecondCharacterOfDay; int integerValueOfFirstCharacterofYear; int integerValueOfSecond CharacterofYear; int integerValueOfThirdCharacterOfYear; int integerValueOfForthCharacterOfYear; int integerValueOfMonth; int integerValueOfDay; int integerValueOfYear; String StringMonthValue = null;// holding values January, or February etc. boolean isLeapYear; 1/*** goodDay, goodMonth, and goodYear start out as true. boolean goodDay = true; boolean goodMonth = true; boolean goodYear = true; Algorithm: 1) input read the date 2) Process a) Check the string length, if bad print message and end b) Check for back slashes, if not there print message and end c) Read first two characters for month d) Convert characters to numbers e) Create one number from the two numbers f) Is it in rangel-12? if not print message and set boolean goodMonth to false g) Read two characters for day h) Convert characters to numbers i) Create one day number from the two numbers j) switch on month, if bad day range print message and set boolean goodDay to false k) Read four characters for year 1) Convert characters to numbers m) Create number year n) If the year is less than 1500 and greater than 2050 it is a bad range. Print message and set boolean goodYear to false o) if February 29 set boolean leap Year p) if February 29 and not leap Year print message and set boolean goodYear to false q) if (goodMonth && goodDay && good Year) 3) Output a) switch on month and assign StringMonth Value (January etc.) b) print month c) print day d) print year
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started