|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Work through today's lab with a partner.
We are employed by a software team writing a web calendar. You are charged with handling and processing dates. (Somebody else is writing code for calendar-entries; they will be using your code.)
Create several examples of the data (Dates), via BlueJ's pull-down menus. In fact, let's create all the Dates we'll want for later testing right now. (You might want to look at the next few exercises, to think about what you'll need test cases for.) Be sure to name your variables descriptively, since you'll be using them for a while!
There is one more nice feature to BlueJ's testing: if you right-click on the khaki test-class, you can select Bench to test fixture. While it looks like all your fine instances have disappeared, really BlueJ memorized everything on the bench and made a “test fixture” -- a bunch of objects which BlueJ will re-create every time you create a new test case.
Note that we finally can finally un-gray the first two items on The Design Recipe: The first step of solving a problem is to decide what information to represent (make a class with fields), and the second step is to make examples of this new type of data.
What if somebody made a date which corresponds to the 31st of February? What about the 29th of February? The -3rd of the 17th month? We'll let them create the date, but we'll also make a method to check whether a date is actually valid.
Consider a Date method
boolean isValid(), which
takes no inputs, and returns true
if the Date represents a date which actually occurs
in the Gregorian calendar.
Write a stub method (which always returns, say, true);
then generate test cases for it. Do not write the actual function.2
Consider a method which, if you walk up to today's Date
and hand it somebody's birthday Date, the method will return
how many years old that person is.
The signature for this function is very interesting:
don't pass in a day/month/year, instead pass in just one thing,
a Date!
Date today = new Date( 2007, 2, 14 ); Date ianBDay = new Date( 1966, 9, 1 ); today.calculateAge( ianBDay ) // Should return 40. |
Write a stub function, and then set up your test cases. Do not write the actual function.
Implement isValid.
Hint: you'll find you want to write an auxiliary, helper function
isInLeapYear().
Challenge problem: implement calculateAge.
Note that inside the method call of today.calculateAge( ianBDay ), you can call both this.getYear() (which will return 2007), as well as yourParamName.getYear() (which will return 1966). Up until now, we'd always used this inside of our methods, but now we can see that sometimes we'll call methods on other objects. Thus, it's nice to mentioning which object you are asking getYear of, even if most of the time it is merely this.
1Note that String is also an immutable type, as are the not-yet-discussed classes Integer and Double. You might ask a String to return (say) an upper-case version of itself, but in Java you'll never change a String. This is just like math, where we had numbers like 17 and -5, but we never change 17 into -5 or any other number; 17 is always there, unchanging. ↩
2 While you certainly know how to write this function, as your test cases lead you to realize, getting the function totally correct involves checking many corner cases. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Oct.10 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |