Z-Dog
Z-Dog

Reputation: 181

Print shapes on the same line

I am trying to print a square and a triangle on the same line as each other like so:

* * * * *             *  
* * * * *            * *  
* * * * *           * * *
* * * * *          * * * *

I have already created the functions to make them individually

public static void drawTriangle(int n) {
    int k = 2 * n - 5;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < k; j++) {
            System.out.print(" ");
        }
        k = k - 1;
        for (int j = 0; j <= i; j++) {
            System.out.print("* ");
        }
        System.out.println();
    }
}

public static void drawSquare(int width, int height) {
    for (int i = 0; i < height; i++) {
        System.out.print("* ");
    }
    System.out.println();
    for (int i = 0; i < width - 2; i++) {
        System.out.print("* ");
        for (int j = 0; j < height - 2; j++) {
            System.out.print("* ");
        }
        System.out.println("* ");
    }
    for (int i = 0; i < height; i++) {
        System.out.print("* ");
    }
    System.out.println();
}

But I don't know how I can combine both outputs on the same line.

Upvotes: 1

Views: 939

Answers (2)

Zabuzard
Zabuzard

Reputation: 25903

Explanation

Modify your methods so that they don't print but build a List<String> instead. After that you can easily combine both outputs into one.

The big advantage is that this approach is way more modular. You could combine it in any way or with any other methods.

Here is how you would use it:

List<String> squareLines = drawSquare(4, 4);
List<String> triangleLines = drawTriangle(4);

// Iterate both lists
Iterator<String> squareLineIter = squareLines.iterator();
Iterator<String> triangleLineIter = triangleLines.iterator();
while (squareLineIter.hasNext() && triangleLineIter.hasNext()) {
    String squareLine = squareLineIter.next();
    String triangleLine = triangleLineIter.next();

    // Print the combined line
    System.out.println(squareLine + "    " + triangleLine);
}

Note that this leaves out remaining lines of a longer list. You could easily support different sizes by filling the smaller list with empty lines.


Modifications

Therefore, modify your methods to build and return the lists.

Create the result, initially empty using

List<String> lines = new ArrayList<>();

Build a line using a StringBuilder

StringBuilder lineBuilder = new StringBuilder();
lineBuilder.append("* "); // Repeat that in a loop
String line = lineBuilder.toString();

Append the line to your lines using

lines.add(line);

Here are your fully modified methods:

public static List<String> drawTriangle(int n) {
    // Build the lines
    List<String> lines = new ArrayList<>();

    int k = 2 * n - 5;
    for (int i = 0; i < n; i++) {
        // Build the current line
        StringBuilder lineBuilder = new StringBuilder();
        for (int j = 0; j < k; j++) {
            lineBuilder.append(" ");
        }
        k = k - 1;
        for (int j = 0; j <= i; j++) {
            lineBuilder.append("* ");
        }
        // Add the line
        lines.add(lineBuilder.toString());
    }
    return lines;
}

public static List<String> drawSquare(int width, int height) {
    // Build the lines
    List<String> lines = new ArrayList<>();

    // Build a line
    StringBuilder lineBuilder = new StringBuilder();
    for (int i = 0; i < height; i++) {
        lineBuilder.append("* ");
    }
    // Add the line
    lines.add(lineBuilder.toString());

    for (int i = 0; i < width - 2; i++) {
        // Build the current line
        lineBuilder = new StringBuilder();
        lineBuilder.append("* ");
        for (int j = 0; j < height - 2; j++) {
            lineBuilder.append("* ");
        }
        lineBuilder.append("* ");
        // Add the line
        lines.add(lineBuilder.toString());
    }

    // Build a line
    lineBuilder = new StringBuilder();
    for (int i = 0; i < height; i++) {
        lineBuilder.append("* ");
    }
    // Add the line
    lines.add(lineBuilder.toString());

    return lines;
}

Upvotes: 2

enlighten_me
enlighten_me

Reputation: 180

Solution

The easiest solution is to combine the 2 methods into 1, as seen below:

public static void drawTriangleAndSquare(int widthS, int heightS) {
    // number of leading spaces in front of triangle
    int k = 2 * heightS - 5;

    // print square
    for (int i = 0; i < heightS; i++) {
        for (int j = 0; j < widthS; j++) {
            System.out.print("* ");
        }

        // print triangle
        System.out.print("\t");
        for (int j = 0; j < k; j++) {
            System.out.print(" ");
        }

        k--;

        for (int j = 0; j <= i; j++) {
            System.out.print("* ");
        }

        System.out.println();
    }
}

public static void main(String[] args) {
    drawTriangleAndSquare(5, 4);
}

Output

* * * * *      * 
* * * * *     * * 
* * * * *    * * * 
* * * * *   * * * * 

Note that I have cleaned up your code a little, especially the drawSquare() function.


Limitation

My implementation only allows you to print square and triangle of the same height (both of them are dependent on the variable heightS in drawTriangleAndSquare()).

P.S. if you want more whitespace between the triangle and the square, simply add more \ts at System.out.print("\t").

Upvotes: 3

Related Questions