One one of my homework questions, I got this one marked wrong and I still can't seem to figure out how to solve it. Was I missing a loop or a condition?
The question was as stated:
You have a GUI that keeps track of a list of pre-specified "special" points. If the user's mouse moves over one of these points, the point lights up. Write the code that would go inside of the handler/listener below. Use the references and also assume the existence of the method listed below (which you can invoke- you do not need to write).
--specialPointList // an ArrayList of the pre-specified "special" points; the type is ArrayList<Point>
--assume this method exists: public void lightUpAPoint(Point p)
First of all you were supposed to assume that specialPointList was a reference to an ArrayList of the points. But instead you changed that variable to refer to a different ArrayList. (Which was empty, since you created it and didn't fill it.)
So the result of that would be that no points would ever light up, because your code wasn't looking at the special points.
However if you hadn't done that, then the second problem would appear. Namely, instead of lighting up the point that the mouse just went over, your code just lights up all of the special points. (I suppose that in one interpretation of the requirements, lighting up all of the points does cause the correct point to be lit up, and you could argue that the requirements didn't say that when the right point lights up, the wrong points must not light up. But you wouldn't win that argument.)
There could be other problems too but you'd have to fix the first two before looking for more.
No, that is still wrong. (Was referring to the previous code)
Think about it like this: You know the game of Bingo? Assuming you do, think of the specialPoints list as your Bingo card. It has a bunch of numbers on it. You have to keep the same Bingo card throughout a round, right? When the caller shouts out a number, you look through your card and see if the number called is on the card you have. If the number is there, you go and put a mark on that number on your card. This goes on until you make Bingo or somebody else does. Make sense so far? Every time you hear a number called, that's like an event happening. Every time an event happens, you hear a certain number. That number is like the event.point. When you go look for the number on your card and mark it, marking the number is like making the point light up.
The code you originally wrote does the Bingo equivalent of this: every time the caller calls out a number, you go get a new Bingo card. That card has no numbers on it. Then you go and try to mark all the numbers on it.
P.S. It looks like you got the right code now but I'll post this anyway because I typed it all with just two thumbs (on my iPad) and the analogy is nice, in my totally biased opinion.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck