Limited access

Upgrade to access all content for this subject

A programmer has created the following method to print a string of characters in a specific way:

public static void printChars(String str, int num)
{
    if(num%2 == 0)
    {
        for (int i=1; i<=num; i++)
        {
            System.out.print(str + " ");
        }
        System.out.println("");;
        for (int i=1; i<=num; i++)
        {
            System.out.print(str + str.substring(0,1) + " ");
        }
        System.out.println("");;
    }
    else
    {
        for (int i=1; i<=num+1; i++)
        {
            System.out.print(str + str.substring(0,1) + " ");
        }
        System.out.println("");;
        for (int i=1; i<=num+1; i++)
        {
            System.out.print(str + str.substring(0,2) + " ");
        }
        System.out.println("");;
    }
}

For example, printChars("AB", 3); will print to the console:
ABA ABA ABA ABA
ABAB ABAB ABAB ABAB

and printChars("XY", 4); will print to the console:
XY XY XY XY
XYX XYX XYX XYX

However, the programmer realizes that she should have used functional decomposition to divide this method into sub-functions.

Which of the following implementations would print the same characters to the console as her original code?

A
public static void printChars(String str, int num)
{
    if(num%2 == 0)
    {
        printLine(str, num);
        printLine(str + str.substring(0,1), num+1);
    }
    else
    {
        printLine(str + str.substring(0,1), num);
        printLine(str + str.substring(0,2), num+1);
    }
}

public static void printLine(String str, int num)
{
    for (int i=1; i<=num; i++)
    {
        System.out.print(str + " ");
    }
    System.out.println("");        
}
B
public static void printChars(String str, int num)
{
    if(num%2 == 0)
    {
        printLine(str, num);
        printLine(str.substring(0,1), num);
    }
    else
    {
        printLine(str.substring(0,1), num+1);
        printLine(str.substring(0,2), num+1);
    }
}

public static void printLine(String str, int num)
{
    for (int i=1; i<=num; i++)
    {
        System.out.print(str + " ");
    }
    System.out.println("");        
}
C
public static void printChars(String str, int num)
{
    if(num%2 == 0)
    {
        printLine(str, num);
        printLine(str + str.substring(0,1), num);
    }
    else
    {
        printLine(str + str.substring(0,1), num+1);
        printLine(str + str.substring(0,2), num+1);
    }
}

public static void printLine(String str, int num)
{
    for (int i=1; i<=num+1; i++)
    {
        System.out.print(str + " ");
    }
    System.out.println("");        
}
D
public static void printChars(String str, int num)
{
    if(num%2 == 0)
    {
        printLine(str, num);
        printLine(str + str.substring(0,1), num);
    }
    else
    {
        printLine(str + str.substring(0,1), num+1);
        printLine(str + str.substring(0,2), num+1);
    }
}

public static void printLine(String str, int num)
{
    for (int i=1; i<=num; i++)
    {
        System.out.print(str + " ");
    }
    System.out.println("");        
}
E
public static void printChars(String str, int num)
{
    if(num%2 == 0)
    {
        printLine(str, num);
        printLine(str + str.substring(0,1), num+1);
    }
    else
    {
        printLine(str + str.substring(0,1), num);
        printLine(str + str.substring(0,2), num+1);
    }
}

public static void printLine(String str, int num)
{
    for (int i=1; i<=num; i++)
    {
        System.out.print(str + str.substring(0,1) + " ");
    }
    System.out.println("");        
}
Select an assignment template