ALT-C7X1:Rot13Tester.java

Rot13b.java

public class Rot13b
{
  private int[] textIn;
  private int[] textOut;
  
  /**
   * Constructor
   * @param s is the String to encode, s.length()>0
   * postCondition: Rot13b object instantiated
   *                textIn stores initial message in ASCII code
   *                TextIn stores encrypted message in ASCII code
   */  
  public Rot13b(String s)
  {
    textIn = new int[s.length()];
    for(int b = 0; b<s.length(); b++)
    {
      textIn[b]=(int)(s.charAt(b));
    }
    
    textOut = new int[s.length()];
    for(int b = 0; b<s.length(); b++)
    {
      textOut[b]=encrypt((char)textIn[b]);
    }
  }
  
  /**
   * Accessor
   * @param ch is a char to encrypt using Rot13 Caesar Cypher Algorithm
   * @return encrypted char 
   */
  private char encrypt(char ch)
  {
    if(Character.isUpperCase(ch))
    {
      return (char)(((ch-'A')+13)%26+'A');
    }
    if(Character.isLowerCase(ch))
    {
      return (char)(((ch-'a')+13)%26+'a');
    }
    return ch;
  }
  
  /**
   * Accessor
   * preCondition: textIn has been encrypted and stored in textOut
   * @return original and encrypted message
   */
  public String toString()
  {
    String temp="Input  = ";
    for(int i=0; i<textIn.length; i++)
    {
      temp+=(char)textIn[i];
    }
    temp+="\n\nOutput = ";;
    for(int i=0; i<textOut.length; i++)
    {
      temp+=(char)textOut[i];
    }
    return temp;
  }
  
  //optional for debugging
  public int[] getTextIn()
  {
    return textIn;
  }
  
  //optional for debugging
  public int[] getTextOut()
  {
    return textOut;
  }
}



Rot13c.java

import java.util.ArrayList;
public class Rot13c
{
  private ArrayList<Integer> textIn;
  private ArrayList<Integer> textOut;
  
  /**
   * Constructor
   * @param s is the String to encode, s.length()>0
   * postCondition: Rot13c object instantiated
   *                textIn stores initial message in ASCII code
   *                TextIn stores encrypted message in ASCII code
   */ 
  public Rot13c(String s)
  {
    textIn = new ArrayList<Integer>();
    for(int b = 0; b<s.length(); b++)
    {
      textIn.add((int)(s.charAt(b)));
    }
    
    textOut = new ArrayList<Integer>();
    for(int b = 0; b<s.length(); b++)
    {
      textOut.add(encrypt(textIn.get(b)));
    }
  }
  
  /**
   * Accessor
   * @param ch is a char to encrypt using Rot13 Caesar Cypher Algorithm
   * @return encrypted char 
   */
  private int encrypt(int ascii)
  {
    if(Character.isUpperCase(ascii))
    {
      return (int)(((ascii-65)+13)%26+65);
    }
    if(Character.isLowerCase(ascii))
    {
      return (int)(((ascii-97)+13)%26+97);
    }
    return ascii;
  }
  
  /**
   * Accessor
   * preCondition: textIn has been encrypted and stored in textOut
   * @return original and encrypted message
   */
  public String toString()
  {
    String temp="Input  = ";
    for(int i=0; i<textIn.size(); i++)
    {
      temp+=(char)textIn.get(i).intValue();
    }
    temp+="\n\nOutput = ";;
    for(int i=0; i<textOut.size(); i++)
    {
      temp+=(char)textOut.get(i).intValue();
    }
    return temp;
  }
  
  //optional for debugging only
  public ArrayList<Integer> getTextIn()
  {
    return textIn;
  }

  //optional for debugging only
  public ArrayList<Integer> getTextOut()
  {
    return textOut;
  }
}



Rot13a.java

public class Rot13a
{
  private String textIn;
  private String textOut;
  
  /**
   * Constructor
   * @param s is the String to encode, s.length()>0
   * postCondition: Rot13a object instantiated
   *                textIn stores initial message
   *                TextIn stores encrypted message
   */
  public Rot13a(String s)
  {
    textIn = s;
    textOut = "";
    
    for(int i = 0; i<s.length(); i++)
    {
      textOut += encrypt(s.charAt(i));
    }
  }
  
  /**
   * Accessor
   * preCondition: textIn has been encrypted and stored in textOut
   * @return encrypted message
   */
  public String toString()
  {
    return textOut;
  }
  
  /**
   * Accessor
   * @param ch is a char to encrypt using Rot13 Caesar Cypher Algorithm
   * @return encrypted char 
   */
  private char encrypt(char ch)
  {
    if(Character.isUpperCase(ch))
    {
      return (char)(((ch-'A')+13)%26+'A');
    }
    if(Character.isLowerCase(ch))
    {
      return (char)(((ch-'a')+13)%26+'a');
    }
    return ch;
  }
}








Main.java

/*
Rot13Tester.java  MrG 2017.0310
purpose:  create a Caesar Cypher 3 different ways practicing 
          String, Array and ArrayList traversal algorithms
Required files: Rot13Tester.java    (main class)
                Rot13a.java         (derived class based on Srting)
                Rot13b.java         (derived class based on int[])
                Rot13c.java         (derived class based on ArrayList)
translator phase:  javac Rot13Tester.java
interpreter phase: java Rot13Tester
*/
import java.util.Scanner;
import java.util.ArrayList;
public class Main 
{
  public static void main(String[] args)
  {
    Scanner bob = new Scanner(System.in);
    System.out.println("enter text to encode: ");
    String s = bob.nextLine();
    System.out.println();
    
    Rot13c ross = new Rot13c(s);
    System.out.println(ross);
    
    System.out.println();
    
    ArrayList<Integer> temp = ross.getTextIn();
    for(int i = 0; i<temp.size(); i++)
    {
      System.out.print(temp.get(i)+" ");
    }
    
    System.out.println();
    System.out.println();
    
    temp = ross.getTextOut();
    for(int i = 0; i<temp.size(); i++)
    {
      System.out.print(temp.get(i)+" ");
    }
  }
}