/* Encode.java allows its users to encode a message stored in a file *  using the Caesar cipher. * * The decoded message is stored in a file. * *  Begun by: Charles Hoot, for Hands On Java. *  Adapted from code by: Joel C. Adams, for Hands On C++. *  Completed by: *  Date: *   *  Specification: *    input(input file):   a sequence of characters. *   output(output file): the sequence of encoded input characters. **********************************************************************/import java.io.*;public class Encode{   public static void main(String Args[])   {      BufferedReader  inStream = null;      BufferedWriter outStream = null;      // 0. Create theKeyboard       // 1. Display introductory message      System.out.println( "\nThis program uses the Caesar cipher to encode the contents of a"		+ "\nfile and writes the encoded characters to another file.");        // 2. Prompt for and read name of the input file.      System.out.print( "\nEnter the name of the input file: ");      String inFileName="";       // 3. Open a BufferedReader named inStream for input from inFileName      // 4. If inStream failed to open, display an error message and quit      // 5. Prompt for and read name of the input file.      System.out.print( "\nEnter the name of the output file: ");      String outFileName="";       try      {          outFileName = theKeyboard.readLine();      }       catch (IOException ex)       {         System.err.println("Failed to read file name");         System.exit(1);      }      // 6. Open an BufferedWriter named outStream for output to outFileName      // 7. If outStream failed to open, display an error message and quit      int  inValue = 0;      char outChar=' ';        // 8. Loop      while(true)      {         // a. read a character from the input file via inStream into inValue         // b. if end-of-file was reached, terminate repetition         // c. encode the character using the Caesar cipher         outChar = caesarEncode( (char) inValue, 3);               // d. write the encoded character to the output file via outStream      }        // 9a. close the connection to the input file      // 9b. close the connection to the output file  	      // 10. display a 'successful completion' message      System.out.println("\nProcessing complete.\n Encoded message is in "			+ outFileName);   }   /*********************************************************************    * caesarEncode implements the Caesar cipher encoding scheme.        *    *                                                                   *     * Receive: ch, a character.                                         *    *          key, the amount by which to rotate ch.                   *    * Return:  The character that is key positions after ch,            *    *          with "wrap-around" to the beginning of the sequence.     *    *********************************************************************/   public static char caesarEncode(char ch, int key)   {      final int FIRST_UPPER = 65,                FIRST_LOWER = 97,                NUM_CHARS = 26;      if (key <= 0 || key >= NUM_CHARS)      {         System.err.println("\n*** CaesarEncode: key must be between 1 and 25\n");         System.exit(1);      }	        if (Character.isUpperCase(ch))         return (char) ((ch - FIRST_UPPER + key) % NUM_CHARS + FIRST_UPPER);      else if (Character.isLowerCase(ch))         return (char) ((ch - FIRST_LOWER + key) % NUM_CHARS + FIRST_LOWER);      else         return ch;   }} //end class
