I would like to ask a question about Abstract Classes in Java.
I have a class named as Car. Also, I have created an Abstract class named as State. This abstract class has four subsclasses named are, GotoXY,Rest,Shake and Chase respectively.
A Car has a state and its state is changing randomly.
In the Car class I have created a variable as
private State state;
First Question : Should I create a constructor with State parameter in the Car Class? Like:
Car(State _state){.//.}
State Class :
public abstract class State {
public State chooseRandomState() {
return new GotoXY();
}
private void pickAnotherState(){
//Random rnd = new Random();
//currentState = States.values()[(int)(Math.random()*States.values().length)];
//System.out.println(currentState);
}
public abstract String getCurrentStateName();
}
Car Class is a normal class.
I have instantiated the Car class in a Common Class.
Second Question: Should I create an enum in the State class and choose a random state? How should I handle the Random state changes in the Car Class?