Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1: Write a class named FixedDistanceBot that can only travel a specified nu

ID: 3866812 • Letter: P

Question

Part 1:

Write a class named FixedDistanceBot that can only travel a specified number of intersections. The exact limit should be specified when the robot is constructed. If the limit is reached, the robot should break. Write a main method to test your class.

Part 2:

Write a class named DistanceBot that extends Robot. It will have a query named totalDistance that returns the total distance traveled by the robot so far. A second query, tripDistance, returns the distance traveled since the “trip” was started by a call to resetTrip.

Learning To Program with Robots: Java The link for the textbook: http://www.learningwithrobots.com/textbook/PDFs/WholeThing.pdf

Chapter 6

Explanation / Answer

Part 1:

class FixedDistanceBot {

int minMoves(int array[], int n)

{

int *moves = new int[n];

int i, j;

if (n == 0 || array[0] == 0)

return INT_MAX; // INT_MAX indicates limit is

reached here it would be first move

moves[0] = 0;

for (i = 1; i < n; i++)

{

moves[i] = INT_MAX;

for (j = 0; j < i; j++)

{

if (i <= j + array[j] && moves[j] != INT_MAX)

{

moves[i] = min(moves[i], moves[j] + 1);

break;

}

}

}

return moves[n-1];

}

}

Part 2:

class DistanceBot extends Robot {

totalDistance(int initialDistance) {

if (i <= j + array[j] && moves[j] != INT_MAX)

{

moves[i] + = min(moves[i], moves[j] + 1);

initialDistance = moves[i];

}

return initialDistance;

}

}

void tripDistance() {

resetTrip(dis);

totalDistance(initialDistance);

}

}

private void resetTrip(int distance) {

distance = 0;

}