|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Reading: Chapters 1,2.
Write a method speak method which returns a String: the sound the dog makes, followed by a comma and a space, followed by the sound again, followed by a period. For example, if a Dog's sound has been set to “arf”, then return “arf, arf.”.
Note that you'll initially write a stub version of the function (which doesn't return the correct answer, but does compile, and does let you call the function for recording a test method). Once you have created your test method, then you will write the actual function.
Modify the setter for the dog's age, so that it won't ever set the dog's age to a negative number. (If somebody tries, you do whatever you think is more appropriate: change the age to zero (newborn), or just leave the age unchanged, or even trigger an error.)
Note that for this class, it's okay if you never made any inputs testing negative ages, if you put a statement in your javadoc for setAge stating that the input should be non-negative.1 However, even though you don't have test cases for negative inputs, it's still not a bad idea for your code to make “sanity checks” on its data; — for example, if you made a silly typo in (say) ageOneYear, then having guarding against negative values in the setter can help things from getting too out of whack. Still, this class will focus on good design more than bullet-proof testing.
As usual, turn in:
1 Ideally, there'd be an easy way to tell the computer the type should be “non-negative integer”, and then Java could enforce that your function is never called inappropriately. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Aug.27 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |