Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty diamond shape with numbers

Tags:

java

ascii-art

So I have been asked this question and I could only solve the top part of the code, I am stuck on the bottom part.

Write a Java program called EmptyDiamond.java that contains a method that takes an integer n and prints a empty rhombus on 2n − 1 lines as shown below. Sample output where n = 3:

  1
 2 2
3   3
 2 2
  1

Here's my code so far:

public static void shape(int n) {
    //TOP PART
    for (int i = 1; i <= (n - 1); i++) {
        System.out.print(" ");
    }
    System.out.println(1);
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j <= (n - i); j++) {
            System.out.print(" ");
        }
        System.out.print(i);
        for (int j = 1; j <= 2 * i - n + 1; j++) {
            System.out.print(" ");
        }
        System.out.println(i);
    }

    //BOTTOM PART (The messed up part)
    for (int i = n + 1; i <= 2 * n - 2; i++) {
        for (int j = 1; j <= n - i; j++) {
            System.out.print(" ");
        }
        System.out.print(i);
        for (int j = 1; j <= n; j++) {
            System.out.print(" ");
        }
        System.out.print(i);
    }
    for (int i = 1; i <= (n - 1); i++) {
        System.out.print(" ");
    }
    System.out.println(1);
}
public static void main(String[] args) {
    shape(4);
}
like image 243
Marwan N Avatar asked Jun 19 '17 23:06

Marwan N


People also ask

What is a hollow diamond?

In the hollow diamond pattern, the first and last row contains only a star, and the rest of the rows contains two stars.


2 Answers

Maybe a little bit late, but because the bottom part of your message is just the first part mirrored you can use a Stack to print the message in reverse order:

public static void main(String[] args) {
    int maxNumber = 3;
    Stack<String> message = new Stack<>();
    // upper part
    for (int row = 0; row < maxNumber; row++) {
        int prefix = maxNumber - (row + 1);
        int spaces = row >= 2 ? row * 2 - 1 : row;

        String line = getLine(row, prefix, spaces);
        System.out.println(line);
        if (row != maxNumber - 1)
            message.add(line);
    }
    // bottom part
    while (!message.isEmpty())
        System.out.println(message.pop());
}

public static String getLine(int row, int prefix, int spaces) {
    StringBuilder line = new StringBuilder("_".repeat(prefix));
    line.append(row + 1);
    if (row != 0) {
        line.append("_".repeat(spaces));
        line.append(row + 1);
    }
    return line.toString();
}

Output:

__1
_2_2
3___3
_2_2
__1

You can of course use any method you want to fill the stack (i.e. to generate the upper part of your message) like with the method this question suggessted. This upper part I describe contains the first line (inclusive) up to the middle line (exclusive).

like image 163
Sebphil Avatar answered Nov 15 '22 05:11

Sebphil


Here is the program for printing empty diamond:

int n = 3; //change the value of n to increase the size of diamond
int upperCount = 1;
for (int i = n; i >= 1; i--) {
    for (int j = i; j >= 1; j--) {
        System.out.print(" ");
    }
    System.out.print(upperCount);
    for (int j = 0; j <= upperCount - 2; j++) {
        System.out.print(" ");
    }
    for (int j = 0; j <= upperCount - 2; j++) {
        System.out.print(" ");
    }
    if (upperCount != 1) {
        System.out.print(upperCount);
    }
    upperCount++;
    System.out.print("\n");
}

int lowerCount = n - 1;
for (int i = 1; i <= n - 1; i++) {
    for (int j = 0; j <= i; j++) {
        System.out.print(" ");
    }
    System.out.print(lowerCount);
    for (int j = 0; j <= lowerCount - 2; j++) {
        System.out.print(" ");
    }
    for (int j = 0; j <= lowerCount - 2; j++) {
        System.out.print(" ");
    }
    if (lowerCount != 1) {
        System.out.print(lowerCount);
    }
    lowerCount--;
    System.out.print("\n");
}

Do following changes in the Bottom Part of your code:

int lowerCount = n - 1;
for (int i = n - 1; i >= 2; i--) {
    for (int j = 1; j <= (n - i); j++) {
        System.out.print(" ");
    }
    System.out.print(i);

    for (int j = 1; j <= lowerCount; j++) {
        System.out.print(" ");
    }
    System.out.print(i);
    lowerCount -= 2;
}
like image 28
Mohit Tyagi Avatar answered Nov 15 '22 05:11

Mohit Tyagi