Problem
Add features to Event
Edit Event and give it two fields: an int representing the hour of the day at which the event occurs, and a string representing a description of the event. We will use military time to denote hours, so 0 represents midnight, 8 is 8:00am, 20 is 8:00pm, 23 is 11:00pm, etc..
Modify the constructor so that it takes two parameters: one int parameter for the time, followed by a string representing the description.
Write an accessor method called getDescription() that returns the description of an event.
Write an accessor method called getHour() that returns the int-valued hour at which the event occurs.
Fill in the corresponding test class
By modifying the constructor for Event, its test class will no longer compile. Decide on the test fixture to use for the Event class (that is, the initial conditions in which all tests are performed), and set it up.
Add a test for the constructor, using the new accessor methods to confirm it initializes the fields correctly.
Add mutator methods
Add a mutator method called setDescription() that allows an event's description to be changed. This mutator should take a single string argument.
Add a mutator method called setHour() that allows an event's time to be changed. This mutator should take a single int argument.
Write additional tests for these mutator methods (again, one test each is enough), compile and run them, and fix any errors.
Extend the Event class to handle more readable times
We would like for our Event class to be a bit more friendly. People usually do not use military hour numbers to refer to times. It might be easier to create Events if one could enter a time like "10pm" or "9am". Let's do this by adding a new mutator called setTime(). This mutator should take a single string parameter that consists of an hour number followed by "am" or "pm". It will set the field inside the Event by examining the characters of the string. First, create a skeleton of this method that does nothing.
Implement this mutator method. First, use the substring() method provided by the String class to extract a new string containing all but the last two characters (i.e., just the digits). Then use the Integer.parseInt() static method to convert the digits into the corresponding int numeric value:
1. String digitsOnly = theTime.substring(/* what goes here? */); int hour = Integer.parseInt(digitsOnly) ;
Now write a second constructor. Add a new constructor to the class that takes two string parameters: first, a string representing the time, (non-military style), and then a string representing the event description. This new constructor can call the mutator method just created to do part of its work.
Write, compile, and run new tests for the new constructor. Now there are two different ways you can create event objects: either by specifying the time as an int value, or by specifying it using a more human-readable string.