Wednesday, 2 October 2013

Moving robot in Java on specified length

Moving robot in Java on specified length

A robot starts at location (0,0) facing east, carrying 7 beepers: Need to
write a method called that accepts an integer parameter and a boolean
parameter. The method should move the robot along the row for the number
of squares specified by the integer parameter, putting down one beeper in
each square (excluding the robot's starting square). Can be assumed that
the integer parameter will be a value that allows the robot to do this
safely. The robot should finish in the square where it put down the last
beeper. If the boolean parameter is true then the program should print out
how many beepers were spread over the row. The message should be one of
the following:
If 0 beepers were spread over the row, the message should be Spread no
beepers
If 1 beeper was spread over the row,the message should be Spread 1 beeper
Otherwise the message should be Spread n beepers,where n is the number of
beepers put down.
My code:
void spreadBeepersOverRow(int x, boolean y) {
while(x>0) {
moveRobotForwards();
dropItemFromRobot();
--x;
}
if(y==true&&x==6||x<6&&x>0)
println("Spread "+ x +" beepers");
else
if(y == true&&x==7)
println("Spread no beepers");
}
}
I am confusing with the conditions of the task... My version gives some
mistakes when compiling

No comments:

Post a Comment