Radford University ITEC

ITEC120-ibarland (incl. office hrs)infolectureslabshws

lab12: ArrayList

Nov.10 (Fri):
You can work on today's lab individually, or in pairs, as you like. It will not be checked off, but you'll want to use part of it in a future (non-pair) homework, so you do need to get it done!

Recall lecture's use of ArrayList.. You may want to also look over the ArrayList documentation.

  1. Interactively in BlueJ's code pad, Create an ArrayList of Doubles.
  2. Still in Code Pad, experiment with the methods add, toString, get, size. Can you make a list which contains the same number twice?
  3. Make a backup/archive copy of your hw05. For this lab, write a method static testTreasureList(). This method will create three Treasures, and put them into a list. Print out the list's toString to System.out.
  4. Using a for-loop and the method Treasure ArrayList<Treasure>.get(int), add up the weight of every Treasure in the list. (You can do this from within the method testTreasureList, if you like.)
  5. Let's make a function which can print a nicer version of a Treasure-list. It should take in a ArrayList<Treasure> as a parameter, and return a String: list only the Treasure's name, preceded by a number (starting with 1, not 0!)
    1) a chocolate egg
    2) a multi-colored pen
    3) a small nugget
    
    Note that this will have to be a static method, presumably inside your class Treasure.
  6. Be sure to save a copy of today's lab, so that you can recover this code in a week or two.

ITEC120-ibarland (incl. office hrs)infolectureslabshws


Nov.10 (Fri)

Today's program will be will not be checked off, but any of the first three problems is fair game for exam2, so you should be practiced at writing such methods; be sure to come to office hours if you are unable to complete the problems in lab. You may work individually, or with one other person (but not somebody you've already done a 120 lab with in the last couple of weeks).

For today's lab, make a new class, though it should be in the same project as Treasure. (You can use another class like Dog instead of Treasure, if you like.)

  1. In a static test method, create at least three Treasures (you can paste from below), and create at least four different lists of Treasures. Make sure to include: (a) an empty list; (b) a list with exactly one item; (c) a list with several different items; (d) a list with the same identical item repeated several times. We'll uses these lists in our testing, which is why we want to be sure to include some degenerate types of lists.
  2. For the following method,
    1. Write at least three calls to the method: one passing in an empty list, one passing in a list with no matches, and one passing in a list with ≥ one match. (Make more test cases, if there are more plausible cases to check.) Indicate what the return value for each test should be.
      /** Determine if a certain-named Treasure is in a list of Treasure.
       * @param swag the list of Treasures to search through.
       * @param targetName the name to look for.
       * @return whether or not any Treasure in swap has the name targetName.
       */
      static boolean containsName( ArrayList<Treasure> swag, String targetName )
      
    2. Since the javadoc comments and the method's signature have been provided, you can now proceed to the next step, and actually write the code for the method.
      A few standard things to keep in mind, when writing methods:
      • Describe the process you used to write your test cases: How did you generate your expected output, from the input?
      • What pieces of info do you have to work with? In this case, targetName and swag. What types are these? What are some existing methods you can use on things of this type?
      • If you have an element of of swag (one you got your hands on via get or a for-each loop), what type is that element? What are methods you can call on such an element?
      • Remember to use .equals (not ==) to check whether two Strings have the same sequence of characters .
    3. Test this method before proceeding.
  3. We say two things are namesakes of each other, if they have the same names (their name fields are .equals), but the things themselves are not ==. For example:

    Treasure t1 = new Treasure( "egg", "a jewel-encrusted Faberget egg", 10 );
    Treasure t2 = new Treasure( "egg", "a chicken egg, hard-boiled.", 0.2 );
    Treasure t3 = new Treasure( "a chocolate egg", "The size of an ostrich egg, but made entirely of dark chocolate!", 35 );
    Treasure t4 = t1
    
    In this example, t1 and t2 are namesakes, but t1 and t3 are not namesakes (different names entirely) and t1 and t4 are not namesakes (they are identically5 the same Treasure).

    1. Write at least three calls to the following method: one passing in an empty list, one passing in a list with no matches, and one passing in a list with ≥ one match. (Make more test cases, if there are more plausible cases to check.) Indicate what the return value for each test should be.
      /** Determine if a Treasure in a list has the same name as a given Treasure.
       * @param swag   The list of Treasures to search through.
       * @param target The Treasure to look for a namesake of.
       * @return true iff some Treasure in swag is a namesake of target.
       */
      static boolean containsNamesake( ArrayList<Treasure> swag, Treasure target )
      
    2. Since the javadoc comments and the method's signature have been provided, you can now proceed to the next step, and actually write the code for the method.
      A few standard things to keep in mind, when writing methods:
      • Describe the process you used to write your test cases: How did you generate your expected output, from the input?
      • What pieces of info do you have to work with? In this case, target and swag. What types are these? What are some existing methods you can use on things of this type?
      • If you have an element of of swag (one you got your hands on via get or a for-each loop), what type is that element? What are methods you can call on such an element?
      • Note that (alas) there is no way to reduce this function to simple call to the previous one.
    3. Run your already-written test cases, and carefully make sure the output matches your expected output.
  4. For the following method,
    1. Write at least three calls to the following method: one passing in an empty list, one passing in a list with no matches, and one passing in a list with ≥ one match. (Make more test cases, if there are more plausible cases to check.) Indicate what the return value for each test should be.
      /** Return a list of all the namesakes of a treasure, in a list.
       * @param swag the list of Treasures.
       * @param target The Treasure to look for namesakes of.
       * @return all namesakes of target in swag.
       *   (The returned list is a new list which didn't exist before this
       *    function was called.  It might be empty.)
       */
      static ArrayList<Treasure> collectNamesakes( ArrayList<Treasure> swag, Treasure target )
      
    2. Since the javadoc comments and the method's signature have been provided, you can now proceed to the next step, and actually write the code for the method.
      A few standard things to keep in mind, when writing methods:
      • Describe the process you used to write your test cases: How did you generate your expected output, from the input?
        (Hint: you had to create a new list, which hadn't existed before. How do you create new lists, in Java?)
      • What pieces of info do you have to work with? In this case, target and swag. What types are these? What are some existing methods you can use on things of this type?
      • If you have an element of of swag (one you got your hands on via get or a for-each loop), what type is that element? What are methods you can call on such an element?
      • Remember to use .equals (not ==) to check whether two Strings have the same sequence of characters .
    3. Run your already-written test cases, and carefully make sure the output matches your expected output.
  5. Optional challenge: First write some test cases, and then write the method, and then test:
    /** Given two lists, find all the elements in the first which
     *  contain a namesake in the second list.
     * @param swag1 The first  list of Treasures.
     * @param swag2 The second list of Treasures.
     * @return a list of all elements which have a namesake in the other list.
     *   Examples:  Suppose t1a, t1b, t1c are namesakes, as are t2a, t2b, t2c,
     *              and t3a, t3b.
     *  namesakes( {t1a, t2a, t2c, t3a, t4, t5},
     *             {t1b, t1c, t2a, t2b, t2c, t3a, t4, t7 } )
     *    = { t1a, t2a, t2b, t2c }
     */
    static ArrayList<Treasure> namesakes( ArrayList<Treasure> swag1, ArrayList<Treasure> swag2 )
    
    Hint: use the Arraylist method addAll. This is a four-line method, like the previous few. Of course, you'll want to call collectNamesakes method.
  6. Math challenge, for pondering only:

5 Okay, technically we should say that something is trivially its own namesake. Consider our term ‘namesake’ to mean ‘nontrivial namesake’.      back

ITEC120-ibarland (incl. office hrs)infolectureslabshws


©2006, Ian Barland, Radford University
Last modified 2006.Nov.15 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme