|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
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.)
new Date( 2008, 7, 14 ) // Bastille Day! |
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!
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!
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.
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. } |
long | short | |
---|---|---|
numeric | "2008.07.14" | "08.07.14" |
text-y | "2008.July.14" | "08.Jul.14" |
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2008, Ian Barland, Radford University Last modified 2008.Feb.21 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |