Monday, 27 January 2014

JAVA: Displaying the position of the compass.

Get the direction and display the position of the compass.

public class directionProgram {

 static char dir[] = new char[4];
 static int R;

 public static void main(String args[]) {

  dir[0] = 'N';
  dir[1] = 'E';
  dir[2] = 'S';
  dir[3] = 'W';

  directionProgram obj = new directionProgram();
  obj.getDirection("L", 3);
  obj.getDirection("L", 1);
  obj.getDirection("L", 2);
  obj.getDirection("R", 1);
  System.out.println("the position is " + dir[R]);
 }

 public void getDirection(String d, int rontations) {

  if (rontations >= 4) {
   rontations = rontations % 4;
  }

  if (d == "R") {
   R = R + rontations;
  } else {
   R = R + 4 - rontations;

  }
  if (R >= 4) {
   R = R % 4;
  }
 }
}


No comments:

Post a Comment