Programming Team:  Practice Contest, 10-Feb-05:  java.util
Mt Lebanon HS 2004-5
David Kosbie

Link to the Programming Team Home Page.

Part 1 of 2This is a Full Contest.  Work as teamsGood luck!

Note:  you must start from a blank compiler (not!).

For this contest, use the problem set from the official SRU 2004 Programming Contest.

Restrict your work to just the upper division problems.

Part 2 of 2:  This is a Quick Contest.  (though you may work as teams this time..)

Note:  you must start from a blank compiler (not!).

For these problems, you can (and should) use the online API, but you must print out each API page first and then work from the printed page (which, of course, you should bring to the SRU contest, too!).

Q1.  Read in a list of integers (all on a single line) and print them out sorted by absolute value.  Note:  you must use java.util.Arrays.sort for this -- the version that requires a Comparator.

Q2.  Read in a list of strings (separated by spaces and/or newlines, terminated by end-of-file) and print out all the duplicate strings in the order they occur.  If a word occurs more than twice, print out each duplication.  Note:  you must use a HashSet -- as you read strings, first test if they are in the hashset, and if not, add them, else print them out.

Q3.  Repeat Q2, but if a word occurs more than twice, print out the word only once.  Note:  you must use a HashMap  -- as you read strings, first test if they are in the hashmap, and if so, add them as  a key with a value of 1 -- the # of times we've seen them -- stored as an Integer, not an "int".  If they are already in the hashmap, however, then change the value for the key to be 1 larger than before, and print out the string if the value is exactly 2.

Q4.  Repeat Q3, but print out the resulting words in alphabetical order.  Note: to do this, instead of printing out the duplicates as you find them, you should add them to an ArrayList (since this allows you to add elements without knowing ahead of time how many elements you'll require), then convert the arraylist to an array of objects via ArrayList.toArray(), and then use java.util.Arrays.sort() on this object array to sort it.

Q5.  Read in a series of numbers and print them out in reverse order.  Note:  to do this, you must use a Stack.  Push each number onto the stack, then while the stack is not empty, keep popping numbers off the top and printing them out.