How to make counter count in multiple methods in java?
A robot starts at location (0,0) facing east. Several beeper stacks are
randomly placed along that row, each containing a number of beepers. A
robot starts at location (0,0) facing east. Several beeper stacks are
randomly placed along that row, each containing a number of beepers. There
may be different numbers of beepers stacks. But there is always a beeper
stack at location (0,0) and always a beeper stack at location (16,0)-(the
end of robot world).
Need to write a method that makes the robot move along the row, picking up
each beeper stack as it goes. The method should return no value and take
no parameters. After the robot has picked up the last stack, the program
should print how many beepers were picked up in total. The message should
look like this: Picked up n beepers, where n is the number of beepers
picked up. The program must define and use these two additional methods:
moveRobotToNextStack() , which moves the robot forwards until it finds the
next stack and pickUpBeeperStack(), which picks up all the beepers in a
stack. My code:
public class Q4 {
void pickUpAllStacks() {
int counter=0;
pickUpBeeperStack();
moveRobotToNextStack();
counter += pickUpBeeperStack();
print("Picked up "+ counter +" beepers");
}
void moveRobotToNextStack() {
while(isSpaceInFrontOfRobotClear()) {
moveRobotForwards();
pickUpBeeperStack();
}
}
int pickUpBeeperStack() {
int counter=0;
while(isItemOnGroundAtRobot()) {
pickUpItemWithRobot();
counter++;
}
return counter;
}
}
I can't make counter count beepers in all stacks.
No comments:
Post a Comment