HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Building Better Boxes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
boxesbuildingbetter

Problem

I'm working with parameters right now in Java, and my colleague told me that this code of mine is redundant, and I see his point. The for loops do look redundant, but I have no idea how to go about changing this. My code is functional, but I wish to improve it.

public static void printLine(int length) 
{
    for (int i = 1; i <= length; i++) 
    {
        System.out.print("*");
    }       
    System.out.println();
}

public static void printBox(int length, int height) 
{
    printLine(length);

    for (int i = 1; i <= height - 2; i++) 
    {
    System.out.print("*");     
        for (int j = 1; j <= length - 2; j++) 
        {
            System.out.print(" ");
        }
        System.out.println("*");
    } 
    printLine(length);     
}

Solution

Your friend is right, and there's ways to parameterize the code to make it more useful. Consider three things:

  • Don't print each character - build a block of text up, and print the block (note that adding a newline to the string \n works as well as a System.out.println())



  • You can input the text to repeat as a parameter to the function...



  • It is common practice to have loops from 0 to 1-less-than-the-limit like for (int i = 0; i



So, take your function:

public static void printLine(int length) 
{
    for (int i = 1; i <= length; i++) 
    {
        System.out.print("*");
    }       
    System.out.println();
}


And apply the three concepts above... and we get:

public static String repeatText(String text, int count) {
    StringBuilder output = new StringBuilder();
    for (int i = 0; i < count; i++) {
        output.append(text);
    }       
    return output.toString();
}


Now, that function can be used as the basis for a bunch of things....

  • your top-and-bottom lines of the box.



  • the inside-content of the other lines of the box



  • the repeating rows inside the box



Consider your printBox function:

public static void printBox(int length, int height) 
{
    printLine(length);

    for (int i = 1; i <= height - 2; i++) 
    {
    System.out.print("*");     
        for (int j = 1; j <= length - 2; j++) 
        {
            System.out.print(" ");
        }
        System.out.println("*");
    } 
    printLine(length);     
}


Let's use the same 3 principles I listed earlier, but let's also use the new
repeatText` function....

public static String buildBox(int length, int height)  {
    String topAndBottom = repeatText("*", length) + "\n";
    String middle = "*" + repeatText(" ", length - 2) + "*\n";
    String box = topAndBottom + repeatText(middle, height - 2) + topAndBottom;
    return box;
}


Now, you can print that box just once with:

System.out.print(buildBox(length, height));


There are still issues, like what happens if the length or height are less than 2? That would be a problem. Consider ways to avoid that, or handle that.

Code Snippets

public static void printLine(int length) 
{
    for (int i = 1; i <= length; i++) 
    {
        System.out.print("*");
    }       
    System.out.println();
}
public static String repeatText(String text, int count) {
    StringBuilder output = new StringBuilder();
    for (int i = 0; i < count; i++) {
        output.append(text);
    }       
    return output.toString();
}
public static void printBox(int length, int height) 
{
    printLine(length);

    for (int i = 1; i <= height - 2; i++) 
    {
    System.out.print("*");     
        for (int j = 1; j <= length - 2; j++) 
        {
            System.out.print(" ");
        }
        System.out.println("*");
    } 
    printLine(length);     
}
public static String buildBox(int length, int height)  {
    String topAndBottom = repeatText("*", length) + "\n";
    String middle = "*" + repeatText(" ", length - 2) + "*\n";
    String box = topAndBottom + repeatText(middle, height - 2) + topAndBottom;
    return box;
}
System.out.print(buildBox(length, height));

Context

StackExchange Code Review Q#143168, answer score: 5

Revisions (0)

No revisions yet.