RU beehive logo ITEC dept promo banner
ITEC 120
2007fall
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw06
constructors
Treasures

v1.1

Part (a)

Due Sep.28 (Fri): only hardcopy is needed.

  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 field?                 
    2. Which line(s) declare a named constant?                 
    3. Which line(s) declare a constructor?                 
    4. Declare a variable to hold an object of this class. (Don't do anything more):                 
    5. Complete the picture of what things will look like immediately after declaring the the variable, by labeling the (currently-uninitialized) box appropriately:
      the environment:
             +--------+
      ____  |        |
             +--------+
      
      (Note that the contents of the labeled-box should be empty, since you haven't initialized the variable yet.)
    6. Write an expression which calls the constructor, storing the result in the variable you just declared above.                 
    7. Draw a picture of what things will look like immediately after calling the constructor and initializing the variable. That is, complete the box for the newly-created object, as in hw05a.
      the environment:                    The object bench (the “heap”)
             +--------+                   
      ____  |    *---|-------\           
             +--------+        \          +------------------------+ 
                                \         | |
                                 \------> | |
                                          | |
                                          | |
                                          | |
                                          +------------------------+
      
    8. Write an expression which calls the placeOrder method of the instance you constructed, concatentating "Your order ID: " to the front of the result.                 
    1. true or false?: a setter method takes one argument.
    2. true or false?: a setter method doesn't return a value.
    3. true or false?: compiling a class causes one instance of that class to be created.
  3. 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?
      (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.

Part (b): Modeling Treasures

Due Oct.01 (Mon): You'll turn in three things (all stapled together):

  1. The html as usual (via Tools >Project Documentation). You will need javadoc for all your methods, including getters.
  2. Your test cases (found inside the test file which BlueJ auto-creates). You will only need test cases for isLint and isHeavierThan. (Of course, you'll only stub out these functions for (b), rather than writing the complete code.)
  3. Finally, draw a picture of two of the Treasure objects you actually created in your tests, including the local-variable used to refer to the object. (Each of the two should pictures look similar to the picture of the Glarkzle in 1g, above.) You can just hand-draw your answer on above printouts, that's fine.

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.
    Make at least three examples, and select “Object Bench to Test Fixture”. (from the khaki test-class).
  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 constant 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.

    Note: When comparing Strings, don't use ==. Instead, use the String method “equals”. We'll talk about why == isn't appropriate for comparing Strings later, but to convince yourself, you can type the following into BlueJ's Code Pad:

    	"hello" == ("hel".concat("lo"))
    	"hello".equals( "hel".concat("lo"))
          

  5. The method toPrettyString, which takes no inputs, returns a short, “pretty” text-representation of the object, intended for end-users to read. For example, "chocolate egg (35.0 lbs)"
  6. Write a Treasure method boolean isLint(), which returns true if the “treasure” is lint. It suffices to check that its name (ignoring case) equals "lint" and that its weight is exactly 0.0.

  7. Write a Treasure method isHeavierThan( Treasure other ), which returns whether or not this is heavier than other.

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

Part (c)

Due Oct.03 (Wed): implementation (Java code) for all the above methods, including toPrettyString, isLint, and isHeavierThan. Turn in hardcopy as usual. Your code should use getters, but not access any fields directly2.


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

2except for the constructor, and the getters themselves, of course.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Oct.09 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme