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
Get step-by-step solutions from verified subject matter experts
