Exercise 2.14
Place the move() statement inside the if-statement, rather than after it.
Find out what is the effect and explain the behavior you observe.
To this step, you need to know what the if-statement is. The if-statement is
public void act()
{
if ( atWorldEdge() )
Then, let's do what it tells you to do. Since it says that you need to put move() inside the if-statement.
and then when you press compile, ...
And as you can see from the screen, something is wrong
After changing that code at least five times, Dr. D told me that you can't put move inside
the if-statement. You can't put move inside because you need to put a situation that Java can understand.
So in the previous blog post, I wrote atWorldEdge() and it worked because I am giving the Java a situation when it can move.
So if I write
public void act()
{
if ( atWorldEdge() )
{
move();
}
}
It means that the crab would move if it is at the WorldEdge.
However, since I typed
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* This class defines a crab. Crabs live on the beach.
*/
public class Crab extends Animal
{
public void act()
{
if ( atWorldEdge() )
{
move();
}
}
}
And since we put this crab to move only if the crab is atWorldEdge, the crab only moves when I locate it on the edge. And it only moves. meaning that the crab doesn't do anything more than to just 'move' and to stop when it lands on a corner of the worldEdge
So to make the crab to turn around the worldedge the whole time, I am going to add another code.
if ( atWorldEdge() )
{
turn(17);
}
move();
}
This code allows the crab to turn if it is on the worldedge, and move until it meets the worldedge again.
So from completing all the exercises, I has finished the chapter TWO
yay!
BUT I STILL HAVE ONE PROBLEM LEFT...
(to be continued)
Place the move() statement inside the if-statement, rather than after it.
Find out what is the effect and explain the behavior you observe.
To this step, you need to know what the if-statement is. The if-statement is
public void act()
{
if ( atWorldEdge() )
So you can see that the if-statement is if (blahblah() ) and depending on what you put inside the bracket, your crab will move differently. However, you need to put a boolbean inside the bracket. Meaning that something that the computer could say true or false.
Then, let's do what it tells you to do. Since it says that you need to put move() inside the if-statement.
see the 'move'? |
and then when you press compile, ...
Gah I hate those Deathly Yellow highlighter !!! |
After changing that code at least five times, Dr. D told me that you can't put move inside
the if-statement. You can't put move inside because you need to put a situation that Java can understand.
So in the previous blog post, I wrote atWorldEdge() and it worked because I am giving the Java a situation when it can move.
So if I write
public void act()
{
if ( atWorldEdge() )
{
move();
}
}
It means that the crab would move if it is at the WorldEdge.
However, since I typed
public void act()
{
if ( move )
{
move();
}
}
Meaning that the crab would move if it is at the move,
It is totally OBVIOUS why it kept on doing the Deathly highlighter
Bahahahahahah silly me !
(RE) Example 2.14
Place the move() statement inside the if-statement, rather than after it.
Find out what is the effect and explain the behavior you observe.
The code for that is
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* This class defines a crab. Crabs live on the beach.
*/
public class Crab extends Animal
{
public void act()
{
if ( atWorldEdge() )
{
move();
}
}
}
And since we put this crab to move only if the crab is atWorldEdge, the crab only moves when I locate it on the edge. And it only moves. meaning that the crab doesn't do anything more than to just 'move' and to stop when it lands on a corner of the worldEdge
So to make the crab to turn around the worldedge the whole time, I am going to add another code.
if ( atWorldEdge() )
{
turn(17);
}
move();
}
This code allows the crab to turn if it is on the worldedge, and move until it meets the worldedge again.
So from completing all the exercises, I has finished the chapter TWO
yay!
BUT I STILL HAVE ONE PROBLEM LEFT...
(to be continued)