Lab Two: Loop, Input and Output

The computer programs that control an FRC robot all follow the same pattern: a loop. In each pass through the loop the following things happen:

We're going to write a program that runs on your computer and simulates these three steps. By pressing certain keys on your keyboard you will move a character on the screen.

package labtwo;

/**
 *
@author Bogdan
 */
public class LabTwo {

   
/**
     *
@param args the command line arguments
     */
   
public static void main(String[] args) {
       
Decision.position = 20;
        Decision.positionUpdated =
true;
        Decision.keepRunning =
true;

        printOutput
();

       
while (Decision.keepRunning) {
           
int input = readInput();
            decideMove
(input);
            printOutput
();
       
}
    }

   
public static void printOutput() {
       
if (Decision.positionUpdated) {
           
String formatString = "%" + Decision.position + "s";
            String output = String.format
(formatString, '|');
            System.out.println
(output);
       
}
    }

   
public static int readInput() {
       
int readCharacter;

       
try {
           
readCharacter = System.in.read();
       
} catch (java.io.IOException e) {
           
readCharacter = -1;
            System.out.println
("IOException reading from input");
       
}

       
return readCharacter;
   
}

   
public static void decideMove(int keyPressed) {
       
       
switch (keyPressed) {
           
case 'a':
                Decision.position--;
                Decision.positionUpdated =
true;
               
break;
           
case 's':
                Decision.position++;
                Decision.positionUpdated =
true;
               
break;
           
case 'q':
                Decision.keepRunning =
false;
                Decision.positionUpdated =
false;
               
break;
           
case -1:
                Decision.keepRunning =
false;
                Decision.positionUpdated =
false;
               
break;
           
default:
                Decision.positionUpdated =
false;
       
}
    }
   
   
public static class Decision {
       
public static int position;
       
public static boolean positionUpdated;
       
public static boolean keepRunning;
   
}
}