RU beehive logo ITEC dept promo banner
ITEC 120

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

Constructors; passing objects

due Oct.10 (Wed) at the start of class.
However, try to see if you can complete it by Tuesday.

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: “

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

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

  1. (6pts) Create a class with these fields.

    Before each field, include a short javadoc comment describing what each field holds (including units, if applicable). Also, be sure that your class extends Object120, so that equals works correctly1 for your Treasure test-cases!

  2. (6pts) Write a constructor which takes the required information as parameters, and initializes the fields according to the provided values.
    Before Monday, you can use the mysterious call to super; but after Monday's lecture be sure to initialize the fields yourself, without calling super.
  3. (4pts) Write a function void testTreasure() which creates at least three example Treasures, at least one of which is different from the examples given above. (Note that the description should be a complete sentence, thought it may well be a short complete sentence.)

    As you begin each of the functions below, start by writing a test-case and adding it to testTreasure. Then write a signature/stub for your function and make sure that compiles, before continuing (as ever). Note that point-values for the functions below include writing tests for those functions.

  4. (5pts) Make a named constant2 UNKNOWN_IMAGE_URL, whose value is "" 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. (4pts) 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)"
  6. (5pts) Write a function isLint, which takes a Treasure and returns true iff3 it is lint. For our purposes, something is lint if its name equals "lint" (ignoring case: 1 of the pts), and that its weight is exactly 0.0. (Remember: don't use == to see if two strings are equal.)

  7. (5pts) Write a predicate isBetterThan which takes in two Treasures (called, say, “thisTreas” and “thatTreas” in that order), and returns a boolean: whether or not thisTreas4 is better than thatTreas. For the moment5, we'll say one treasure is better than another if it weighs less, and it is not lint.
  8. (8pts) 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 (that's 2 of the pts) the ending punctuation.

Although we will not be doing this for this class, here is one way a Treasure might be used as part of a bigger program: an old homework

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 your 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.

Here is some code to help get you started. Note that assertEquals is a generalized version of testEqualDoubles

  /** Run tests for all Treasure functions (besides the constructor).
  void testTreasure() {
    Treasure t1 = new Treasure( "fancy pen", "This pen can write in purple, green, and plaid.", 0.1, "" );
    // Make at least two more examples.
    // Have at least 1-2 additional tests per function.
    assertEquals( toPrettyString(t1), "chocolate egg (35.0 lbs)" );
    assertEquals( isLint(t1), false );
    assertEquals( isBetterThan(t1,t1), false );
    assertEquals( counterfeit(t1), 
                  new Treasure( "fancy pen", "This pen can write in purple, green, and plaid, but it feels a bit chintzy.", 0.05, "" ) );
  /** Check if an actual result is equal to the desired result.
   * If not, print an error message to stderr6;
   * if so, print something minimal to stdout7 (to give some slight feedback that a test was made).
   * @param  actual   The actual result from a function call.
   * @param  desired  The desired result for that function call.
  static void assertEquals( Object actual, Object desired ) {
    if (Object120.equals(actual,desired)) {
    else {
      System.err.println( "\n***Unit test failed:");
      System.err.println( "Actual : " + Object120.toString(actual) );
      System.err.println( "Desired: " + Object120.toString(desired) );

Short answer

Put the answer to these questions in a block-comment at the end of your file.

  1. (1pt) The purpose of a constructor is: to make sure that                                         
  2. The following questions refer to this class. (When asked for line(s), answer with the line-number(s).)
    01.      class Glarkzle {
    03.        int numFloobs;
    04.        String name;
    06.        static final int MAX_ORDER_SIZE = 25;
    08.        Glarkzle( int nfs, String nm ) {
    09.          this.numFloobs = nfs;
    10. = nm;
    11.          }
    13.        static String placeOrder( Glarkzle g, int numOrdered ) {
    14.          /* ... body not shown ... */
    15.          }
    17.        }
    1. (2pts) Which line(s) declare a field?                 
    2. (1pt) Which line(s) declare a constructor?                 
    3. (1pt) Which line(s) declare a named constant?                 
    4. (1pt) Declare a variable to hold an object of this class. (Don't do anything more):                 
    5. (2pts) Write an expression which calls the constructor, storing the result in the variable you just declared above.                 
    6. (2pts) Write an expression which calls the placeOrder function using the instance you just constructed; concatentate "Your order ID: " to the front of the result of calling placeOrder.                 
  3. (2pts)
    1. T / F : Compiling a class causes one instance of that class to be created.
    2. T / F : In front of every field, we include the word static.
    3. T / F : In front of every function (besides the consturctor), we include the word static.

1We'll see later how to override equals ourselves.)      

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

3“iff” means “if and only if”.      

4 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.      

5If 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.      

6“stderr” is geekspeak for “standard error” — in BlueJ this is the rarely-seen bottom half of the terminal window and it prints in red. In some environments, stderr and stdout happen to map to the same device (and output sent to those two logically-different places ends up interleaved).      

7“stdout” is geekspeak for “standard output” — the terminal window.      

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

©2012, Ian Barland, Radford University
Last modified 2012.Oct.12 (Fri)
Please mail any suggestions
(incl. typos, broken links)
Powered by PLT Scheme