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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw06

hw06

Due Mar.26 (Mon) 17:00.

Short answer

  1. Frooble f1 = new Frooble();
    Frooble f2;
    f2 = new Frooble();
    Frooble f3 = f1;
    f3 = f2;
        
    After evaluating all lines of the above the code fragment,
    1. How many Froobles exist?                 
    2. How many variables of type Frooble exist?                 
    3. f1 == f2?                 
    4. f1 == f3?                 
    5. f2 == f3?                 
    6. (Extra Credit, 1pt) f1.equals(f2)?                                                                                                                                                                                                                                                 
  2. To test whether two String (reference)s s1,s2 have the same characters, use s1                s2
  3. To test whether two PizzaServer (reference)s p1, p2 refer to identically the same object, write p1                p2
  4. (A one-character answer:) The general syntax of a for-each loop is:
    for ( Type namevariable      expressioncollection ) expressionloop-body

modeling Treasures

Start a new project, and copy in your class Treasure from hw04. We will add a second class to the same project.

An Explorer has a name, and two pockets (left and right) which each contain one Treasure.

  1. Draw a picture of three objects: two Treasures (neither of which happens to have the name “lint”), and one Explorer who has is “carrying” those treasures in its pockets. (There are no variables in this picture -- only objects.) Note that the English concept “an explorer carrying a treasure” is represented in our program by “an Explorer object has a reference to a Treasure object”.
  2. Copy/paste your picture from above, and modify it to represent what things would look like if an Explorer dropped the Treasure in their right pocket, which now contains lint.
    More precisely: add third Treasure object to your picture (the new lint which suddenly exists), and update the Explorer's fields appropriately.
    Note that there are now three Treasures, although the Explorer has no way to get (a reference to) the “dropped” Treasure.
  3. Create a constructor which takes just a name. It creates a new Explorer with the given name, and lint in each pocket.
  4. Write a toString method for Explorers.
  5. Write a method String inventory(), which returns a detailed listing of the Explorer's two pockets.
  6. Write String dropLeft() which replaces the contents of the left pocket with lint and returns a String saying what happened, e.g. “You drop a chocolate egg.”. Similarly, write String dropRight(Treasure).
  7. Write method String grab(Treasure), which takes a Treasure as a parameter.
  8. Go back and implement a weight limit for Explorers: if an Explorer tries to grab a Treasure which would bring the combined left- and right-pocket Treasures' weights above the limit (perhaps 50lbs.), then the pocket stays the same, and it instead the method returns a message along the lines of “The chocolate egg is too heavy to pick up.”
Turn in documentation, code, and unit-tests for Explorer. (Note that Treasure wasn't actually modified for this homework, so you don't need to turn in any printouts of that class.)

As usual, follow The Design Recipe. PIs have been instructed to not help you on a method, if you don't have its comments and test-cases already written. As we've mentioned, remember to declare every field and method either public or private.


1If you prefer, you can have grabbing a third item summarily drop one of the the two existing items: “You pick up the chocolate egg, dropping a multi-colored pen”.      

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