RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

Objects (constructors; passing objects)
hw05a

due Oct.25 (Fri) at the start of class. Part B due Oct.28 (Mon) at the start of class
However, you should be able to complete it before the previous class starts (bringing any questions to lecture/lab/office-hours/discussion-board).

All methods should be static (besides the constructor). If one of the inputs to a method is a STreasure, it should be the first parameter, and that parameter must be named thiss.

Part B

Take your solution to part A (or my posted solution), and re-write it in an object-oriented format:

Note: we are still keeping extends Object120 : it's important that equals works corrrectly (so that assertEquals can work), and that doesn't happen if we stop extending Object120.

Modeling Treasures

A STreasure is a relatively simple class: every (s)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: https://collections.nmnh.si.edu/media/?irn=10246678

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 correctly for your STreasure test-cases!

  2. (6pts) Write a constructor which takes the required information as parameters, and initializes the fields according to the provided values.
    As we talk about in Monday's lecture, be sure to initialize the fields yourself (without calling super).
  3. (4pts) Write a function void testSTreasure() which creates at least three example STreasures, 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 testSTreasure. 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 constant1 UNKNOWN_IMAGE_URL, whose value is "https://upload.wikimedia.org/wikipedia/commons/4/44/Question_mark_%28black_on_white%29.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 the empty string.

  5. (4pts) Write the method toPrettyString, which takes in a STreasure and returns a short, pretty text-representation of the object, intended for end-users to read. For example, "chocolate egg (35.0 lbs)"
  6. Write newLint(), which takes in no inputs, and returns a newly-constructed STreasure whose name is “lint” and weight is 0.0. (The description and URL can be anything reasonable.)
  7. (5pts) Write a function isLint, which takes a STreasure and returns true iff2 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.)

  8. (5pts) Write a predicate isBetterThan which takes in two STreasures (called, say, thisTreas and thatTreas in that order), and returns a boolean: whether or not thisTreas3 is better than thatTreas. For the moment4, we'll say one treasure is better than another if it weighs less, and it is not lint.
  9. (8pts) Write counterfeit, which takes a STreasure and returns a brand new STreasure 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 probably not be doing this for this class, here is one way a STreasure 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 STreasure functions (besides the constructor).
   */ 
  void testSTreasure() {
    STreasure t1 = new STreasure( "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( false, isLint(t1) );
    assertEquals( false, isBetterThan(t1,t1) );
    assertEquals( new STreasure( "fancy pen", "This pen can write in purple, green, and plaid, but it feels a bit chintzy.", 0.05, "" ),
                  counterfeit(t1) );
  }


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 {
    02.
    03.        int numFloobs;
    04.        String name;
    05.
    06.        static final int MAX_ORDER_SIZE = 25;
    07.
    08.        Glarkzle( int nfs, String nm ) {
    09.          this.numFloobs = nfs;
    10.          this.name = nm;
    11.          }
    12.
    
    13.        static String placeOrder( Glarkzle g, int numOrdered ) {
    14.          /* ... body not shown ... */
    15.          }
    16.
    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.
  4. 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. final 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;

1 Recall that we've decided named constants should actually be fields, not local variables.      
2 iff means if and only if.      
3 I'd prefer calling the parameters this and that. (After all, we already know from the signature that they're of type STreasure.) However, this is a reserved word in Java; we'll actually start using it soon.      
4 If 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.      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.