I am trying to write two classes:
one for a robot that will instance with unique ID and direction. and another class for direction that will use enum for the directions for a robot. I tried to write it in the following way but I think i am missing something...
package Q1;
public class Robot {
static int IDGenerator = 1000; //ID generator for class Robot
int RoboID; //The ID of the robot
Direction direction; //The Direction the robot is facing
//Constructor for Robot
public Robot(int dir){
this.direction = new Direction(dir);
this.RoboID = IDGenerator;
IDGenerator++;
}
}
the class for the enum:
package Q1;
public enum Direction{
UP(1), RIGHT(2), DOWN(3), LEFT(4);
private final int dir;
//constructor for Direction enum for a robot
private Direction(int dir){
this.dir = dir;
}
//return facing direction of a robot
public int getDirection(){
return this.dir;
}
}
privateis permitted sinceenumis suppose to be holder of constants of enum type. It would be strange to let user create new instances representing some particular constant if it is already defined in enum.