Homework 4 (HashMaps vs. Zombies) FAQ

Q: What are some good resources or examples I can look at to get started on this program?
A: Look at the HashSet example from Friday's lecture, the one that uses separate chaining. In particular, check out the debug method in that file, because it is a helpful way to print out the entire hash table state. If you want to read more about implementing a collection using an array, you can read this sample Building Java Programs Chapter 18 hashing (please do not distribute!).
Q: I think I finished Part A, but my output is in a different order so it doesn't match. How do I check my output for Part A?
A: If your hashCode function is not exactly the same as ours, your output will display the sets/maps elements in a different order. This does not mean that your output is wrong, just that your hash code algorithm is slightly different. You do not need to match ours exactly.

The Output Comparison Tool has "Ignore exact order of lines" and "Ignore order of chars on each line" boxes you can check that will help you match our output more precisely.

Q: In Part A, why do I get an IllegalStateException saying that the simulation does not contain a given name? I look at the prior output and I do see that name in the simulator.
A: This error means that the person cannot be found in the hash map/set even though they are expected to be there. If you are using Java's HashMap and see this message, it probably means your HvZPlayer has an improper equals or hashCode method, so the map/set cannot find them. If you are using your own HashMap, it probably means that you are not properly finding a key or value that should be in the map. A common cause of this is when you are not comparing keys properly, like if you use == to compare them rather than .equals.
Q: On Part B, I get a compiler warning or a casting exception when I try to cast my hash table of Node objects. Is that okay? What should I do?
A: That's just a Java thing; look at Friday's HashSet for an example that casts properly. You can also add @SuppressWarnings("unchecked") above your method/constructor header to remove the error. This is okay and acceptable style in this one particular case. (Please don't use @SuppressWarnings elsewhere, only in this particular situation.) Your line of code to construct your array of nodes should look something like this:
elements = (Node[]) new HashMap.Node[10];

(If you are curious about exactly why these weird casts are needed, read this explanation about generic arrays by UW CSE Prof Dan Grossman.)

Q: Am I required to use a StringBuilder in my toString method?
A: No. It's much faster than just concatenating a String, but we don't require it.
Q: What kind of set am I supposed to create/return in the keySet method?
A: Use a HashSet (the one from java.util). Yes, it is okay to import java.util.Set and java.util.HashSet to do this.
Q: Can I call my own keySet method and use it to help me solve the other methods?
A: No, you should not do that, because keySet is a bulky method that makes a copy of all your keys. So it is very inefficient to call it as a helper in all the other methods.
Q: What about my other methods? Can I call my own containsKey, get, etc. method to help me solve the other methods?
A: Yes, that is fine. Please do try to remove redundancy by having methods call each other as appropriate, and/or by making common private helper methods that are called by multiple public methods.
Q: Is it okay if I put some methods or other things in my Node class?
A: Don't put bulky, complex code in Node, but if you want to put one or two small methods in there to help you (like a toString method or something), that is fine.
This document and its content are copyright © Marty Stepp, 2013. All rights reserved.
Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form
is prohibited without the author's expressed written permission.