Add JavaDoc comments to the method findAndReplace . Each comment should include
ID: 3787064 • Letter: A
Question
Add JavaDoc comments to the method findAndReplace. Each comment should include a brief description of what the method does and descriptions of the parameters and the return value using JavaDoc format.
Add a brief description of the class Utils, using the JavaDoc syntax, making sure to include the name of the author of the class (you).
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 {
for(int i=0;i<in.length;i++){
if(in[i]==null){
valid = false;
break;
}
}
for(int i=0;i<what.length;i++){
if(what[i]==null){
valid = false;
break;
}
}
for(int i=0;i<with.length;i++){
if(with[i]==null){
valid = false;
break;
}
}
}
if ( valid ) {
String concat = " ";
for ( int i=0; i<in.length; i++ ) {
concat = concat+" "+in[i];
}
for(int i=0;i<what.length;i++){
concat = concat.trim().replaceAll(what[i], with[i]);
}
out = concat.split(" ");
}
// 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;
}
}
Explanation / Answer
Hi I think all sections are commented.
Please let me know in case of any issue.
/**
*
* @author pravesh
*
* This Utils class contains static method
*/
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'.
*/
/* Precondition: all parameter should not be null
all array element should not be null
*/
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 {
for(int i=0;i<in.length;i++){
if(in[i]==null){
valid = false;
break;
}
}
for(int i=0;i<what.length;i++){
if(what[i]==null){
valid = false;
break;
}
}
for(int i=0;i<with.length;i++){
if(with[i]==null){
valid = false;
break;
}
}
}
if ( valid ) {
String concat = " ";
for ( int i=0; i<in.length; i++ ) {
concat = concat+" "+in[i];
}
for(int i=0;i<what.length;i++){
concat = concat.trim().replaceAll(what[i], with[i]);
}
out = concat.split(" ");
}
// 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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.