The following is a free response question from 2011. It was question 4 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.
In this question you will write two methods for a class RouteCipher that encrypts (puts into a coded form) a message by changing the order of the characters in the message. The route cipher fills a two-dimensional array with single-character substrings of the original message in row-major order, encrypting the message by retrieving the single-character substrings in column-major order.
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" in each unfilled cell if str.length() > numRows *
* numCols, trailing characters are ignored
*/
public 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 */
}
/**
* 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
}
Subsection4.54.1Route Cipher A Description
Part a. Write the method fillBlock that fills the two-dimensional array letterBlock with one-character strings from the string passed as parameter str.
The array must be filled in row-major orderβthe first row is filled from left to right, then the second row is filled from left to right, and so on, until all rows are filled.
If the length of the parameter str is smaller than the number of elements of the array, the string βAβ is placed in each of the unfilled cells. If the length of str is larger than the number of elements in the array, the trailing characters are ignored.
For example, if letterBlock has 3 rows and 5 columns and str is the string βMeet at noonβ, the resulting contents of letterBlock would be as shown in the following table.
If letterBlock has 3 rows and 5 columns and str is the string βMeet at midnightβ, the resulting contents of letterBlock would be as shown in the following table.
We need to utilize elements by indexing them so a for each loop will not work
if
This is not a type of loop
for
Correct!
while
Although this could would, we would need some kind of tracker variable to allow use to count indexes which would be more easily accomplished by a different loop.
str.substring(c + r * this.numCols, 1 + c + r * this.numCols)
Correct!
str.substring(c - r * this.numCols, 1 + c - r * this.numCols)
Try using this formula to find a given character of one of the example strings. Does it work? Try coming up with some of your own examples to figure out the forumla for k.
str.substring(c + r, 1 + c + r)
Try using this formula to find a given character of one of the example strings. Does it work? Try coming up with some of your own examples to figure out the forumla for k.
str.substring(c - r, 1 + c - r)
Try using this formula to find a given character of one of the example strings. Does it work? Try coming up with some of your own examples to figure out the forumla for k.
The method fillBlock below contains the correct code for one solution to this problem, but it is mixed up. Drag the needed code from the left to the right and put them in order with the correct indention so that the code would work correctly.
public void fillBlock(String str)
{
int pos = 0;
---
for (int r = 0; r < this.numRows; r++ )
{
---
for (int c = 0; c < this.numCols; c++ )
{
---
if (pos < str.length())
{
---
String subStr = str.substring(pos, pos+1);
this.letterBlock[r][c] = subStr;
pos++;
---
} else
{
this.letterBlock[r][c] = "A";
} // end else block
---
} // end inner for
} // end outer for
} // end method
Part b. Write the method encryptMessage that encrypts its string parameter message. The method builds an encrypted version of message by repeatedly calling fillBlock with consecutive, non-overlapping substrings of message and concatenating the results returned by a call to encryptBlock after each call to fillBlock. When all of message has been processed, the concatenated string is returned. Note that if message is the empty string, encryptMessage returns an empty string.
Assume that fillBlock and encryptBlock methods work as specified. Solutions that reimplement the functionality of one or both of these methods will not receive full credit.
The method encryptMessage below contains the correct code for one solution to this problem, but it is mixed up. Drag the needed code from the left to the right and put them in order with the correct indention so that the code would work correctly.
Instead of using loops, this problem can be solved using recursion. If you are unfamiliar with recursion do not worry if the recursive solution does not make immediate sense. It is not necessary that you understand recursion at this point however, once you have learned about recursion, feel free to return to this question to practice working through the recursive solution.
If you still feel unsure of the recursive solution, it is recommended that you return to the recursion unit to do some more practice as this problem is quite challenging to solve recursively.
The method encryptMessage below contains the correct code for one solution to this problem, but it is mixed up. Drag the needed code from the left to the right and put them in order with the correct indention so that the code would work correctly.