RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab06b
constructors with arguments
lab06a

Work through today's lab with a partner.

Here is some skeleton code, which includes some getters/setters already written for you.
Also, create a test-class (right-click on your class and choose Create Test Class), open that khaki unit-test class, and replace its contents with this unit-test file.

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. Copy/Paste the following skeleton code into your class. (We'll spend the day replacing the parts of that file indicated in all-caps.)
  3. Note that getters/setters for the day and year have been provided for you. Write a getter and setter for the month.
  4. Write a constructor which creates instances of your class Date. Write it so that people will call the constructor with the year, month (numeric), and day (in that order).
    Don't worry (yet) about people calling your constructor with bogus arguments (like the -3rd of the 17th month).
  5. Call your constructor from the code pad:
          new Date( 2008, 7, 14 )   // Bastille Day!
        
    You can double-click on the tiny red result, to inspect it.
    (Note that we called the constructor, but we didn't actually remember it in a local variable.)
  6. Create a test-class (right-click on your class and choose Create Test Class), open that khaki unit-test class, and replace its contents with this unit-test file.

    If you run the test cases right now, you'll fail them — that's because you have to write the last three methods. Once you pass those, you'll be checked off!

  7. If you walk up to a Date (say, an instance which represents July 14, 2008) and ask that object toStringUS, it will give back a String representation of itself: "07/14/2008".
    Write this method, and then see if you pass the first test case.
    Hint: The provided code includes a function which you'll find helpful: intToStringPad2. Read its comments to see what it does.
  8. Similarly, a Date knows how to answer people who ask for a String written in the European date style: "14-07-2008" for July 14, 2008.
    Before proceeding, see if you pass the appropriate test case.
  9. Finally, a Date also knows how to answer people who ask for a String written in the ISO (International Standards Organization) format: "2008.07.14" for July 14, 2008. (This format rules!)
    If you now pass all three test cases, you can be checked off!

  10. Challenge problem: When humans are reading the date, it's nicer to use the a month's three-letter abbreviation, rather than its number.

    Write toStringTxtEuro, which returns a string of the form "14-Jul-2008".
    Similarly, write toStringTxtISO and toStringTxtUS, which return answers like "2008.Jul.14" and "Jul/07/2008", respectively.

    Don't repeat code! That is, your program should not mention "Jul" more than once, even though three different methods use it!

  11. Challenge: Overloading. Write a method which is also called toStringEuro, but it takes in a boolean: whether or not to represent the month using its (abbreviated) name! That is, toStringEuro(false) will return something like "14-07-2008", but toStringEuro(true) will return something like "14-Jul-2008".

    (Notice that we two different methods, both called “toStringEuro”. How can Java tell which one we're calling? It's pretty clear: if the caller provides a boolean, Java will use this version we just wrote. if the caller provided no arguments, then Java will use the original version.

  12. Of our two versions of toStringEuro, the second version is more general: it can do everything the original version did (by passing it false), and more! Yet we still want to keep the original version around (it's convenient to call, and besides if we remove it our test cases will fail).

    But we can still get rid of any repeated code: we can have the original version's implementation call our the more general version. with the argument true provided!:

      String toStringEuro() {
        return this.toStringEuro(false);   // Just call our overloaded version.
        }
        
    If needed, re-write your code so that it contains this short version.

  13. The final challenge: Write versions which take in two booleans: whether or not to represent the month as a number vs. a name, and whether or not to try to make the entire date long or short. Here's an example for the ISO format:
    long short
    numeric "2008.07.14" "08.07.14"
    text-y "2008.July.14" "08.Jul.14"
    Hint: your code need only include a month's full name; you can use substring to extract its first three letters as needed. (See a solution, and some issues)

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Feb.21 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme