Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 orderthe 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.

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

str.substring(k, k + 1) 

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.

class Main {

public static void main (String[] args)

{

RouteCipher c = new RouteCipher();

c.numRows = 2;

c.numCols = 3;

c.letterBlock = new String[c.numRows][c.numCols];

System.out.println("[" + c.encryptMessage("Meet at midnight") +"]");

}

}

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;

// Part (a)

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

{

}

/** Extracts encrypted string from letterBlock in column-major order.

* Precondition: letterBlock has been filled

* @return the encrypted string from letterBlock

*/

private String encryptBlock()

{

String str = "";

for (int c = 0; c < numCols; c++)

for (int r = 0; r < numRows; r++)

str += letterBlock[r][c];

return str;

}

// Part (b)

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

{

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

1. Who is responsible for resolving this dilemma?

Answered: 1 week ago