0

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;
    }
}
2
  • if I am defining it as public i get "Illegal modifier for the enum constructor; only private is permitted." compile error Commented Apr 29, 2014 at 10:16
  • OK I remembered right after I wrote my previous comment (temporary brain fart) that is why I removed it immediately. Of course only private is permitted since enum is 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. Commented Apr 29, 2014 at 10:21

4 Answers 4

3

Enums are not instantiated via new; instead, they have a defined set of instances at compile time. Just access the instances directly like this:

public Robot(Direction dir) {
    this.direction = dir;
}

You call this constructor e.g. like this:

Robot bot = new Robot(Direction.UP);
Sign up to request clarification or add additional context in comments.

2 Comments

@YuvalLevy Your Direction constructor looks fine to me.
Are the numbers used for anything else? If not, just drop them and the constructor completely.
2

Assuming you don't want to use a Direction parameter in your Robot constructor, then the classic solution is to provide a static method in your enum to return the right value for a given input:

public enum Direction {
    //... code omitted

    public static Direction fromInt(int direction) {
        for (Direction d : values()) {
            if (direction == d.getDirection()) {
                return d;
            }
        }
        throw new NoSuchElementException(Integer.toString(direction));
    }
}

Then use this in your constructor:

//Constructor for Robot
public Robot(int dir){
    this.direction = Direction.fromInt(dir);
    this.RoboID = IDGenerator;
    IDGenerator++;
}

Comments

0

You are calling this.direction = new Direction(dir); although the constructor is private. Provide a (static) method in the enum to convert the integer into the correct direction.

Edit: Or use

public Robot(Direction direction) {
...

instead.

3 Comments

should he change Robot constructor argument to Direction. because enums are designed not to be created.
It would still not be necessary to create them. One would call new Robot(Direction.DOWN) for example. Creating new instances is indeed not a good idea.
Just to clarify this: The static method to convert the integer would still not return a new instance, but would rather use Direction.UP etc. to deliver the enum.
0

try something like

package SO;

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(Direction dir) {
        this.direction = dir;
        this.RoboID = IDGenerator;
        IDGenerator++;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Direction: " + direction;
    }

    public static void main(String[] args) {
        Robot r = new Robot(Direction.UP);
        System.out.println(r);
    }
}

Pass direction while creating object of Robot.

Apart from that i personally would have used Builder pattern here, something like following

package SO;

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

    public Robot setDirection(Direction dir){
        this.direction = dir;
        return this;
    }

    public Robot setId(int id){
        this.RoboID = id;
        return this;
    }
    // Constructor for Robot

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "Direction: " + direction;
    }

    public static void main(String[] args) {
        Robot r = new Robot();
        r = r.setDirection(Direction.UP).setId(212);
        System.out.println(r);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.