Limited access

Upgrade to access all content for this subject

Consider the following method written by a programmer. The method copies the element from the parameter array to a new array if the first character of the element is an “a”, “b”, or “c” ANDthe first character matches the second character. Otherwise the first character is repeated at the front of the String element and added to the output array.

// Prerequisite: Each element in input will be at least two characters long.
public static String[] echoChars(String[] input)
{
    String[] out = new String[input.length];
    for (int i=0; i<input.length; i++)
    {
        if (input[i].substring(0,1).equals("a") &&           
                input[i].substring(1,2).equals("a"))
            out[i] = input[i];
        else
        if (input[i].substring(0,1).equals("b") && 
                input[i].substring(1,2).equals("b"))
            out[i] = input[i];
        else
        if (input[i].substring(0,1).equals("c") && 
                input[i].substring(1,2).equals("c"))
            out[i] = input[i];
        else
            out[i] = input[i].substring(0,1) + 
                     input[i].substring(0,1) + input[i].substring(1);
    }
    return out;
}

For example, if the following code is run:

String[] input = {"dog", "aardvark", "funny", "bbbrrr", "cat", "ccrazy", "ddonut"};
String[] out = echoChars(input);

The String array named out would contain the elements:

"ddog", "aardvark", "ffunny", "bbbrrr", "ccat", "ccrazy", "dddonut"

However, the programmer anticipates that in the future this method may need to accommodate other sets of characters (not only “a”, “b”, or “c”). So she would like to make the method more generic.

Which of the following methods would satisfy this requirement?

A
public static String[] echoChars(String[] input, String firstChar)
{
    String[] out = new String[input.length];
    for (int i=0; i<input.length; i++)
    {
        if (firstChar.indexOf(input[i].substring(0,1)) >= 0 && 
            firstChar.indexOf(input[i].substring(1,2)) >= 0)
            out[i] = input[i];
        else
            out[i] = input[i].substring(0,1) + input[i].substring(0,1) +  
                           input[i].substring(1);
    }
    return out;
}

Then call the method as follows to replicate the example with characters “a”, “b”, and “c”:

String[] input = {"dog", "aardvark", "funny", "bbbrrr", "cat", "ccrazy", "ddonut"};
String[] out = echoChars(input, "abc");
B
public static String[] echoChars(String[] input, String firstChar)
{
    String[] out = new String[input.length];
    for (int i=0; i<input.length; i++)
    {
        if (firstChar.indexOf(input[i].substring(0,1)) >= 0 && 
            input[i].substring(0,1).equals(input[i].substring(1,2)))
            out[i] = input[i];
        else
            out[i] = input[i].substring(0,1) + input[i];
    }
    return out;
}

Then call the method as follows to replicate the example with characters “a”, “b”, and “c”:

String[] input = {"dog", "aardvark", "funny", "bbbrrr", "cat", "ccrazy", "ddonut"};
String[] out = echoChars(input, "abc");
C
public static String[] echoChars(String[] input, String[] firstChar)
{
    String[] out = new String[input.length];
    for (int i=0; i<input.length; i++)
    {
        if (firstChar[i].indexOf(input[i].substring(0,1)) >= 0 && 
            input[i].substring(0,1).equals(input[i].substring(1,2)))
            out[i] = input[i];
        else
            out[i] = input[i].substring(0,1) + input[i].substring(0);
    }
    return out;
}

Then call the method as follows to replicate the example with characters “a”, “b”, and “c”:

String[] input = {"dog", "aardvark", "funny", "bbbrrr", "cat", "ccrazy", "ddonut"};
String[] firstChar = {"a", "b", "c"};
String[] out = echoChars(input, firstChar);
D
public static String[] echoChars(String[] input, String[] firstChar)
{
    String[] out = new String[input.length];
    for (int i=0; i<input.length; i++)
    {
        if (firstChar[i].equals(input[i].substring(0,1)) && 
            input[i].substring(0,1).equals(input[i].substring(1,2)))
            out[i] = input[i];
        else
            out[i] = input[i].substring(0,1) + input[i];
    }
    return out;
}

Then call the method as follows to replicate the example with characters “a”, “b”, and “c”:

String[] input = {"dog", "aardvark", "funny", "bbbrrr", "cat", "ccrazy", "ddonut"};
String[] firstChar = {"a", "b", "c"};
String[] out = echoChars(input, firstChar);
E
public static String[] echoChars(String[] input, String[] firstChar)
{
    String[] out = new String[input.length];
    for (int i=0; i<input.length; i++)
    {
        if (i<firstChar.length && 
           firstChar[i].equals(input[i].substring(0,1)) && 
           input[i].substring(0,1).equals(input[i].substring(1,2)))
            out[i] = input[i];
        else
            out[i] = input[i].substring(0,1) + input[i].substring(0);
    }
    return out;
}

Then call the method as follows to replicate the example with characters “a”, “b”, and “c”:

String[] input = {"dog", "aardvark", "funny", "bbbrrr", "cat", "ccrazy", "ddonut"};
String[] firstChar = {"a", "b", "c"};
String[] out = echoChars(input, firstChar);
Select an assignment template