Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can I fix the pattern "Star of David"?

+6 votes
asked Mar 28, 2022 by Lexus Guevara (1,010 points)

The lower part of the pattern is not yet fixed. I tried to reverse the code but still didn't fix the pattern.
The minimum value for this pattern is 7. Also, I am using only Odd numbers.

//the bold texts are the part that needs to be fixed

Thank you!

import java.util.Scanner;

public class MidEx{

public static void main(String args[]){

int num, num1, i, I, j, J, k, K;

Scanner input = new Scanner(System.in);

System.out.print("Input number (must be Odd and 7 is the lowest number): ");

num = input.nextInt();

System.out.println("STAR OF DAVID");

for(i=1;i<num-5;i=i+2) {

for(I=num-i;I>1;I=I-2) {

System.out.print("_ ");

}

for(j=1;j<=i;j++) {

System.out.print("* "); 

}

for(I=num-i;I>1;I=I-2) {

System.out.print("_ ");

}

System.out.println(""); 

}

num1=num;

i=1;

k=i;

for(i=num-2;i<=num;i=i+2){

for(j=1;j<k;j++){

System.out.print("_ ");

}

for(I=num1;I>=1;I--){

System.out.print("* ");

}

for(j=1;j<k;j++){

System.out.print("_ ");

}

System.out.println("");

k++;

num1=num1-2;

}

//2nd full line

for(i=1;i<=num;i++){

System.out.print("* ");

}

System.out.println("");

num1=num;

for(i=num1;i>=1;i=i-2) {

for(I=num-i;I>1;I=I-2) {

System.out.print("_ ");

}

for(j=i;j>=1;j--) {

System.out.print("* "); 

}

for(I=num-i;I>1;I=I-2) {

System.out.print("_ ");

}

System.out.println(""); 

}

}

}

1 Answer

0 votes
answered Mar 29, 2022 by Peter Minarik (86,040 points)
edited Apr 1, 2022 by Peter Minarik

Your code is hard to read. i, I, j, J, k, K, num, num1, etc... they don't make much sense, do they? You should name variables in a way that they convey some meaning. You'll look at your code months later and even you will forget what they mean. You should use variable names like row or column, etc.

Let me share my solution to the problem so you can take away anything from it that is useful for you:

import java.util.Scanner;

public class Main
{
    private static final char FULL = '*';
    private static final char EMPTY = ' ';
    
    public static void main(String args[])
    {
        System.out.print("Height? ");
        int height = new Scanner(System.in).nextInt();
        int width = height + 1; // +1 to make it look symmetric
        // Let's divide the star into 4 equal segments --> Hence the star looks best if height is a whole multiple of 4
        int top = 0;
        int middleTop = height / 4;
        int middleBottom = height / 2;
        int bottom = height * 3 / 4;

        // Let's start rendering every row from 0 to height
        int row = 0;
        //
        // top
        //
        int start = width / 2;
        int end = start;
        while (row < middleTop)
        {
            for (int column = 0; column < width; column++)
                System.out.print((start <= column && column <= end) ? FULL : EMPTY);
            start--;
            end++;
            System.out.println();
            row++;
        }
        //
        // middle top
        //
        start = 0;
        end = width - 1;
        while (row < middleBottom)
        {
            for (int column = 0; column < width; column++)
                System.out.print((start <= column && column <= end) ? FULL : EMPTY);
            start++;
            end--;
            System.out.println();
            row++;
        }
        start--;
        end++;
        //
        // middle bottom
        //
        while (row < bottom)
        {
            for (int column = 0; column < width; column++)
                System.out.print((start <= column && column <= end) ? FULL : EMPTY);
            start--;
            end++;
            System.out.println();
            row++;
        }
        //
        // bottom
        //
        int triangleWidth = height - bottom - 1; 
        start = width / 2 -  triangleWidth;
        end = width / 2 + triangleWidth;
        while (row < height)
        {
            for (int column = 0; column < width; column++)
                System.out.print((start <= column && column <= end) ? FULL : EMPTY);
            start++;
            end--;
            System.out.println();
            row++;
        }
    }
}

Notes

A Simple Solution

The above is a very crude solution to cover the entire area of David's Star. As comments say, it looks best if the height of the David's Star is a whole multiple of 4. It works though and should be easy to understand.

A Better Solution

A more flexible solution would be to write your own ASCII wireframe renderer. Similar to a graphics renderer, but you only do it via characters. You could define points ((x, y) coordinates) and connect them with a line. The renderer would go one by one across all the characters per row and column and check if they align with the line. If they do, put a mark there, otherwise, leave it empty.

This way, you could draw your David's Star (or any 2D wireframe shape) in a few lines of code:

import java.util.Scanner;

public class Main
{
    private static final char FULL = '#';
    private static final char EMPTY = ' ';
    private static final int preferredHeight = 14;
    private static final int preferredWidth = 12;

    public static void main(String args[])
    {
        System.out.print("Height? ");
        int height = new Scanner(System.in).nextInt();
        
        float scaleY = (float)height / preferredHeight;
        float scaleX = scaleY;
        
        Renderer renderer = new Renderer();
        renderer.Add(new Triangle(new Point(0, 4), new Point(12, 4), new Point(6, 14)).Scale(scaleX, scaleY));
        renderer.Add(new Triangle(new Point(0, 10), new Point(12, 10), new Point(6, 0)).Scale(scaleX, scaleY));
        renderer.Render(new Point(0, 0), new Point(preferredWidth, preferredHeight).Scale(scaleX, scaleY), FULL, EMPTY);
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...