lec06-Loops-after.pdf

# Shorthand for stepstaken steptaken1 totaldist dist

• 27

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

// 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???

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

// 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??
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

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

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;
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.

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

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?
This is the end of the preview. Sign up to access the rest of the document.
• Winter '08
• Kube,P
• Subroutine, The Return, Void type, Turtle maria

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern