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

Consider the following code which is part of the Turtle class: public ______ tra

ID: 3824301 • Letter: C

Question

Consider the following code which is part of the Turtle class:

public ______ travel(int distToTravel, int angle)

{

int stepsTaken = 0

int stepSize = 20

while (distToTravel > 0) {

stepsTaken += 1

this.forward(stepSize)

this.turn(angle)

distToTravel = distToTravel stepSize

}

return stepsTaken

}

------------------------------------------------------------------------------------------------------------------------

Part A : In what file must this method appear?

----------------------------------------------------------------------------------------------------------------------------

Part B : Consider the following code, which in a tester class:

public static void main (String[] args)

{

World w = new World(100, 100)

Turtle maria = new Turtle(50, 50, w)

int target = 90

int total = maria.travel(target, 15)

System.out.println(total)

}

What is printed by this method? Hint: You might want to complete Part D first. Choose one of

the following answers.

A. 3 B. 4 C. 5 D. 6 E. Any of these might be printed

-------------------------------------------------------------------------------------------------------------------------

Part C : Fill in the memory model that is produced when the code from part B is

executed. You should clearly show the values of the variables as they change by crossing off old values and writing in the new values.

PLZ help me A through C!

Thanks in advance!

#java

Explanation / Answer

The answers are given below. Please don't forget to rate the answer if it helped. Thank you very much.

Part A:

In Java , a class will appear in a file with its own name. So the function will appear inside of Turtle class in the file named Turtle.java like this

public class Turtle

{

......

.... other code

public int travel(int distToTravel, int angle)

{

int stepsTaken = 0

int stepSize = 20

while (distToTravel > 0) {

stepsTaken += 1

this.forward(stepSize)

this.turn(angle)

distToTravel = distToTravel stepSize

}

return stepsTaken

}

}

Note the return type would be int for the method travel ( ) because it returns the stepsTaken which is of type int.

Part B:

The answer is Option C) 5

The output printed will be 5. For reasons see answer to Part C below where steps are shown how the answer for Part B is arrived at.

Part C

The main program prints the value of total.

In the code,

target = 90

total = maria.travel(target, 15)

So the travel is called with arguments as travel ( 90, 15) i.e distanceToTravel=90 and angle =15 in travel(). It also initialises stepSize = 20 and stepsTaken=0 before while loop starts.

Now different iterations of while loop are shown below. The loop iterminate when distanceToTravel becomes zero or a -ve value.

So the value returned is stepsTaken after the loop terminates and this reaches 5 as shown above.

stepSize distanceToTravel condition: distanceToTravel > 0 stepsTaken 20 90 true 1 20 90-20 = 70 true 2 20 70-20 = 50 true 3 20 50-20 =30 true 4 20 30-20=10 true 5 20 10-20= -10 false loop terminates
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote