/* Decode.java allows its users to decode a message stored in a file *  using the Caesar cipher. * *  The encoded 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):   an encoded sequence of characters. *     output(output file): the sequence of decoded input characters. **********************************************************************/import java.io.*;public class Decode{   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 decode the contents of a"		+ "\nfile and writes the decoded characters to another file.");        // 2. Prompt for and read name of the input file.	      // 3. Open a BufferedReader named inStream for input from inFile      // 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 outFile="";       // 6. Open an ofstream named outStream for output to outFile      // 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. decode the character using the Caesar cipher         outChar = caesarDecode((char) inValue, 3);               // d. write the decoded 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 Decoded message is in "			+ outFile);   }   /********************************************************************    * caesarDecode implements the Caesar cipher encoding scheme.       *    *                                                                  *     * Receive: ch, a character,                                        *    *          key, an integer.                                        *    * Return:  The character that is key positions before ch,          *    *          with "wrap-around" to the end of the sequence.          *    ********************************************************************/   public static char caesarDecode(char ch, int key)   {      final int FIRST_UPPER = 65,                FIRST_LOWER = 97,                NUM_CHARS = 26;	        if (key <= 0 || key >= 26)      {         System.err.println("\n*** CaesarDecode: key must be between 1 and 25!\n");         System.exit(1);      }      if (Character.isUpperCase(ch))         return (char) ((ch - FIRST_UPPER + NUM_CHARS - key) % NUM_CHARS + FIRST_UPPER);      else if (Character.isLowerCase(ch))         return (char) ((ch - FIRST_LOWER + NUM_CHARS - key) % NUM_CHARS + FIRST_LOWER);      else         return ch;   }} //end class
