Oakii
10-11-2004, 05:46 PM
Language: Java - Karel J Robot
This was a question I got in my AP Java class test:
Use this mothod below to answer question:
---
public void faceSouth()
{
while ( ! facingSouth() )
{
turnLeft();
}
}
---
What would be a reasonable precondition for this method?
a) Preconditions: the robot is not facing South
b) Preconditions: the robot is facing North
c) Preconditions: the robot is facing East
d) Preconditions: the robot is facing South
e) None of the above.
I marked A, however the answer was E, can someone explain how A is "not the reasonable" precondition since the method is called when the robot is to face south.
Veghost
10-11-2004, 07:04 PM
What does it mean "reasonable precondition"? I think all preconditions are reasonable, because method will work with all preconditions.
AlexV
10-11-2004, 07:21 PM
He is facing west. Heh. That's a pretty good question. That block of code is designed to turn the robot to face south. The block of code can only turn the robot using left turns. Theoretically, the correct answer can be any direction, however the best answer is west (which is not on the list so the answer is e) - None of the above), because it would take only one run through the while() loop to get it to turn south. There are many ways to interpret the question, and all of the answers can be considered correct. I don't know why your teacher is so mean to you.
Oakii
10-11-2004, 07:39 PM
Note: Karel J Robot can only 'turnLeft' (can't 'turnRight')
Originally posted by Veghost
What does it mean "reasonable precondition"? I think all preconditions are reasonable, because method will work with all preconditions.
I believe this is more an opinion question, because everyone's reasonable is different. However I thought A because A would cover all bases except when the robot is facing south (of which the method wouldnt be called)
ilyash
10-12-2004, 06:25 AM
if i understand this correctly.. precondition is how it is before you run the method.
So no matter which direction the bot is facing, the program will run correctly, so i picked E
Burhan
10-12-2004, 08:52 AM
I think this is how one is meant interpret the question. When the robot is facing West the only way the robot can turn using its limited motion (left turns only) is South, which makes facing West a reasonable precondition for this method. If the method was named something like turn, rotate, move, etc. then any position would be reasonable.
Therefore, if the robot is facing anywhere but West, there are other better suited methods to be called. For example, any method, no matter what its name, would turn the robot to the left since that's the only direction the robot can go.
Its a poorly worded question. A better way to write it would have been "What is the ideal precondition for this method?"
The reason why its not reasonable (heh) is that one would assume that the person using the class would have already moved the robot using more "appropriate" methods (westward(), tallyholeft(), giddyuprobotleft()) to reach a condition where the robot is facing exactly West. Then, when they called faceSouth(), it would take a minimum amount of iterations to return the robot to the desired state.
You are right in stating that the program would run correctly if you executed that method even if the robot was currently facing East. However, it would not be an efficient use of the program if you were to execute that method when the robot was facing anywhere but West. Suppose that with one turn, the robot can move only 90 degrees. That would mean that if the robot was facing East and you executed faceSouth() it would take it three turns to return to the state you desired. However, if the robot was facing West, it would only take one turn (one iteration), making West the best state in which to call faceSouth().
Oakii
10-12-2004, 05:53 PM
Thanks fyrestrtr, so you would say the best precondition would be that the robot is facing west, thus E answer?
Burhan
10-13-2004, 02:47 AM
Yes. For all other conditions, there are better suited methods.