MCX2                                                                                                                 Math Department
Mr. Gary. Jaye                                                                                                    Danny Jaye, A.P.S.

                                        2000 APCS A Exam Question 4 or AB Exam Question 1

One way of encrypting a word is to encrypt pairs of letters in the
word together.  A scheme to do this is to fill a 6 x 6 square with
the 26 capital letters of the alphabet and the ten digits '0' thu
'9'. Each letter and digit appears exactly once in the square.
To encrypt a letter pair, the rectangle formed by the two letters is
used. Each letter of the original pair is replaced by the letter located
on the same and in the other corner of the rectangle. If both letters
happen to be in the same row or column, the letters are swapped.
For example, in the following arrangement, AP is encrypted as DM.
       S  T  U  V  W  X

       Y  Z  0  1  2  3

       4  5  6  7  8  9

       A--B--C--D  E  F
       |        |     
       G  H  I  J  K  L
       |        |
       M--N--O--P  Q  R
Consider the following defintions for a class that uses this scheme
to encrypt a word.
class Point {
  private int row;
  private int col;
  public Point () { this(0, 0); }
  public Point(int r, int c) { row = r; col = c; }
  public getRow() { return row; }
  public getCol() { rurn col; }
  public void setPoint(int row, int col) { this.row = row; this.col = col; }
}
public class Encryptor {
  private char[][] myMat;
  private Point GetCoordinates(char ch) {...}
     // returns the coords of ch in the 2-dimensional array
  public String EncryptTwo(final String pair) {...}
     //  returns an encrypted form of the pair
  public Encryptor() {...}
     // Fills the matrix with the 26 capital letters of the alphabet
     // and the 10 digits from '0' thru '9'
  public String EncryptWord(final String word) {...}
     // returns an ecrypted form of the word
  
}
(a) Write member method GetCoordinates as started below. GetCoordinates
    takes a given letter or digit and returns its row and column in the
    2-dimensional array. Assume that the parameter ch is a capital letter in
    the range 'A' to 'Z' or a digit in the range '0' through '9'.

    The following example shows the point locations of the character ch in the 
    given matrix. 

       Encryptor.myMat          ch         Point coordinates

       S  T  U  V  W  X          P         row = 5, col = 3   

       Y  Z  0  1  2  3          8         row = 2, col = 4   

       4  5  6  7  8  9          M         row = 5, col = 0   

       A  B  C  D  E  F
                     
       G  H  I  J  K  L
                
       M  N  O  P  Q  R
    Complete GetCoordinates below. 
   
    private Point GetCoordinates(char ch) {
    // precond: 'A' <= ch <= 'Z' or '0' <= ch <= '9'
    //postcond: returns the row and column of the location
    //          of ch in myMat  

 

(b) Write member method EncryptTwo, as started below. EncryptTwo is passed
    a two-chracter string and returns an encoded two-character string.

    The encoding of the letter pair is formed as follows.

       1. If both letters are in the same row or column, swap the two letters.
       2. Otherwise, find the other two corners of the rectangle formed by the two letters. Each
          letter of the original pair is replaced by the letter located on the same row and in 
          other corner of the rectangle. 

    For example, to encrypt a letter pair, say NE, look at the rectangle with the corners N and E.
    The encrypted letter pair is QB because Q is the letter at the other corner on the same row as
    N, and B is the letter at the other corner on the same row as E.


       S  T  U  V  W  X    

       Y  Z  0  1  2  3     

       4  5  6  7  8  9     

       A  B--C--D--E  F
          |        |   
       G  H  I  J  K  L
          |        |  
       M  N--O--P--Q  R
      Letters: BR  NE  ET  RE  TH  PR  GG
    Encrypted: FN  QB  BW  QF  HT  RP  GG

    In writing  EncryptTwo, you may call GetCoordinates specified in part (a). Assume that
    GetCoordinates works as specified, regardless of what you wrote in part (a). 

    Complete EncryptTwo below. 
   
    public String EncryptTwo(final String pair) {
    // precond: pair.length = 2
    //postcond: returns an encoded two-character string 
(c) Write member method EncryptWord, as started below. EncryptWord takes a word parameter and
    returns a string that contains the encryption of that word. Every two letter of the word are
    examined and encrypted by replacing the original letters with those located in the opposite corners
    of the rectangle formed by the two letters. If the original word contains an odd number of letters,                             
    the last letter is unchanged.

    The following are examples encrypted words using the 2-dimensional array shown below.

       S  T  U  V  W  X      

       Y  Z  0  1  2  3      

       4  5  6  7  8  9      

       A  B  C  D  E  F
                     
       G  H  I  J  K  L
                
       M  N  O  P  Q  R

         Word: CO MP UT ER    SC IE NC E    ST UD EN TS   
    Encrypted: OC PM TU FQ    UA KC OB E    TS VC BQ ST

    In writing EncryptWord, you may call EncryptTwo specified in part (b). Assume that
    EncryptTwo works as specified, regardless of what you wrote in part (b). 

    Complete EncryptWord below. 
   
    public Point EncryptWord(final String word) {
    // precond: word contains only capital letters 'A' thru 'Z' and digits '0' thru '9'
    //postcond: returns an encrypted version of word, in which every two letters have been
    //   examined and encrypted by replacing the original letters with those located in the
    //   opposite corners of the rectangle formed by the two letters. If the original word
    //   contains an odd number of letters, the last letter is left unchanged.