Frogger addition
Fall 2006, David Matuszek

The problem:

The assignment asks you to keep track of "dead" frogs. However, with the assignment the way it is, all your frogs can get across the road safely. I said in lab on Friday that I would need to think some more about this.

The solution:

I didn't want to ask you to make some of your frogs deliberately "stupid." Fortunately, there is a better way.

The roadway is dark gray. Dark gray cars are hard to see on it (mine are outlined in black, so it's still possible for the human to see them). So here's the new requirement: If a car is very dark--dark gray or black--the frog doesn't see it. (If you use other colors, decide which cars are dark enough to be invisible.) You should have a high enough proportion of dark cars that we can test your program easily.

How to do it:

There are two obvious ways to make this work in your program.

  1. You could pass in the list of active cars, and the frog could "ignore" the dark ones.
  2. You could pass in the list of visible cars.

I think the second of these is clearly the better solution. Ignoring certain cars just doesn't seem like something a frog would deliberately do.

The assignment says, "When you tell a frog to move, give it (as a parameter to your 'hop' method), the ArrayList of all the currently active cars." So your Frog class doesn't need to be changed at all. Instead, go through the ArrayList of currently active cars and put all but the dark cars into a second ArrayList, which you pass to the frog. (A car can be in more than one list at a time.) I'd suggest writing a method in your Frogger class with a signature like:

private static ArrayList<Car> getVisibleCars(ArrayList<Car> allCars)

This says: The method getVisibleCars takes, as a parameter, an ArrayList containing Cars, and returns an ArrayList containing Cars as its result. It's private because there's no reason for any other class but Frogger to use it, and it's static because it doesn't need an object of type Frogger in order to do its job.