RU beehive logo ITEC dept promo banner
ITEC 120
2007fall
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab06a
constructors with arguments
lab06a

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.)

  1. What pieces of information comprise a date? Write a class which contains those fields.
  2. Write a constructor which creates instances of your class Date.
    Don't worry (yet) about people calling your constructor with bogus arguments (like the -3rd of the 17th month).
  3. 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.

  4. Write getters for each field. Note that Dates will be immutable1 -- we will never be changing the 4th of July, 1776 into August 6, 1945. Thus, we don't need to write setters for this class. (Note that a calendar-event-object might get rescheduled, in which case it will create a different date-object, but that's not your concern.)
    Write a test case, testing each getter.
  5. Write a method String toPrettyString() for Dates; this method will return the date in a nice format of your choice. (Presumably, you're result will have numbers for the month and day; many fancy applications (Word, Windows, Mac OS) go to some length to present dates more appropriately.)
  6. 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

  7. 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.
        
    (These are test cases -- you can type this into CodePad,k but don't include it inside your Class Date! If you did, Java would think that every Date has two additional fields, today and ianBDay!)

    Write a stub function, and then set up your test cases. Do not write the actual function.

  8. Implement isValid.
    Hint: you'll find you want to write an auxiliary, helper function isInLeapYear().

  9. 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.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Oct.10 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme