Question: Write the WordSandwich method makeSandwich . This method returns a String created from its two String parameters as follows. Take the first n characters of

Write the WordSandwich method makeSandwich. This method returns a String created from its two String parameters as follows.

  • Take the first n characters of str1
  • Append str2 to the end of these characters
  • Append the remaining characters of str1 to the end of this String.

For example, the following table shows some results of calling makeSandwich.

str1

str2

n

makeSandwich(str1, str2, n)

bread

ham

2

brhamead

bread

ham

4

breahamd

ham

bread

1

hbreadam

Complete method makeSandwich below.

 /** @param str1 a String of characters * @param str2 a String of characters * @param n an integer value * Precondition: 0 <= n < str1.length() * @return A string composed of the first n characters of str1, followed by the * entirety of str2, followed by the remaining characters of str1. */ private String makeSandwich(String str1, String str2, int n)

_____________________-

Write the WordSandwich method allSandwiches. This method creates and returns a new array of String objects as follows.

Each element of the array will be a sandwich of str1 and str2: that is the characters of str2 are inserted into str1. The first String in the array consists of 1 letter of str1 followed by str2, followed by the rest of the letters of str1. For each subsequent String in the array the position in str1 where str2 starts is shifted by 1 until the final string in the array which consists of all but 1 letter of str1 followed by str2, followed by the final remaining letter of str1. For example, the call allSandwiches(bread, ham) will return the following array.

{bhamread, brhamead, brehamad, breahamd}

In writing allSandwiches you may call makeSandwich. Assume that makeSandwich works as specific regardless of what you wrote in part (a).

Complete method allSandwiches below.

 /** @param str1 a string * @param str2 another string * Precondition: str1.length() > 1 * @return An array of String objects created by inserting str2 into sequentially * different positions in str1. */ private String[] allSandwiches(String str1, String str2)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!