RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

hw04
Constructors; passing objects
hw04

due Oct.12 (Tue) at the start of class.
However, try to see if you can complete it by Monday.

modeling Treasures

A Treasure is a relatively simple class: every treasure has four attributes: a name, a description (a complete sentence), a weight (in lbs), and an image-URL. Some examples might be:

name: a small nugget
description: Upon close inspection, the nugget glints of gold!
weight: 0.05 lbs
image-URL: “http://americanhistory.si.edu/dynamic/images/collections_large/98-4804_225px.jpg

name: a fancy pen
description: This pen can write in purple, green, and plaid.
weight: 0.1lbs
image-URL: “

name: chocolate egg
description: The size of an ostrich egg, but made entirely of dark chocolate.
weight: 35 lbs
image-URL: “

  1. Create a class with these attributes.
  2. Write a constructor which takes the required information as parameters, and initializes the fields according to the provided values.
    Do not call super; instead initialize the fields as discussed in Friday's lecture.
  3. Make a function void testTreasure() which creates at least three example Treasures different from the ones above. (Note that the description should be a complete sentence, thought it may well be a short complete sentence.)
    (You'll use those for your test cases later, and add test classes for the functions below inside testTreasure().)
  4. Make a named constant1 UNKNOWN_IMAGE_URL, whose value is "http://ru-itec120.pbwiki.com/f/Question_mark.svg.png" (as discussed in Friday's lecture). Now, go and modify your constructor: if the image-URL provided to the constructor is the empty string, then initialize the corresponding field to be the unknown-image-URL, instead of the empty string.
  5. Write the method toDebugString, which takes in a Treasure and returns a text-representation for it, intended for programmers to read when debugging. Think of it as a text-version of BlueJ's Inspect window. For example, it might return "[name: 'chocolate egg'; description: 'The size of an ostrich egg, but made entirely of dark chocolate.'; weight: 35.0; imageURL: 'http://americanhistory.si.edu/dynamic/images/collections_large/98-4804_225px.jpg']" 2

  6. Write the method toPrettyString, which takes in a Treasure and returns a short, “pretty” text-representation of the object, intended for end-users to read. For example, "chocolate egg (35.0 lbs)"
  7. Write a function isLint, which takes a Treasure and returns true iff it is lint. It suffices to check that its name equals "lint" and that its weight is exactly 0.0. (Remember: don't use == to see if two strings are equal.)

  8. Write a predicate isBetterThan which takes in two Treasures (called, say, “thisTreas” and “thatTreas” in that order), and returns whether or not thisTreas3 is better than thatTreas. For the moment4, we'll say one treasure is better than another if it weighs more.
  9. Write counterfeit, which takes a Treasure and returns a brand new Treasure whose name and image-URL is the same as the original, the weight is half of the original weight, the description is the same as the original's description except that “, but it feels a bit chintzy” is inserted just before the ending punctuation.

    Printing the actual and expected results isn't so helpful when the result is an instance of a class we've defined; it simpley prints as “Treasure@6ee3849c” or so. Instead: have the actual/expected objects, but either call and print toDebugString, or you can call Object120.equals(Treasure,Treasure) and verify it returns true.


Include test cases and javadoc for each function you write. You can have all your tests in a single function (so that you can re-use the same variables/examples-of-data for all your tests). As always, write these tests and comments before writing the actual code; the comments will help you focus on what the method does. PIs have been instructed to not help you on a method, if you don't have those comments already written.


1Recall that we've decided named constants should actually be fields, not local variables.      

2Note that our method is adding the single-quotes, just like any other punctuation. You can actually embed double-quotes instead, if you prefer, but you have to be sneaky: use the two characters \" in the middle of the String: "ab\"cd" is a string whose length is 5; the third characters is a quotation-mark. The backslash is needed to tell java “The next character is a literal quotation mark, not the end of the String”.      

3 I'd prefer calling the parameters “this” and “that”. (After all, we already know from the signature that they're of type Treasure.) However, “this” is a reserved word in Java; we'll actually start using it soon.      

4If you want to implement some different criterion for what makes one treasure better than another, go ahead. Be sure to document it in your comments.      

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Dec.11 (Sat)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme