In this question you will write two methods for a class RouteCipher that encrypts (puts into a coded form) a) Write the method fillBlock that fills the two-dimensional array letterBlock with one-character a message by changing the order of the characters in the message. The route cipher fills a strings from the string passed as parameter str. 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. Original Message *Surprise" Contents of Array "Sur "p" "ris" "e" Encrypted Message *Sruirspe* 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 if str.length() > numRows numCols, "A" is placed in each unfilled cell 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. 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. "M" "e" "e" | "t" "a" "t" "n" "o" "n" "A" "A" "A" 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. "M" "e" "e" "t" "a" "t" "d" "n" i "g" "h" The following expression may be used to obtain a single-character string at position k of the string str. str.substring(k, k+1) Complete method fill Block below. /** 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) } 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, nonoverlapping substrings of message and concatenating the results returned by a call to encryptBlock after each call to fill Block. 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. Substring letterBlock after Call to fillBlock Value Returned by encryptBlock Concatenated String "M" "e" "Mte ea" "Mte ea" "Meet a" "t" "t midn" "t" "m" "ti dmn" "Mte eati dmn" "1" "d" "n" "ight" "g" "h" "itgAhA" "Mte eati dmnitgAhA" "A" "A" 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. Complete method encryptMessage below. /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)
In this question you will write two methods for a class RouteCipher that encrypts (puts into a coded form) a) Write the method fillBlock that fills the two-dimensional array letterBlock with one-character a message by changing the order of the characters in the message. The route cipher fills a strings from the string passed as parameter str. 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. Original Message *Surprise" Contents of Array "Sur "p" "ris" "e" Encrypted Message *Sruirspe* 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 if str.length() > numRows numCols, "A" is placed in each unfilled cell 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. 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. "M" "e" "e" | "t" "a" "t" "n" "o" "n" "A" "A" "A" 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. "M" "e" "e" "t" "a" "t" "d" "n" i "g" "h" The following expression may be used to obtain a single-character string at position k of the string str. str.substring(k, k+1) Complete method fill Block below. /** 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) } 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, nonoverlapping substrings of message and concatenating the results returned by a call to encryptBlock after each call to fill Block. 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. Substring letterBlock after Call to fillBlock Value Returned by encryptBlock Concatenated String "M" "e" "Mte ea" "Mte ea" "Meet a" "t" "t midn" "t" "m" "ti dmn" "Mte eati dmn" "1" "d" "n" "ight" "g" "h" "itgAhA" "Mte eati dmnitgAhA" "A" "A" 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. Complete method encryptMessage below. /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)
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Please solve part a and b.
AI-Generated Solution
AI-generated content may present inaccurate or offensive content that does not represent bartleby’s views.
Unlock instant AI solutions
Tap the button
to generate a solution
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education