Question: public class Utils { /** * * Returns a copy of the array 'in' where each word occurring in the array * * 'what' has

 public class Utils { /** * * Returns a copy ofthe array 'in' where each word occurring in the array * *

public class Utils {

/**

*

* Returns a copy of the array 'in' where each word occurring in the array

*

* 'what' has been replaced by the word occurring in the same position

*

* in the array 'with'.

*

*

*

* @param in

* an array of Strings;

*

* @param what

* an array of words to be replaced;

*

* @param with

* an array of replacement words;

*

* @return a new array idententical to 'in' except that all the occurrences

* of words

*

* found in 'what' have been replaced by the corresponding word from

* 'with'.

*/

public static String[] findAndReplace(String[] in, String[] what,

String[] with) {

String[] out = null; // The new array to be returned

boolean valid = true; // True if the pre-conditions are satistified

// Testing pre-conditions

if (in == null || what == null || with == null) {

valid = false;

} else {

// if length of what is not equal to that of with, setting valid to

// false

if (what.length != with.length) {

valid = false;

} else {

// looping through each element in 'in', and if any element is

// null, setting valid to false and exiting loop

for (String str : in) {

if (str == null) {

valid = false;

break;

}

}

// looping through each element in what and with arrays, if any

// element is null, setting valid to false and exiting loop

for (int i = 0; i

if (what[i] == null || with[i] == null) {

valid = false;

break;

}

}

}

}

if (valid) {

out = new String[in.length];

for (int i = 0; i

// storing word at index i in in array

String word = in[i];

// looping and checking if this word is in what array

for (int j = 0; j

if (what[j].equals(word)) {

// found, replacing word with element at index j in with

// array

word = with[j];

break; //exiting inner loop

}

}

//setting word at index i in out

out[i] = word;

}

}

// Returning a reference to the newly created array that

// contains the same entries as 'in' except that all the

// occurrences of words from 'what' have been replaced by

// their corresponding occurrence from 'with'.

return out;

}

}

public static String[] findAndReplace(String[] in, String[] what,

String[] with) {

this part is an error, how to fix it

2 Manipulating arrays - FindAnd Replace Complete the implementation of the (static) class method String[] findAndReplace(String[] in, String[] what, String[] with) of the class Utils. The method returns a copy of the array in where each word occurring in the array what has been replaced by the word occurring at the corresponding position in the array with. The array designated by in must remain unchanged. If there are duplicate words in the array what, simply use the first instance you find. Later in the semester, we will learn about exceptions. Exceptions provide tools for handling error situations. In the meantime, the method findAndReplace returns null whenever one of the preconditions of the methods is violated: In particular, the formal parameters cannot be null. For all three arrays, none of the elements can be null. The query and replacement arrays must be of the same length. You will hand this exercise in. Download and complete the starter file here: Utils.java. public class Utils i * * Returns a copy of the array in' where each word occurring in the array what' has been replaced by the word occurring in the same position * in the array 'with'. ** * @param in an array of Strings; * @param what an array of words to be replaced; * @param with an array of replacement words: * @return a new array idententical to 'in' except that all the occurrences of words * found in 'what' have been replaced by the corresponding word from 'with'. */ public static String[] findAndReplace(String[] in, String [] what, String[] with ) { String [] out = null; // The new array to be returned boolean valid = true; // True if the pre-conditions are satistified // Testing pre-conditions if ( in == null || what == null || with == null ) { valid = false; } else { // more or less 16 lines missing } if (valid ) { out = new String in. length ]: for (int i=0; i

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!