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 draw a Diamond?

+8 votes
asked Dec 10, 2020 by THOMAS GUERRA (280 points)
reshown Dec 10, 2020 by THOMAS GUERRA

import java.util.Scanner;

public class Main 
{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int x = 0; // input height
        
        int choice;
        
        System.out.println("1. Trapezoid ");
        System.out.println("2. Diamaond ");
        System.out.println("3. Hollow Triangle ");
        System.out.println("What kind of shape do you want to see? ");
        choice = input.nextInt(); // user chooses shape
        
        if(choice==1){
            System.out.print("Enter height: ");
            x = input.nextInt(); //user inputs height
            
            drawTrapezoid(x);
        } else if(choice == 2){
            System.out.print("Enter height: ");
            x = input.nextInt(); //user inputs height
            drawDiamond(x);
        } else if(choice == 3){
            System.out.print("Enter height: ");
            x = input.nextInt(); //user inputs height
            //drawHollowTriangle(x,mark);
        }
        
    }
    

    //Diamond
    public static void drawbDiamond(int length){
        for(int j = 1; j<=length; j++){
            System.out.print('x');
            
        }
    }
    public static void drawbDiamond(int length, char mark){
        for(int j = 1; j<=length; j++){
            System.out.print(mark);
            
        }
    }
    public static void drawbDiamondln(int length){
        drawbDiamond(length);
        System.out.println();
    }
    public static void drawbDiamondln(int length, char mark){
        drawbDiamond(length,mark);
        System.out.println();
    }
    public static void drawDiamond(int x){
        int dotspace = x - 1;
        int xstars = 1;
        do{ //first half of diamond
            drawbDiamond(dotspace,'.');
            drawbDiamondln(xstars);
            dotspace--;
            xstars+=2;
        } while(dotspace>=0);
       
        do{ 
            drawbDiamond(dotspace, '.');
            drawbDiamondln(xstars);
            dotspace++;
            xstars-=2;
        } while(dotspace<=x);
        
    }

    
    
    // TRAPEZOID
    public static void drawbTrapezoid(int length){
        for(int i = 1; i<=length; i++){ 
            System.out.print('x');
        }
        
    }
    public static void drawbTrapezoid(int length, char mark){
        for(int i = 1; i<=length; i++){ 
            System.out.print(mark);
        }
    }
    public static void drawbTrapezoidln(int length){
        drawbTrapezoid(length);
        System.out.println();
    }
    public static void drawbTrapezoidln(int length, char mark){
        for(int i = 1; i<=length; i++){ 
            drawbTrapezoid(length,mark);
        }
    }
    public static void drawTrapezoid(int x){
        
        int dotspace = x - 1;
        int xstars = 2 * x - 1;
        do{
           drawbTrapezoid(dotspace, '.');
           drawbTrapezoidln(xstars);
           dotspace--;
           xstars+=2;
        } while(dotspace>=0);
    }
    
}

1 Answer

+1 vote
answered Dec 15, 2020 by Peter Minarik (22,620 points)

Here is my solution.

The idea is that there are 2 different diamonds: one with the height being even and one with odd. The odd is the "classic one" with pointy edges. The even comes with blunt edges for symmetry.

import java.util.Scanner;

public class Main 
{
    private static final char empty = ' ';
    private static final char mark = '*';
    
    private static boolean validate(int number, int min, int max)
    {
        if (number < min || number > max)
        {
            System.out.println("Invalid value entered. It must be between " + min + " and " + max);
            return false;
        }
        
        return true;
    }

    private static void drawTrapezoid(int height)
    {
        int baseWidth = 50;
        while (--height >= 0)
        {
            for (int i = 0; i < height; i++)
                System.out.print(empty);

            int width = baseWidth - height * 2;
            for (int i = 0; i < width; i++)
                System.out.print(mark);

            System.out.println();
        }
    }
    
    private static void drawDiamondSlice(int halfHeight, int halfSliceIndex, int parityMinusOne)
    {
        int emptyWidth = halfHeight - halfSliceIndex + parityMinusOne;
        for (int j = 0; j < emptyWidth; j++)
            System.out.print(empty);
        
        int width = 1 + halfSliceIndex * 2 - parityMinusOne;
        for (int j = 0; j < width; j++)
            System.out.print(mark);

        System.out.println();
    }
    
    private static void drawDiamond(int height)
    {
        int parityMinusOne = height % 2 - 1;
        int halfHeight = height / 2;

        // Upper half
        for (int i = 0; i < halfHeight; i++)
            drawDiamondSlice(halfHeight, i, parityMinusOne);

        // Lower half
        for (int i = halfHeight + parityMinusOne; i >= 0; i--)
            drawDiamondSlice(halfHeight, i, parityMinusOne);
    }

    private static void drawHollowTriangle(int height)
    {
        // The tip
        for (int i = 1; i < height; i++)
            System.out.print(empty);

        System.out.println(mark);
        
        // Slices
        for (int i = 2; i < height; i++)
        {
            for (int j = 0; j < height - i; j++)
                System.out.print(empty);
                
            System.out.print(mark);
                
            for (int j = 0; j < (i - 1) * 2 - 1; j++)
                System.out.print(empty);

            System.out.println(mark);
        }
        
        // The base
        if (height > 1)
        {
            int baseWidth = height * 2 - 1;
            for (int i = 0; i < baseWidth; i++)
                System.out.print(mark);
            
            System.out.println();
        }
    }
    
    public static void main(String[] args)
    {
        System.out.println("1. Trapezoid");
        System.out.println("2. Diamaond");
        System.out.println("3. Hollow Triangle");
        System.out.println("What kind of shape do you want to see? ");

        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();   // User chooses shape
        
        if (!validate(choice, 1, 3))
        {
            System.out.println("Exiting...");
            return;
        }
        
        System.out.print("Enter the height: ");
        int height = input.nextInt();
        if (!validate(height, 1, 20))
        {
            System.out.println("Exiting...");
            return;
        }
        
        switch (choice)
        {
            case 1: drawTrapezoid(height); break;
            case 2: drawDiamond(height); break;
            case 3: drawHollowTriangle(height); break;
            default: return;    // We have actually already handled this above when we validate the choice.
        }
    }
}
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.
...