Question
Answer following questions based on code below: 8. Why can we use a for-loop going down the mountain when we needed a while-loop going up?
Answer following questions based on code below:
8. Why can we use a for-loop going down the mountain when we needed a while-loop going up? Because we know how many times the loop needs to iterate to go back down but not up
9. How are we able to find base camp?
10. Which specific method call(s) result in different behaviors, depending on the object?
11. Does the steepness of the mountain change our code to go up and down? Why or why not?
12. How can different types of objects make use of a single, common code?
public class Mountain { public static void explore(Climber arg) { arg.putBeeper(); arg.turnLeft(); explore_west(arg); if(!arg.hasBeepers()) { explore_east(arg); } arg.pickBeeper(); } private static void explore_west(Climber arg) { int n = 0; while(arg.frontIsClear()) { arg.move(); } while(!arg.frontIsClear()) { arg.climbUpLeft(); n = n + 1; } if(arg.nextToABeeper()) { arg.pickBeeper(); } arg.turnAround(); for(int k = 1; k <= n; k++) { arg.climbDownRight(); } while(!arg.nextToABeeper()) { arg.move(); } } private static void explore_east(Climber arg) { int n = 0; while(arg.frontIsClear()) { arg.move(); } while(!arg.frontIsClear()) { arg.climbUpRight(); n = n + 1; } if(arg.nextToABeeper()) { arg.pickBeeper(); } arg.turnAround(); for(int k = 1; k <= n; k++) { arg.climbDownLeft(); } while(!arg.nextToABeeper()) { arg.move(); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started