Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program in java please In this assignment you are to determine if a date entered by the user is valid. The user is to enter

Program in java please

In this assignment you are to determine if a date entered by the user is valid.

The user is to enter a date in this format:

mm/dd/yyyy

where mm represents the month, dd represents the day and yyyy represents the year. Your code MUST accept the date as a single string.

Your code must print to the screen if the date is or is not valid. If the date is not valid you are to print the reason. A date entered may be invalid because:

  • invalid length: the string must be 10 characters in length
  • invalid format: missing / or / in incorrect position(s)
  • invalid month: must be 1 - 12
  • invalid day: must be 1 - 30, 1 - 31, 1 - 28, 1- 29 depending on month and year (in a leap year February has 29 days)
  • invalid year: must be >= 0 and <10000

You may assume that the user entered a string consisting of digits (0-9) and forward slashes (/). You do not have to deal with the user entering letters or symbols.

Hint: to convert a String to a number we use the parseInt method of the Integer class. For example if we have:

String year = 12;

we can convert it to a number with this code (I will use the variable name yearInt to make it easier to remember):

int yearInt = Integer.parseInt(year);

Hint: a leap year is a year that is evenly divisible by 4, if the year is evenly divisible by 100 it must also be evenly divisible by 400, for example the year 2000 is a leap year, the year 1900 is not. Here is some code you could use to determine if a year is a leap year

int yearInt: // in this variable you store the value of the year entered

boolean leap; //declare a boolean

leap = ((yearInt % 4 == 0) && (yearInt % 100 != 0)) ||

(yearInt % 400 == 0); //this returns true or false

I suggest that you test your program before submission. Here are some test date you could use in testing:

These dates are valid:

  • 01/01/2014
  • 02/29/1964
  • 02/29/2000
  • 12/01/2014
  • 01/31/2014

These dates are not valid:

  • 02/29/1963
  • 02/29/1900
  • 13/01/2014
  • 12/33/2014
  • 12/31/14
  • 12/1/14

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago