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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw04
Constructors; passing objects
hw04 soln

Was due Feb.19 (Mon) 17:00.

Short answer

  1. The purpose of a constructor is to make sure that                                         
  2. 01.      class Glarkzle {
    02.
    03.        int MAX_ORDER_SIZE = 25;
    04.
    05.        int numFloobs;
    06.        String name;
    07.
    08.        Glarkzle( int _numFloobs, String _name ) {
    09.          /* ... body not shown ... */
    10.          }
    11.
    12.        // Setters/getters not shown...
    13.
    14.        String placeOrder( int numOrdered ) {
    15.          /* ... body not shown ... */
    16.          }
    17.
    18.        }
          
    1. Which line(s) declare a named constant?                 
    2. Which line(s) declare a field?                 
    3. Which line(s) declare a constructor?                 
    4. Which line(s) declare a constructor?                  ignore
    5. Declare a variable to hold an object of this class. (Don't do anything more):                 
    6. Write an expression which calls the constructor, storing the result in the variable you just declared above.                 
    7. Write an expression which calls the placeOrder method of the instance you constructed, concatentating "Your order ID: " to the front of the result.                 
    1. T/F: a setter method takes one argument.                 
    2. T/F: a setter method doesn't return a value.                
    3. T/F: compiling a class causes one instance of that class to be created.                
    4. A predicate function (recall lect03a) is a function (method) which returns an answer of type                 .
    5. A function which looks up (returns) the value of a field is called a                 .
    6. A function which stores a given value into a field is called a                 .
    7. Suppose you are writing a method which doesn't return any answer (that is, the only reason anybody would call the function is to change state, or “have a side effect”). In the signature for that method, in the place of the return type, there will instead be the java keyword         .
  3. Would you use a field, or a local variable, for each of the following? Imagine we were extending class Dog from the previous homework.
    1. boolean hasFleas; // Whether or not this dog has fleas.
    2. double DOG_YEARS_PER_YEAR = 7.0; // A named constant.
    3. // Inside a method, name a partial result:
      int ageToReturn = this.getAge() * DOG_YEARS_PER_YEAR;
  4. For this problem, you might want to look at the documentation for class String. Hint: String's full name is java.lang.String; see the links at the top of each class web page.
    1. What is the result of typing "abcde".charAt(2) in the Code Pad? (Include any quotation marks, but note that “"” does not occur in the answer!)
      (You can see also the optional part of lect06a: the truth about strings)
    2. According to the documentation for String, what is the return type of charAt(int)?
      (Note that your answer does not start with a capitalized letter, so the return type isn't a class; it is actually the last of the primitive types we'll mention in class1).
    3. What is a String method which determines whether one String is equal to another, ignoring case? What do you type in to Code Pad, to ask if "theRearEarthAtStorehouse" equals (ignoring case) "thereAreArtHatsToRehouse"?
      (Okay, you can simply compare hiThere to hitHere if you want to do less typing.)
    4. Note that in the last two items, both situations involve walking up to some particular String object, and asking it a question. In the first case the input happens to be an int, and in the second case it happens to be another String, but otherwise the general task is the same.

modeling Treasures

A Treasure is a relatively simple class: every treasure has four attributes: a name, a description, 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 those attributes.
  2. Write a constructor which takes the required information as parameters, and initializes the fields according to the provided values.
  3. Create getter methods for each field.
    Do not make setter methods; once a treasure is created, it will never be modified!
    We'll actually defer test cases until part (e) below.
  4. Make a named constant2 UNKNOWN_IMAGE_URL, whose value is "http://ru-itec120.pbwiki.com/f/Question_mark.svg.png". 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 that provided value.
  5. Write the method toDebugString, which takes no inputs, and returns a text-representation of the entire object, intended for programmers to read when debugging. Think of it as a text-version of BlueJ's Inspect window. For example, "[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']" 3

    When writing a debugging-info method, it suffices to only test this method, instead of testing all of your getters4 individually.
    This is only allowed for the case of testing getters, because they are almost always this simplest of functions.
    Of course, your method should be calling the getters, rather than accessing the fields directly! (For your test cases, you presumably want to have a treasure with a shorter description and image-URL than shown here. You'll need at least two test cases, since you want to be sure your part (d) works.)

  6. Write the method toPrettyString, which takes no inputs, 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 Treasure method boolean isLint(), which returns true if the “treasure” is lint. It suffices to check that its name equals "lint" (ignoring case), and that its weight is exactly 0.0.

  8. Not required, but a few points of extra credit: See lect06c
    Write a Treasure method isBetterThan( Treasure other ), which returns whether or not this is better than other. For the moment5, we'll say one treasure is better than another if it weighs less.

(Optional, but will count for towards participation points:)
See http://ru-itec120.pbwiki.com/grue-data-2007spring (password: “highlander”). Read the instructions closely, and then edit the wiki page and add at least two treasures to that page.

If you're curious about where this assignment might be headed, you can check out last semester's assignments The “gRUe” projects shown there are similar to what we'll be writing. (It will be several weeks before we get to part II of this homework, though.)


As usual, include javadoc code for each class you write. Write these 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.
Similarly, if you are requesting help on a method, and don't have all other already-written methods called and tested from your test method, you must do that before receiving later help.


1So the primitive types you need to know are boolean, double, char, int (integers up to ±2 billion (approx)), long(integers up to ±10 quintillion (approx)). The primitive types which you don't need to know are float (single-precision floating-point), short (integers up to ±32767), and byte (integers up to ±128).      

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

3Note 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”.      

4Note that if were writing setters for this object, we would still need to test those, by interleaving calls to the setters with calls which check whether the fields were actually set.      

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.      

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