8.7.3. Free Response - Route Cipher A

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.

../_images/routeCipherFig.png

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
     */
    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
}

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.

../_images/routeCipherTable.png

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.

../_images/routeCipherTable2.png

The following expression may be used to obtain a single-character string at position k of the string str.

str.substring(k, k + 1)

8.7.3.1. How to Solve This

9-12-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.

8.7.3.2. The Algorithm

The exercises above have been guiding you towards just one of many possible solutions. To read through a different solution, click on the Reveal button below and complete the problem.

8.7.3.3. Try And Solve It

Complete the method fillBlock below.

You have attempted of activities on this page