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

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

Dont 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 weve 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().

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!