Page 728 - AP Computer Science A, 7th edition
P. 728

4. (a) {
public void reset()
if(arm.isFacingRight()) arm.changeDirection();
arm.moveForward(arm.getCurrentIndex());
arm.changeDirection(); }
public int mostAcidic()
{
reset();
int minPH = Integer.MAX_VALUE, minIndex = 0; int index = 0;
while (index < solutions.size())
{
Solution s = solutions.get(index); if (s.getPH() < minPH)
{
minPH = s.getPH();
minIndex = index; }
index++; }
if (minPH >= 7) return –1;
else {
arm.moveForward(minIndex);
return minIndex; }
}
NOTE
• In part (b), a for-each loop won’t work, because you need to save an index.
• In part (b), notice that resetting the mechanical arm causes the arm to face right.
(b)










































































   726   727   728   729   730