This assignment is due Tuesday, September 29.
This assignment is based on the material from Section 3.1 (and also Chapter 2). This assignment makes use of the files contained in this zip file.
Write a Java class called Hw3.java
which solves Programming Project 4 on page 195 at the end of Chapter 3 of the textbook.
In this assignment's zip file there are some files that let you test your program. Download and unzip the zip file. Copy your solution file Hw3.java
into the unzipped hw3
folder and then compile your Hw3.java
file to produce Hw3.class
.
Use your keyboard and mouse to "Shift-Right-Click" on an empty space in the hw3
folder and select the pop-up menu item "Open command window here". You should now see a "command prompt" window. Click on this window and type the following command at the prompt.
hw3> run_test_script.cmd
That command should produce some output in the command window and the output should look exactly like the contents of the file test_script_output.txt
from the assignment's zip file.
Here are a few hints and comments about your program.
After you read the user's input string, you need to find the month, day, and year substrings. Use the indexOf()
method, with the parameter "/"
, to break the input string into three substrings (similar to Assignment 2).
As soon as you find a problem with the user's input, output an appropriate error message and stop your program (stopping a program is mentioned on pages 168-169 of the textbook). 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."); System.exit(0); // stop the program }
After you have the three substrings for the month, day, and year, you need to convert those strings into integers. This is described in Figure 2.9 on page 123 of the textbook. Here is an example of how that might look.
int year = Integer.parseInt( yearString );
You need to determine which years are leap years. This is tricky. The main tool for doing this is the "integer remainder" operator, %
, which was described on page 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 this program is to make the program a sequence of if-statements, where each if-statement checks for exactly one kind of error.
Turn in a zip file called CS123Hw3Surname.zip
(where Surname
is your last name) containing your version of Hw3.java
.
This assignment is due Tuesday, September 29.