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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw03
Objects with state
hw03; due Feb.12 (Mon) 17:00

Reading: Chapters 1,2.

  1. Design and implement a class Dog which contains two fields: the dog's age, and what sound it makes. Initially, Dogs are 0 years old, and they make the sound “woof”.
  2. Include getter and setter methods for the age and sound.
  3. Make a test method which makes a new Dog, calls each of the two getter methods (asserting the results are as expected), then calls each of the two setter methods, and finally again calls each of the two getter methods (asserting the results are changed in the expected way). Use the unit-test tools as covered in lab.
  4. 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.

  5. Write a method which returns the age of the dog in “dog years”, which is seven times the dog's real age. (Again, you'll write a stub function and test method, before writing the actual code.)
  6. Write a method ageOneYear, which doesn't return a value, but it does cause the Dog to become one year older. Add a unit-test for this method as well.

    (You know the drill: first a stub function, then the test method, then the actual code.) Your code should call the getters and setters, but not reference the field directly.
  7. 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.

  8. Not required for this week's homework: Depending on how far we get in lecture, there might be one more problem added: Add a field for the dog's name (with a getter, but no setter). Write a constructor method which takes in a String, and uses that to initialize the name field.
    The decision on whether this is required will be posted here on Feb.08 (Thu) noon.

As usual, turn in:

Be sure to include your name (in comments) at the top of your java file.
Note: If your entire file starts with a javadoc comment describing the class, you can include an @author tag, which also shows up in the documentation.


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.      

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