Write a java code for below instruction: public Car(Grid grid, int speed, int xL
ID: 3916302 • Letter: W
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 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).
Explanation / Answer
public class Car {
private int yearModel;
private String make;
private int speed;
// The constructor accept the car's year model and make as argument
// The constuctor should assign 0 to the speed field
public Car (int yrModel, String carMake)
{
yearModel = yrModel;
make = carMake;
speed = 0;
}
public void setyearModel(int yrModel)
{
yearModel = yrModel;
}
public void setMake (String carMake)
{
make = carMake;
}
public void setSpeed(int carSpeed)
{
speed = carSpeed;
}
public int getYearModel()
{
return yearModel;
}
public String getMake ()
{
return make;
}
public int getSpeed ()
{
return speed;
}
public void AccelerateSpeed (int speed)
{
speed = speed + 5;
}
public void BrakeSpeed (int speed)
{
speed = speed - 5;
}
}
]
[/import javax.swing.JOptionPane;
public class MyNewCar {
public static void main(String[] args) {
Car myCar = new Car (2010, "Honda");
int speed = myCar.getSpeed();
speed = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Speed" ));
for (int i = 0; i < 5; i++)
{
System.out.println("The" + " " + myCar.getYearModel() + " " + myCar.getMake() +
" " + "is going " );
myCar.AccelerateSpeed(speed);
System.out.println("Your Speed now is: " + speed);
}
speed = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Speed" ));
for (int i = 0; i < 5; i++)
{
System.out.println("The" + " " + myCar.getYearModel() + " " + myCar.getMake() +
" " + "is going " );
myCar.BrakeSpeed(speed);
System.out.println("Your Speed now is: " + speed);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.