2011 Route Cipher

<< 2011 FuelTank | APQuestionsTrailIndex | 2010CookieOrder >>

Question 4 from 2011 asks you to write two methods of the RouteCipher class, fillBlock and encryptMessage.

Click here to see the questions from 2011

In the original code on the Exam, they did not show the implementation of the encryptMessage method. If you can't figure out how to decrypt on your own, then looking at my implementation of the encryptMessage might give you a 'hint', but try not looking at it unless you have to...

RouteCipher.java

public class RouteCipher
{
	/** A two-dimensional array of single-character strings, instantiated in the constructor */ 
	private String[][] letterBlock;

	/** The number of rows of letterBlock, set by the constructor */ 
	private int numRows;

	/** The number of columns of letterBlock, set by the constructor */ 
	private int numCols;

	/**
	 * Places a string into letterBlock in row-major order. 
	 * @param str the string to be processed
	 * Postcondition:
	 *   if str.length() < numRows * numCols, "A" is placed in each unfilled cell
	 *   if str.length() > numRows * numCols, trailing characters are ignored
	 */

	private void fillBlock(String str)
	{ /* to be implemented in part (a) */ 

	}
	/** Extracts encrypted string from letterBlock in column-major order.
	 *  Precondition: letterBlock has been filled
	 *  @return the encrypted string from letterBlock
	 */
	private String encryptBlock()
	{ /* implementation not shown  
	  /* - the code below is Fr Chris' 
	   * - try not looking at it unless you need a hint 
	   */
		String code="";
		for (int c=0; c<numCols; c++)
			for(int r=0; r<numRows; r++){
				if (!letterBlock[r][c].equals("A"))
					code+=letterBlock[r][c];
			}
	 	return code;
	}
	 /** Encrypts a message.
	  * @param message the string to be encrypted
	  * @return the encrypted message;
	  * if message is the empty string, returns the empty string */
	public String encryptMessage(String message)
	{ /* to be implemented in part (b) */ 

	}
	// There may be instance variables, constructors, and methods that are not shown.
	// Including these, written by Fr Chris to get this to work with the tester class
	// If you need a hint, look at this, but this info was not available during the AP exam.
	public RouteCipher(int rows, int cols){
		this.letterBlock=new String[rows][cols];
		this.numRows=rows;
		this.numCols=cols;
	}
	public String showFillBlock(String message){
		fillBlock(message);
		String result="";
		for (int r=0; r<numRows; r++)
		{
			for(int c=0; c < numCols; c++)
			{
				result+="[\""+letterBlock[r][c]+"\"]";
			}
			result+="\n";
		}
		return result;
	}
}

RouteCipherTester.java

public class RouteCipherTester 
{
	public static void main(String[] args){
		RouteCipher example1 = new RouteCipher(2,4);
		String ex1="Surprise";
		String ex1out= example1.encryptMessage("Surprise");
		System.out.println("Example 1-\""+ex1+"\"\n"+example1.showFillBlock(ex1)+"Encrypts to \""+ex1out+"\"");
		RouteCipher example2 = new RouteCipher(3,5);
		String ex2="Meet at noon";
		String ex2out= example2.encryptMessage(ex2);
		RouteCipher example3 = new RouteCipher(3,5);
		System.out.println("\nExample 2-\""+ex2+"\"\n"+example2.showFillBlock(ex2)+"Encrypts to \""+ex2out+"\"");
		String ex3="Meet at midnight";
		String ex3out= example3.encryptMessage(ex3);
		System.out.println("\nExample 1-\""+ex3+"\"\n"+example3.showFillBlock(ex3)+"Encrypts to \""+ex3out+"\"");

	}
}

If all is well, the output of the Tester should look like this:

Example 1-"Surprise"
["S"]["u"]["r"]["p"]
["r"]["i"]["s"]["e"]
Encrypts to "Sruirspe"

Example 2-"Meet at noon"
["M"]["e"]["e"]["t"][" "]
["a"]["t"][" "]["n"]["o"]
["o"]["n"]["A"]["A"]["A"]
Encrypts to "Maoetne tn o"

Example 1-"Meet at midnight"
["M"]["e"]["e"]["t"][" "]
["a"]["t"][" "]["m"]["i"]
["d"]["n"]["i"]["g"]["h"]
Encrypts to "Madetne itmg iht"