RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

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.
  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 item on The Design Recipe: The first step of solving a problem is to decide what information to represent (make a class with fields), and then to make examples of this new type of data.

    Note that this question is a paraphrasing of Pittges exercise #1 (doc).

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

    Write a stub function, and then set up your test cases. Do not write the actual function. Note that this question is a paraphrasing of Pittges exercise #1 (doc), modified to use Date objects rather than three separate inputs.

  8. Challenge problem: Here is an implementation of isValid, calculateAge. However, it's old-school -- meaning, more than a week old, which was before we had discussed objects having fields. So it's version of the methods don't deal with one or two Date objects, but rather three or six ints. Your mission, if you choose to accept it, is to rewrite that solution into class Date, so that it matches the signatures and test-cases you made above.

    Note that inside the method call of today.calculateAge( ianBDay ), you can call both getYear() on the parameter (which will return 1966), as well as this.getYear() (which will return 2007). 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.Aug.27 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme