Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.awt.Point; import java.util.ArrayList; public class Robot { // fields private Point location; private int direction; // array list to store the list of Strings

image text in transcribed

import java.awt.Point;

import java.util.ArrayList;

public class Robot {

// fields

private Point location;

private int direction;

// array list to store the list of Strings representing Robot's movement

// history

private ArrayList movementHistory;

// constructor taking initial location and direction

public Robot(Point location, int direction) {

this.location = location;

// validating direction before setting

if (direction >= 0 && direction

this.direction = direction;

// initializing array list

movementHistory = new ArrayList();

// adding current position and direction to the list

movementHistory.add(String.format("(%d,%d), %s", location.x, location.y,

getDirection()));

}

// method to turn left

public void turnLeft() {

direction--;

// wrapping around if necessary

if (direction

direction = 3;

}

// adding current position and direction to the list

movementHistory.add(String.format("(%d,%d), %s", location.x, location.y,

getDirection()));

}

// method to turn right

public void turnRight() {

direction++;

// wrapping around if necessary

if (direction > 3) {

direction = 0;

}

// adding current position and direction to the list

movementHistory.add(String.format("(%d,%d), %s", location.x, location.y,

getDirection()));

}

// method to move 1 unit distance in current direction

public void move() {

// based on direction, updating x or y coordinate of location

switch (direction) {

case 0:

location.y--; // north

break;

case 1:

location.x++; // east

break;

case 2:

location.y++; // south

break;

case 3:

location.x--; // west

break;

}

// adding current position and direction to the list

movementHistory.add(String.format("(%d,%d), %s", location.x, location.y,

getDirection()));

}

// returns the location

public Point getLocation() {

return location;

}

// returns the direction as single character String

public String getDirection() {

switch (direction) {

case 0:

return "N";

case 1:

return "E";

case 2:

return "S";

default:

return "W";

}

}

// getter for movement history

public ArrayList getMovementHistory() {

return movementHistory;

}

}

Activity 1. In the previous lab, you have implemented a class for a robot movement simulation. However, using this Java class, you are not able to record the movement history of a given robot object. Perform required modification on the Robot class to be able to store all the movements of a given Robot object in an ArrayList, starting from the initial location to the current location. For instance, if a Robot moves like the ones we have in the tester class of the previous activity, then the list should have the following information: (5.5). E (6.5), E (6.5). S (6,6), S (6,7). S (6,7). W (5.7), W (4,7). W (4,7), S (4,8). S In a tester class, like the one in the previous activity, after some movements, print the path of the robot movements, like above

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions