This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dutchy
Dutchy wrote ...

2013/9/27

Book Greenfoot: Exercises 3.8 and 3.9

Dutchy Dutchy

2013/9/27

#
What exactly is exercise 3.8 trying to let me do? I don't really see how to successfully execute what the exercise wants me to do. Does anyone know? And also: What's the point of these methods? Why is the book letting me write these? Is it a demonstration of how a method can be coded into the method "act()" while it previously had been described and coded how to function (this is not the case with these examples of methods)?
danpost danpost

2013/9/27

#
Exercise 3.8 and 3.9 are trying to teach you how to refactor your code; creating short methods that accomplish specific things and calling those methods instead of having all the code in your act method. Doing this is useful in making the main code-line (the act method) easier to read and understand. It can also make it easier to find where any bugs are located. I usually only do this (1) when the act method is extraordinarily long; (2) when a code-set, or snippet, within the act method is used more than once (so, I do not have to re-write the code multiple times); or (3) when I need to locate a problem. It is a useful skill to learn, and hence these exercises.
Dutchy Dutchy

2013/9/27

#
Thanks for your reply. Unfortunately, I'm getting error messages when compiling. It's probably because they haven't been defined yet. But I get them also when I'm at Exercise 3.13. I checked "little-crab-3" for the code and it's different from what I'm writing in "little-crab-2". Here I'm wondering again what I'm doing wrong. I could just copy the code from "little-crab-3" but it would mean I didn't learn anything. Maybe you could help or someone else?
danpost danpost

2013/9/27

#
You could at least post tell us what error message you are getting and post the lines around the one highlighted.
Dutchy Dutchy

2013/9/28

#
Error message is: cannot find symbol- method turnAtEdge() Code: public void act() { if ( atWorldEdge() ) { turn(17); } if ( Greenfoot.getRandomNumber(100) < 10 ) { turn(Greenfoot.getRandomNumber(90)-45); } *turnAtEdge();* randomTurn(); move(); lookForWorm(); } *-* = highlighted lines I suspect it will turn out the same result for *randomTurn()* Sorry for that, btw.
sametguzelgun sametguzelgun

2013/9/28

#
Add this code:
public void turnAtEdge()
    {
        if(getX() <= 0 || getX() >= getWorld().getWidth())
        {
            turn(180);
        }
        else if(getY() <= 0 || getY() >= getWorld().getHeight())
        {
            turn(180);
        }
    }
danpost danpost

2013/9/28

#
You already had the code for 'turnAtEdge' and 'randomTurn'. You just need to remove that code from your act method and put it in the new methods you create that you will have the act method call. The act method you had before you added the new method calls was:
public void act()
{
    if (atWorldEdge())
    {
        turn(17);
    }
    if ( Greenfoot.getRandomNumber(100) < 10 )
    {
        turn(Greenfoot.getRandomNumber(90)-45);
    }
}
I do not know whether you had the code for the 'move' or the 'lookForWorm' methods (you do not show them), but let us get past the two that you do have code for. Lines 3 through 6 is the code for turning at an edge of the world. Lines 7 through 10 is the code for turning a random amount at random times. The end result after creating the methods and moving the code from the act method to the new methods (replacing the code in the act method with calls to the new methods) should be no change in how the scenario runs.
Dutchy Dutchy

2013/9/29

#
There's no mention made of what the code for 'turnAtEdge()' or 'randomTurn' should be. I don't have the code. Perhaps Ex. 3.8 was wanting me to create the code for myself but I can't see how because it isn't so far into the book. They did give the code for 'move' and 'lookForWorm', however the code for 'move' is found in the class 'Animal' and has been standard in this scenario. The code for 'move' is:
public void move()
    {
        double angle = Math.toRadians( getRotation() );
        int x = (int) Math.round(getX() + Math.cos(angle) * WALKING_SPEED);
        int y = (int) Math.round(getY() + Math.sin(angle) * WALKING_SPEED);
        
        setLocation(x, y);
    }
The code for 'lookForWorm' is:
public void lookForWorm()
    {        
        if ( canSee(Worm.class) )
        {
            eat(Worm.class);
        }        
    }
As you can see, the code for move is much more advanced and it's not something that I was supposed to type at this stage as it already came with the file. The class 'Crab' extends the class 'Animal'. After the line 10 in the code in your post, I had to type:
move();
lookForWorm();
After that, I had to add 'turnAtEdge' and 'randomTurn'. I'm not sure what Sametguzelgun is trying to let me do but it's too advanced at this stage or maybe it's a correction to an error made in the book/code in the file? I'll give you the link for the exercises I mean: Book Greenfoot: Chapter 3; ex. 3.8 through 3.13
danpost danpost

2013/9/29

#
Alright, you have a method called 'act'. It is a public method (required by Greenfoot) and has a 'void' return (meaning there is no returned value or object). You have examples of other classes ('lookForWorm', 'move', 'canSee' and 'eat'). I believe they are all public methods with 'void' return types, except for the 'canSee' method which returns a boolean value. The exercise is to create additional methods in the class that this act method is in to execute the code that is currently in your act method (instead of executing the code directly in the act method; which I explained above) and call these new methods from the act method, just as the methods 'move' and 'lookForWorm' are being called. Review sections 3.3 and 3.4 for an example of how to transfer lines of code from the act to a new method and replace the code in the act with a call to the new method.
danpost danpost

2013/9/29

#
Dutchy wrote...
There's no mention made of what the code for 'turnAtEdge()' or 'randomTurn' should be. I don't have the code.
I am sure that somewhere in one of the previous sections, when they show the creation of the code contained in the act method, that they explained what it does; and I had specifically mentioned which part of the code was doing what.
Dutchy Dutchy

2013/9/29

#
I think I found the solution. Thanks danpost.
You need to login to post a reply.