ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws
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) a chocolate egg 2) a multi-colored pen 3) a small nugget |
ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws
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.)
/** 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 ) |
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 |
/** 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 ) |
/** 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 ) |
/** 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 ) |
ArrayList<Treasure> poorlyNamed( ArrayList<Treasure> tt ) { return namesakes( tt, tt ); } |
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)—info—lectures—labs—hws
©2006, Ian Barland, Radford University Last modified 2006.Nov.15 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |