8.7.4. Free Response - Route Cipher B¶
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 https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year.
Question 4. 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.
For example, the word “Surprise” can be encrypted using a 2-row, 4-column array as follows.
An incomplete implementation of the RouteCipher
class is shown below.
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
*/
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 */
}
/**
* 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
}
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.
The following example shows the process carried out if letterBlock
has 2 rows and 3 columns and encryptMessage("Meet at midnight")
is executed.
In this example, the method returns the string “Mte eati dmnitgAhA”.
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.
8.7.4.1. How to Solve This¶
9-13-1: Explain in plain English what your code will have to do to answer this question. Use the variable names given above.
This section contains a plain English explanation of one way to solve this problem as well as problems that test your understanding of how to write the code to do those things. Click on the buttons to reveal the questions.
To solve this problem, you have a message that you need to process and concatenate. Do you need to use a loop for this?
- while
- Correct!
- if
- You would need a loop.
- for
- For this problem, a while loop would be easier to use.
- switch statement
- You would need a loop.
9-13-2: What type of loop could you use to solve this problem?
- while (message.substring(k, k + 1) < 0)
- You do not need to apply that formula here.
- while (message.substring(k, k + 1) > 0)
- You do not need to apply that formula here.
- while (message.length() < 0)
- The inequality is backwards.
- while (message.length() > 0)
- Correct!
9-13-3: What should your while statement conditional be?
- int chunkSize = this.numRows * this.numCols;
- Correct!
- int chunkSize = this.numRows + this.numCols;
- This does not give you the correct result.
- int chunkSize = this.numRows - this.numCols;
- This does not give you the correct result.
- int chunkSize = this.numRows / this.numCols;
- This does not give you the correct result.
9-13-4: How can you determine how large the “chunk size” should be?
- chunkSize = message.substring(chunkSize);
- This does not give you the correct result.
- chunkSize = += encryptBlock();
- This does not give you the correct result.
- chunkSize = message.length();
- Correct!
- chunkSize = fillBlock(message);
- This does not give you the correct result.
9-13-5: If chunkSize is greater that message.length(), what should you set chunkSize to?
- encryptMessage(message);
- This is the method we are trying to write!
- fillBlock(message);
- Correct!
- RouteCipher(message);
- RouteCipher is the class of the method we are currently writing.
- encryptBlock(message);
- We need to call a different method before we call encryptBlock()
9-13-6: What method needs to be called on message before you can call “encryptBlock()”?
Below is a mixed up version of the correct solution hinted at by the previous questions.
The method encryptMessage below contains the correct code for one solution to this problem, but it is mixed up and contains extra blocks that are not needed. 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.
8.7.4.2. Solve Part B¶
Complete method encryptMessage
below.
8.7.4.3. Alternate Recursive Solution¶
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 completed unit 10 which covers the basics of recursion, feel free to return to this question to practice working through the recursive solution.
- if (message.length() == 0) { return ""; }
- Correct!
- if (message.length() <= this.numRows * this.numCols) { return encryptBlock(); }
- This would not work in this situation.
- return "";
- This would not work in this situation.
- return encryptBlock();
- This would not work in this situation.
9-13-9: What is your base case?
- Head
- The recursive call is not the first statement in the method hence it is not head recursive.
- Tail
- Correct!
- Tree
- We do not make multiple recursive calls so the method is not tree recursive.
- Body
- This is not a term that describes recursion.
9-13-10: What kind of recursion will you use?
- return "";
- This is the return statement of the base case.
- return (encryptMessage(message.substring(this.numRows * this.numCols)));
- This does not return the correct results
- return (encryptBlock());
- This is the return statement of one of the conditional base cases.
- return (encryptBlock() + encryptMessage(message.substring(this.numRows * this.numCols)));
- Correct!
9-13-11: What is your tail recursive call?
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 and contains extra blocks that are not needed. 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.