Shorthand for stepstaken steptaken1 totaldist dist

Info icon This preview shows pages 19–25. Sign up to view the full content.

View Full Document Right Arrow Icon
// Shorthand for stepsTaken = stepTaken+1; totalDist += dist; dist = dist * 2; } return totalDist; } // In the TurtleTester main method // Assume you have a reference to a Turtle in variable maria int traveled = maria.wander( 4 ); System.out.println ( “Maria traveled “ + traveled + “units” ); Which line(s) above would you delete if you changed the while loop to a for-loop so that the code does exactly the same thing? for loops vs while How and why???
Image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
// in the Turtle class public int wander( int nSteps ) { int stepsTaken = 0; int dist = 2; int totalDist = 0; // was: while ( stepsTaken < nSteps ) { for ( int stepsTaken = 0; stepsTaken < nSteps; stepsTaken++ ) { this.forward( dist ); this.turnRight(); stepsTaken += 1; // Shorthand for stepsTaken = stepTaken+1; totalDist += dist; dist = dist * 2; } return totalDist; } // In the TurtleTester main method // Assume you have a reference to a Turtle in variable maria int traveled = maria.wander( 4 ); System.out.println ( “Maria traveled “ + traveled + “units” ); So when do you use for and when do you use while??
Image of page 20
Using loops in a method (II) Write a method that draws "steps" of a particular size until the Turtle passes a "height limit" then returns the number of steps taken World w = new World(400, 400); Turtle t = new Turtle(30, 400, w); t.drawSteps(50, 220); step size height limit
Image of page 21

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using loops in a method (II) Write a method that draws "steps" of a particular size until the Turtle passes a "height limit" then returns the number of steps taken public int drawStepsToHeight(int size, int heightLimit) { int distClimbed = 0; while (distClimbed < heightLimit) { this.forward(size); this.turnRight(); this.forward(size); this.turnLeft(); } } In Turtle class (Turtle.java) What line of code must be added inside the loop? A. No additional line of code is needed B. distClimbed++; C. distClimbed = size; D. distClimbed = heightLimit; E. distClimbed += size;
Image of page 22
Using loops in a method (II) Write a method that draws "steps" of a particular size until the Turtle passes a "height limit" then returns the number of steps taken public int drawStepsToHeight(int size, int heightLimit) { int distClimbed = 0; while (distClimbed < heightLimit) { this.forward(size); this.turnRight(); this.forward(size); this.turnLeft(); distClimbed += size; } } In Turtle class (Turtle.java) Finish the code so that the method counts the number of steps taken and returns it.
Image of page 23

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Another Variable Challenge Turtle maria = new Turtle(25, 25, world1); Turtle jose = new Turtle(100, 50, world1); maria = jose; jose = new Turtle(50, 150, world1); jose.forward(25); Where in the World (what coordinates) is the Turtle object that maria references at the end of this code?
Image of page 24
Image of page 25
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern