Question: This question involves the implementation of a Position class. A Position object is created with two parameters representing a position on a 2d grid, with

 This question involves the implementation of a Position class. A Position

object is created with two parameters representing a position on a 2d

This question involves the implementation of a Position class. A Position object is created with two parameters representing a position on a 2d grid, with the first value giving the position on a east/west axis, where east is positive, and the other on an north/south axis where north is positive. The position class supports movement of the current position north, south, east and west, reporting of the current position as a String and reporting of the total distance traveled. The Position class provides a constructor and the following methods. move changes the position by the number specified in the first (double) parameter in the direction given by the second (String) parameter (precondition: this is equal to one of N, S, E, W). If none of these 4 directions are given, does nothing toString returns the current position as a String formatted as a pair of coordinates totalDistance returns the total distance the position has been moved as a double The following table contains a sample code execution sequence and the corresponding results. Comment Statements and Expressions Value Returned (blank if no value) Position p = new Position (1.5, -5.0); p.toString(); (1.5, -5.0) This is the starting position p.totalDistance(); 0.0 No movement so far p.move (3.5, "E"); Moves the position 3.5 east p.move (1.0, "S"); p.toString(); (5.0, -6.0) p.totalDistance(); 4.5 p.move (8.2, "N"); Moves the position 3.5 south East/west axis value has increased by 3.5, north/south axis value has decreased by 1.0 Total distance traveled is 3.5+1.5 = 5.0 Moves the position 8.2 north Moves the position 3.7 west Moves the position 0.6 west East/west axis value has increased by 3.5, north/south axis value has decreased by 1.0 Total distance traveled is 4.5+8.2+3.7+0.6 = 17.0 p.move (3.7, "W"); p.move (0.6, "W"); p.toString(); (0.7, 2.2) p.totalDistance(); 17.0 Write the complete Position class including the constructor and any required instance variables and methods. Your implementations must meet all specifications and conform to the example

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!