# KingCrab solution2

Note: GridWorld will not be featured on the 2015 and subsequent AP CS Exams.

KingCrab processes actors differently than CrabCritter; therefore, it must override processActors. Overriding the correct methods and adhering to the postconditions of the superclass methods is commonly required on the AP Computer Science Free Response. KingCrab must push actors farther away from itself if possible. This can be accomplished by checking whether the distance between the KingCrab and each possible new location, surrounding empty locations, is greater than one. This works because the getActors method in CrabCritter returns only neighboring actors.

Review the KingCrab solution with AP CS Tutor Brandon Horn.

## KingCrab

```public class KingCrab extends CrabCritter
{
/**
* Moves each actor farther away. Eats actor if can't be moved
*/
public void processActors(ArrayList<Actor> actors)
{
for (Actor actor : actors)
if (!scare(actor))
actor.removeSelfFromGrid();
}

/**
* Moves actor one location farther away if possible.
*
* @return true if actor was moved, false otherwise
*/
private boolean scare(Actor actor)
{
{
if (this.distance(loc) > 1)
{
actor.moveTo(loc);
return true;
}
}
return false;
}

/**
* Returns the distance from this KingCrab to loc.
*/
private double distance(Location loc)
{
return Point2D.distance(this.getLocation().getRow(),
this.getLocation().getCol(),
loc.getRow(),
loc.getCol());
}
}
```

## 2 thoughts on “KingCrab solution”

1. Seb

This is a very elegant solution !

In the AP exam, are we allowed to import java.awt.geom.Point2D to compute the distance?

The gridworld Solutions Manual at apcentral.collegeboard.com uses math.sqrt() and the pythagoras theorem to compute the distance…

Thank you.

• Brandon Horn

I just spoke with someone who grades the exams nearly every year and is otherwise involved in the process. Any legal Java can be used on the AP CS Exam and will receive full credit if it is correct.

Partially correct solutions that use methods outside the AP Java Subset might be eligible for less partial credit than similar solutions that remain within the Subset.