I did a lot of Java programming and then dropped it and did a bunch of ruby. Now I am back in Java and I am wondering whether my programming style is strange.
My code below feels very verbose to me, and the question is it reasonably idiomatic Java?
Any suggestions/improvements that you would recommend?
final static int FORCE_RIGHT = 0;
final static int FORCE_DOWN = 1;
final static int FORCE_LEFT = 2;
final static int FORCE_UP = 3;
final static int IMP_RIGHT = 4;
final static int IMP_DOWN = 5;
final static int IMP_LEFT = 6;
final static int IMP_UP = 7;
public void applyForce(int dir) {
counter++;
Vector2 vect = new Vector2();
switch (dir) {
case FORCE_RIGHT: vect = new Vector2(3.0f, 0.0f); break;
case IMP_RIGHT: vect = new Vector2(1.0f, 0.0f); break;
case FORCE_LEFT: vect = new Vector2(-3.0f, 0.0f); break;
case IMP_LEFT: vect = new Vector2(-1.0f, 0.0f); break;
case FORCE_UP: vect = new Vector2(0.0f, -3.0f); break;
case IMP_UP: vect = new Vector2(0.0f, -1.0f); break;
case FORCE_DOWN: vect = new Vector2(0.0f, 3.0f); break;
case IMP_DOWN: vect = new Vector2(0.0f, 1.0f); break;
}
Vector2 place = body.getWorldCenter();
if (dir == FORCE_RIGHT || dir == FORCE_LEFT || dir == FORCE_DOWN || dir == FORCE_UP)
{
body.applyForce(vect, place);
}
else
{
body.applyLinearImpulse(vect, place);
}
Log.v("CAR", "Applied force: " + dir + "("+counter+")");
}
Here are my improvement suggestions: