Write a program that converts dates from numerical month–day format to alphabetic month–day format. For example, input of 1/31 or 01/31 would produce January 31 as output. The dialogue with the user should be similar to that shown in Programming Project 2. You should define two exception classes, one called MonthException and another called DayException. If the user enters anything other than a legal month number (integers from 1 to 12), your program should throw and catch a MonthException. Similarly, if the user enters anything other than a valid day number (integers from 1 to either 29, 30, or 31, depending on the month), your program should throw and catch a DayException. To keep things simple, assume that February always has 28 days.

The big problem that influences the design of this project is that the numbers entered are ASCII strings and not integers. The processing that needs to be done (check for valid month numbers, check for valid day numbers, and translate from month numbers to month names) is much easier if the month and day are integers, so that is the approach taken in the solution shown here. The first problem is to parse the input to get the one or two ASCII character digits for each of the data items, month and day. The program uses String methods to find the position of the slash character, /, that separates the two, then usees it to obtain the one or two characters before it for the month, and the one or two characters after it for the day. Next it converts the one- or two-character digits to the decimal integers they represent. A relatively “clean” solution is to write a helper method that converts one ASCII digit-character into its integer value, then use the helper method to convert the one- or two-character month and day values to integers. The code to convert an ASCII integer character to a decimal integer value is written as a switch structure. The month must be converted before day because it is used to determine if the day value is valid. Once the month input is converted to a number it is easy to do the remaining processing. The month can be easily checked for validity (only 1 through 12 are valid values), used as an index into a String array to get the month’s name, and used to check the day number for validity (e.g. if the month number is 1, 3, 5, 7, 8, 10, or 12, the day number must be in the range 1 – 31). The switch structure is also a very good choice for the day-check since it is very compact and readable. Organizing the steps to keep month processing separate from day processing allows the try/catch blocks for the two exceptions to be cleanly and clearly separated. So the solution here is organized as follows:
• Parse the input string to get the month part and the day part.
• Convert the month part to an integer, if possible.
• Check the month for validity: convert day number only if month is valid. It is in this block that a MonthException is thrown for any invalid input for month or if the slash character that separates month and day is missing. Note that any 3-character number for day or month is considered invalid. So, while 01/01 is accepted and converted to January 1, 001/01 and 01/001 are flagged as invalid.
If month is a valid number, convert day to its integer value and check for validity. It is here that a DayException is thrown for any invalid day input.

See the code in MonthException.java, DayException.java, and DateFormatConverter.java.

Computer Science & Information Technology

You might also like to view...

A technician moves a VoIP phone to a new location, but discovers the phone does not start up when plugged into the network. Which of the following explains this situation?

A. The switch does not support PoE. B. The jack needs to be rewired using the 568B standard. C. QoS is not enabled on the switch. D. The phone needs a fiber port

Computer Science & Information Technology

Which of the following devices is used for connecting multiple network hosts where the physical signal is always repeated to all ports?

A. Bridge B. Router C. Hub D. Switch

Computer Science & Information Technology