Question: Write a java code for below instruction: public Car(Grid grid, int speed, int xLoc, int yLoc, int direction) A constructor for Car, The direction parameter

Write a java code for below instruction:

public Car(Grid grid, int speed, int xLoc, int yLoc, int direction)

A constructor for Car, The direction parameter sets the direction of the car: 0 for right-up and 1 for left-down. Car checks that the speed is between 3 and 8, inclusive and throws an exception otherwise. You may assume that the location given is one of the road elements.

public String getDirectionString()

A getter method for the boat's direction, returned as a String for convenience.

public int getDirection()

A getter method for the boat's direction as the stored integer.

public void move()

Car's implementation of move (see below).

public String toString()

An override of the toString method that returns a String version of the Car data (including the Vehicle instance).

Implementations of the move() method

Car: Let the car's speed be s. In each call to move(), the car moves s road elements in the direction its traveling, unless it moves past the end of the road for the direction it is traveling. It this case, the car reverses direction at the end of the road. For example, if the is traveling left-down and the car is current at road element 3(with coordinates (22, 16) with speed 5, move() notes that this will move past the end of the road and sets the location to (20, 15) and reverses the direction to right-up. If the car is at road element 3 traveling left-down at speed 3, move() changes the location to (21, 15).

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!