Question & Answer: Completing the homework 1. Double-click on the crab class in the Greenfoot class diagram to open the class in an ed…..

Please help I have a software engineering project and i am struggling with it badly and have no idea how to go about doing it. The project is being done using Java and is developed using Greenfoot. Below are pictures of the assignment, what Greenfoot looks like, and an image of the code that needs to be used to complete the project. Thanks!

Completing the homework 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. o This method causes a crab to turn/rotate 90 degrees to its left (i.e., with respect to its current orientation) o 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. ° . Write the body of the act method in the Crab class. o 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. 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 a-operator. This ismo equal signs with no space in between) Here are some examples o 5--5 evalutes to true · 3· 5 evaluates to false Lets say there is a method n() that returns an int value. Then you can check if this method returns a particular value like this if t ) s . - 5. Once you have completed the steps above, you are ready to turn in your homework for grading

Completing the homework 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. o This method causes a crab to turn/rotate 90 degrees to its left (i.e., with respect to its current orientation) o 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 crab’s 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. ° . Write the body of the act method in the Crab class. o 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. Let’s 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 it’s 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 a-operator. This ismo equal signs with no space in between) Here are some examples o 5–5 evalutes to true · 3· 5 evaluates to false Let’s say there is a method n() that returns an int value. Then you can check if this method returns a particular value like this if t ) s . – 5. Once you have completed the steps above, you are ready to turn in your homework for grading

Expert Answer

 

import greenfoot.*;
import java.lang.Math;

public class Crab extends Actor {

/*******************Intialize with the angle crab facing initially in degrees*****************/
static int faceDirection=0;
/*******************Intialize with the x coordinate initially of the crab*****************/
static double x=0;
/*******************Intialize with the y coordinate initially of the crab*****************/
static double y=0;

/*******************Intialize with the distance of edge along x axis*****************/
static double xedge=200;

/*******************Intialize with the distance of edge along y axis*****************/
static double yedge=200;

public void turnLeft(){
/*System.our.println(“Previously facing “+faceDirection+” degrees.”);*/

faceDirection+=90;
faceDirection=faceDirection % 360;

/*System.our.println(“Now facing “+faceDirection+” degrees.”);*/
return;
}

public void backUp(int distance){

faceDirection+=180;
faceDirection=faceDirection%360;
double yadd=(double)(distance * Math.sin(Math.toRadians(faceDirection)));
double xadd=(double)(distance * Math.cos(Math.toRadians(faceDirection)));

/********************To check if crab doesn’t go out from the edges*****************************/
if((x+xadd)<xedge && (x+xadd)>((-1)*(xedge))){
x+=xadd;
}else if((x+xadd)>=xedge){
x=xedge;
}else if((x+xadd)<=((-1)*(xedge))){
x=(double)(-1)*xedge;
}
if((y+yadd)<yedge && (y+yadd)>((-1)*(yedge))){
y+=yadd;
}else if((y+yadd)>=yedge){
y=yedge;
}else if((y+yadd)<=((-1)*(yedge))){
y=(double)(-1)*yedge;
}
/********************To check if crab doesn’t go out from the edges*****************************/
return;
}

public void act(){
double yadd=(double)(5 * Math.sin(Math.toRadians(faceDirection)));
double xadd=(double)(5 * Math.cos(Math.toRadians(faceDirection)));
/********************To check if crab doesn’t go out from the edges*****************************/
if((x+xadd)<xedge && (x+xadd)>((-1)*(xedge))){
x+=xadd;
}else if((x+xadd)>=xedge){
x=xedge;
}else if((x+xadd)<=((-1)*(xedge))){
x=(double)(-1)*xedge;
}
if((y+yadd)<yedge && (y+yadd)>((-1)*(yedge))){
y+=yadd;
}else if((y+yadd)>=yedge){
y=yedge;
}else if((y+yadd)<=((-1)*(yedge))){
y=(double)(-1)*yedge;
}
/********************To check if crab doesn’t go out from the edges*****************************/

if(y==yedge){
backup(50);
turnLeft();
}
else return;
}

public void challenge(){
double yadd=(double)(5 * Math.sin(Math.toRadians(faceDirection)));
double xadd=(double)(5 * Math.cos(Math.toRadians(faceDirection)));
/********************To check if crab doesn’t go out from the edges*****************************/
if((x+xadd)<xedge && (x+xadd)>((-1)*(xedge))){
x+=xadd;
}else if((x+xadd)>=xedge){
x=xedge;
}else if((x+xadd)<=((-1)*(xedge))){
x=(double)(-1)*xedge;
}
if((y+yadd)<yedge && (y+yadd)>((-1)*(yedge))){
y+=yadd;
}else if((y+yadd)>=yedge){
y=yedge;
}else if((y+yadd)<=((-1)*(yedge))){
y=(double)(-1)*yedge;
}
/********************To check if crab doesn’t go out from the edges*****************************/
if(y==yedge){
backup(150);
backup(50);
turnLeft();
}
return;
}
}

Still stressed from student homework?
Get quality assistance from academic writers!