Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method that commands a robot to face a specific direction. Your method d

ID: 3914498 • Letter: W

Question

Write a method that commands a robot to face a specific direction. Your method declaration should look like:

public static void faceDirection (Robot r, Direction dir)

{

//Write your code here;

}

What algorithm should we use to do this?

Think about it …

public static void faceDirection (Robot rob, Direction dir)

{ while (rob.getDirection() != dir) { rob.turnLeft();

}

}

Test the method:

City city = new City();

Robot robbie= new Robot(city, 1, 1, Direction.EAST); faceDirection(robbie, Direction.SOUTH);

Starting with your program from Exercise 1, write a method that makes a robot move to a specific street public static void goToStreet (Robot rob, int targetStreet)

                                                          {        // Write your code here;       }



Hints:

Move the robot either North or South to the desired street along the current avenue

Use the current position and your faceDirection() method to decide which way to go

Don’t worry about walls or other obstacles

(Think about this a while, then go to the next slide)

To Decide in which direction to go: Use method: getStreet() ? Starting with your program from Exercise 1:

Add a method goToStreet() that makes a robot move to a specific street.

Algorithm:


If the robot's current street number < the target street, then we must be North of the target and we need to travel South.

If the robot's current street > the target street, then we're South of the target and we need to move North.

Use your new faceDirection() method to face in the right direction.

Using a loop, move the robot until it's on the right street.

Complete the method and test your new method by calling in main().

Note: Make sure it works even if the robot is already on the target street.

Starting with your program from Exercise 2:

Add a method goToAvenue() that makes a robot move to a specific avenue. You must add required parameters.

This should be easy, as it can be similar to method goToStreet().

Complete the method and test your new method by calling in main().

Starting with your program from Exercise 3:

Add a method goToIntersection() that makes a robot move to a specific intersection. You must add required parameters.

Hint: This should be easy to write by reusing code we’ve already written (where reusing does not mean copy & paste!).

What parameters should this method accept?

Think about it …

Complete the method and test your new method by calling in main().

Explanation / Answer

public static void faceDirection (Robot rob, Direction dir) {

while (rob.getDirection() != dir) {

rob.turnLeft();

}

}

/*

*Go to a particular street given by targetStreet

*/

public static void goToStreet (Robot rob, int targetStreet) {

if(rob.getStreet()!=targetStreet) {

return;

}

if(rob.getStreet()<targetStreet) {

faceDirection(rob,Direction.SOUTH);

} else if(rob.getStreet()>targetStreet) {

faceDirection(rob,Direction.NORTH);

}

while(rob.getStreet()!=targetStreet) {

rob.moveInCurrentDirection(); //Assuming rob has a member function

}

}

/*

*Go to a particular avenue given by targetAvenue

*/

public static void goToAvenue (Robot rob, int targetAvenue) {

if(rob.getAvenue()!=targetAvenue) {

return;

}

if(rob.getAvenue()<targetAvenue) {

faceDirection(rob,Direction.SOUTH);

} else if(rob.getAvenue()>targetAvenue) {

faceDirection(rob,Direction.NORTH);

}

while(rob.getAvenue()!=targetAvenue) {

rob.moveInCurrentDirection(); //Assuming rob has a member function

}

}

/*

*Go to an intersection which is given by 2 street numbers

*/

public static void goToIntersection (Robot rob, int street1, int street2) {

if(rob.getStreet() == street1) { //If already on street1

while(rob.getStreet()==street1) {

goToStreet(rob, street2);

}

} else if(rob.getStreet() == street2) { //If already on street2

while(rob.getStreet()==street2) {

goToStreet(rob,street1);

}

} else {

goToStreet(rob,street1); //Go to street1, then street2

while(rob.getStreet()==street1) {

goToStreet(rob, street2);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote