Question
I need help writing a prgram for a crab the current code is posted below the question. 1. Double-click on the Crab class in the
I need help writing a prgram for a crab the current code is posted below the question.
1. Double-click on the Crab class in the Greenfoot class diagram to open the class in an editor window. Write the body of the turnLeft() method in the Crab class.
This method causes a crab to turn/rotate 90 degrees to its left (i.e., with respect to its current orientation).
Be sure to test the turnLeft() method before proceeding to the next step.
2.Write the body of the backUp() method in the Crab class.
This method causes a crab to back up (move backwards) the indicated distance with respect to its current orientation. The crabs orientation must not change. For example, the method call backUp(50) would cause the crab to move backwards 50 units.
Be sure to test the backUp() method before proceeding to the next step.
3.Write the body of the act() method in the Crab class.
This method defines the following behavior for a crab:
First, the crab moves forward distance 5. Then, the crab checks to see if it is at an edge of the world. If it is, the crab moves backwards 50 units using the backUp() method and then turns to its left using the turnLeft()method.
Be sure to test the act() method before proceeding to the next step.
4. Challenge: The crab is walking straight into the gull, which is probably not a good idea (Links to an external site.)Links to an external site.. Lets help the crab out by changing the act() method to ensure that the crab walks below the gull when it makes the trip across the top of the screen. Specifically, you need implement the following behavior in the act() method:
First, the crab moves forward distance 5. Then, the crab checks to see if it is at an edge of the world. If it is, the crab then checks to see if its the top edge. If it is the top edge, the crab moves backwards 150 units using the backUp()method. No matter which edge it is at, the crab then moves backwards 50 units using the backUp() method and then turns to its left using the turnLeft() method.
To do this, you will have to search through the documentation of the Actor class to find a method that will allow you to detect when the crab is at the top edge of the world. Once you find the method you need to use, the hints below should help you put this all together.
You will need to add another if statement inside the one you already have.
You can ask if two int values are equal by using the == operator. (This is two equal signs with no space in between.) Here are some examples:
5 == 5 evalutes to true
3 == 5 evaluates to false
Lets say there is a method m() that returns an int value. Then you can check if this method returns a particular value like this: if (m() == 5).
Crab Code below:
import greenfoot.*;
/** * This class defines a crab. Crabs live on the beach. */ public class Crab extends Actor { public void act() { } /** Turns the crab to face left from its current perspective. */ public void turnLeft() { } /** Moves the crab backwards the given distance. */ public void backUp(int distance) { } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started