/***************************************************************
 Prepare for a QUIZ Monday.  It will cover STRING methods,
 and will be done using the COMPUTER and your NOTES.
 Prepare by PRACTICING with String programs.
 Ensure that you can successfully use the following commands:
   .length()
   .substring(int,int)
   .indexOf(String)
   .charAt(int)
   .toUpperCase()
****************************************************************/

import java.awt.*;

public class Encrypt extends EasyApp
{  public static void main(String[] args)
   {
      new Encrypt();
   }
   
   TextField tMessage = addTextField("",50,50,100,30,this);
   Button bEncrypt = addButton("Encrypt",50,100,100,30,this);
   TextField tSecret = addTextField("",50,150,100,30,this);
   Button bDecrypt = addButton("Decrypt",50,200,100,30,this);
   TextField tDecrypt = addTextField("",50,250,100,30,this);
   
   public void actions(Object source,String command)
   {
      if (source == bEncrypt) { encrypt(); }
      if (source == bDecrypt) { decrypt(); }
   }
   
   public void encrypt()
   {
      String info = tMessage.getText();
      
      int size = info.length();
      char first = info.charAt(0);
      char last = info.charAt(size-1);
      String middle = info.substring(1,size-1);

      String secret = last + middle + first;
      
      tSecret.setText(secret);
   }
   
   public void decrypt()
   {
      String info = tSecret.getText();
      
      int size = info.length();
      char first = info.charAt(0);
      char last = info.charAt(size-1);
      String middle = info.substring(1,size-1);

      String unscrambled = last + middle + first;
      
      tDecrypt.setText(unscrambled);
   }
}

/****************************************************************

Encrypt means translating a message into a secret code,
to make it unreadable.  The Encrypt program is a VERY SIMPLE
version of this - it just scrambles up the letters.  

A better (more realistic) ENCRYPTION ALGORITHM should change
the letters.  For example, a Caesar cipher (named for Julius Ceasar)
changes each letter in the alphabet to a different letter, for
example changing  'A' --> 'C' ,  'M' --> 'O' , etc,
always moving 2 letters further in the alphabet.  At the end
there is no place for Y and Z, so they must "wrap around" to
the beginning, becoming A and B respectively.

The following CODE FRAGMENT performs this cipher:

      String message = "I LOVE ZZTOP";
    
      String secret = "";

      for (int c=0; c < message.length(); c++)
      {  
         char letter = message.charAt(c);
         int code = (int)letter;
         code = code + 2;
         char newLetter = (char)code;
         secret = secret + newLetter;
      }

      output(secret);
      
(1)  Change the ALGORITHM above so that it will work correctly
     as the encrypt() method in the program.
     
(2)  Make a correpsonding decrypt() algorithm to decipher
     the secret message (after it was encrypted).
     
(3)  What happens to the letter Z in this algorithm?
     Use an if.. command to change Z to the correct letter B.
     Add if.. commands for any other characters that do not
     turn into sensible characters.  You also need to make
     corresponding changes in the decrypt method.
     
(4)  Write a new algorithm to make "silly tilk" .
     This works by changing all vowels to 'i'.  
     You only need if.. commands for this -
     you won't need to do any calculations with ASCII codes.
     
     Explain why it is not possible to make a decrypt() method
     for "silly tilk".
     
*****************************************************************/