This assignment is due Tuesday, November 5.
This assignment is based on the material from Chapters 3 and 4. This assignment makes use of the files contained in this zip file.
Complete the Java program called Hw6.java
in the assignment's zip file so that it solves the programming problem described below.
The main
method of Hw6.java
prompts the user to enter a date string in the format mm/dd/yyyy
and then it reads the user's string. You need to complete the method checkDate
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 Hw6.java
is in the unzipped hw6
folder and compile your Hw6.java
file to produce Hw6.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 look exactly 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 its dateString
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.
The easiest way to write the checkDate
method is to make it a sequence of if-statements, where each if-statement checks for exactly one kind of error (like the if-statement above for yearString
). Use the examples in the test_cases.txt
file to help you decide what your if-statements should check for.
Turn in a zip file called CS123Hw6Surname.zip
(where Surname
is your last name) containing your version of Hw6.java
.
This assignment is due Tuesday, November 5.