Programming Methodologies (Using Java)
voteup image
0

Problem 3 In this exercise, your job is to get Karel to create a checkerboard pattern of beepers inside an empty rectangular world, as illustrated in the following before-and-after diagram

Can anyone tell where did I made mistake ?

my code is as follows :

public class CheckerboardKarel extends SuperKarel {

public void run()
{
while(noBeepersPresent()) 
  {
        putBeeperLine();

        if(facingEast())
        {
        turnLeft();
        move();
        turnLeft();
        }else {
        turnRight();
        move();
        turnRight();
        }
  }
}



private void putBeeperLine()
{
    while(frontIsClear())
    {
        putBeeper();
        move();
        if(frontIsClear()){
        move();
        }
    }
}

}

asked Feb 11, 2012

http://twitter.com/Zaibunnisa82's image

http://twitter.com/Zaibunnisa82

4 Answers:
voteup image
0

Bug is fixed but now ...... this program is not working for 3 rows ans 5 columns :( ... same program should work for that too....

public class CheckerboardKarel extends SuperKarel {

public void run()
{
while(noBeepersPresent()) 
  {
        putBeeperLine();

        if(facingEast())
        {
        turnLeft();
            if(frontIsClear()){
                move();
                turnLeft();
                }

        }else
        {
        turnRight();
            if(frontIsClear()){
                move();
                turnRight();
                }
        }
// You fill in this part
  }
}



private void putBeeperLine()
{
    while(frontIsClear())
    {
        putBeeper();
        move();
        if(frontIsClear()){
        move();
        }
    }
}

}

answered Feb 11, 2012

http://twitter.com/Zaibunnisa82's image

http://twitter.com/Zaibunnisa82

voteup image
0

This is a wonderful opportunity for learning how to logically debug. I would suggest that you draw a 3x5 grid and then manually (by hand) follow your program as Karel moves. Put the beeper where you get the putBeeper() command and make Karel move as the logic of your program decides.

This will help you understand where exactly your algorithm is failing. Once you understand that, you can then design a better algorithm.

Good Luck :-)

answered Feb 12, 2012

http://twitter.com/coding_insights's image

http://twitter.com/coding_insights

voteup image
0

@Zaibunnisa82, you will notice a 'Messages' tab on the top of this page, besides the 'Blog' tab. Just FYI, everytime someone posts an answer to a question you have asked, you will see an '*' sign on the 'Messages' tab.

Hope this helps you quickly find out if there is some activity on the forum questions you have asked.

answered Feb 12, 2012

http://twitter.com/coding_insights's image

http://twitter.com/coding_insights

voteup image
0

thank you very much .. I ll use the link you mentioned .. and make 3x5 grid to solve this problem manually ...

answered Feb 12, 2012

http://twitter.com/Zaibunnisa82's image

http://twitter.com/Zaibunnisa82

Provide an answer to this question.
This editor supports the markdown format. Please visit this page, If you are not familiar with Markdown.

Recent Course Updates: