Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

enter a date string in the format mm/dd/yyyy and then it reads the user's string. You need to complete the methodcheckDate so that it tests

enter a date string in the format mm/dd/yyyy and then it reads the user's string. You need to complete the methodcheckDate so that it tests whether the date string contains a valid date. If the date string is not valid, the checkDate method should output a message explaining why the date is not valid.

A valid month value mm must be from 1 to 12 (January is 1) and it must contain two digits. The day value dd must be from 1 to a value that is appropriate for the given month and it also must contain two digits. The year value yyyy must contain four digits and be a positive number. September, April, June, and November each have 30 days. February has 28 days except for leap years when it has 29. The remaining months all have 31 days. A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400.

In the assignment's zip file there are some files that let you test your program. Make sure your solution file Hw4.java is in the unzipped hw4 folder and compile your Hw4.java file to produce Hw4.class. Then use your mouse to double-click on the file run_test_script.cmd. That causes a window to pop up that runs your program using input from the file test_cases.txt. There should not be any error messages in the pop up window. The output from running your program will be in a newly created file called test_script_output.txt. Your output should lookexactly like the contents of the file test_script_output_correct.txt from the assignment's zip file.

Here are a few hints and comments about your program.

The checkDate method needs to find the month, day, and year substrings of itsdateString parameter. Use the indexOf() method, with the parameter "/", to break the date string into three substrings, one for each of the month, day, and year (see page 167 of the textbook).

After you have the three substrings for the month, day, and year, you need to convert those strings into int values. This is done with a line of code like the following (for the year).

 int year = Integer.parseInt( yearString ); 

As soon as you find a problem with the user's input, output an appropriate error message and return from the checkDate method. Here is an example of how that might look.

 if ( 4 != yearString.length() ) { System.out.println("Error with " + savedDate + ": The year must have four digits."); return; // exit from the checkDate method } 

You need to determine which years are leap years. This is tricky. The main tool for doing this is the "integer remainder" operator, %, which is described on pages 68-70 of the textbook. So, for example, the year is divisible by 4 when 0 == (year % 4)is true. A year is not divisible by 100 when 0 != (year % 100) is true.

these are the numbers that will need to be put in1/12/2010 07/4/2011 15/01/1900 04/-1/1900 04/001/1900 11/12/99 01/32/2015 04/31/2015 02/29/2015 02/30/2016 02/29/1900 02/30/2000 12/31/2015 02/29/1968 08/31/9999

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

Step: 3

blur-text-image

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

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago