I am a bit new to Java and OOP. My question is:
I have an abstract class called Car with drive method. There is another subclass of it, called AntiqueCar
. Antique car stops working when it drives for 3 seconds. So it will stop after 3 seconds but then continue driving again. (I think that I could override drive method in AntiqueCar class) I tried many things, but I just could manage to stop the antique car but could not make it work again.
Here is drive method:
public void drive(Dimension2D gameBoardSize) {
if (this.crunched) {
return;
}
double maxX = gameBoardSize.getWidth();
double maxY = gameBoardSize.getHeight();
// calculate delta between old coordinates and new ones based on speed and
// direction
double deltaX = this.speed * Math.sin(Math.toRadians(this.direction));
double deltaY = this.speed * Math.cos(Math.toRadians(this.direction));
double newX = this.position.getX() + deltaX;
double newY = this.position.getY() + deltaY;
// calculate position in case the boarder of the game board has been reached
if (newX < 0) {
newX = -newX;
this.direction = MAX_ANGLE - this.direction;
} else if (newX + this.size.getWidth() > maxX) {
newX = 2 * maxX - newX - 2 * this.size.getWidth();
this.direction = MAX_ANGLE - this.direction;
}
if (newY < 0) {
newY = -newY;
this.direction = HALF_ANGLE - this.direction;
if (this.direction < 0) {
this.direction = MAX_ANGLE + this.direction;
}
} else if (newY + this.size.getHeight() > maxY) {
newY = 2 * maxY - newY - 2 * this.size.getHeight();
this.direction = HALF_ANGLE - this.direction;
if (this.direction < 0) {
this.direction = MAX_ANGLE + this.direction;
}
}
// set coordinates
this.position = new Point2D(newX, newY);
}
Then here is the AntiqueCar class:
public class AntiqueCar extends Car {
private static final String ANTIQUE_CAR_IMAGE_FILE = "antiquecar.gif";
// this car is middle-speeded. Not so fast, not so slow.
private static final int MIN_SPEED_FAST_CAR = 2;
private static final int MAX_SPEED_FAST_CAR = 7;
public AntiqueCar(Dimension2D gameBoardSize) {
super(gameBoardSize);
setMinSpeed(MIN_SPEED_FAST_CAR);
setMaxSpeed(MAX_SPEED_FAST_CAR);
setRandomSpeed();
setIconLocation(ANTIQUE_CAR_IMAGE_FILE);
}
Appreciate any kind of help, thanks!