Search code examples
javanested-loops

Creating a Christmas Tree with proper spacing with the trunk and base under the leaves


I just can't seem to space the trunk and base of my christmas tree. The trunk and base are a fixed value and they have to be centered for any values given for the tree leaves.

I tried doing this

public class Proj01_xmasTree {
    //christmas time scott r portdog45680-41fasda
    public static void main(String args[]) {
        //int gives amount of lines to print
        int size = 4;
        int treeWidth = size * 2 - 1;
        tree1(size);
        tree2(size);
        tree3(size);
        trunk(1);
        base(1);
    }
    
    public static void tree1(int size){
        // "i" starts on different stars
        for (int i = 0; i < size; i++){
            //4 + 1 + 0 = 5. 4 + 1 - 1 = 4, etc..
            for (int j = 0; j < size + 1 - i; j++)
                System.out.print(" ");
                //stars 1,3,5,7. i*2 + 1. 0 + 1 = 1. 
                for (int k = 0; k < i*2 + 1; k++){
                    System.out.print("*");
            }
            System.out.println();
        }
    }
    
    public static void tree2(int size){
        //"i" starts on different stars
        for (int i = 1; i < size; i++){
            //4 + 1 + 0 = 5. 4 + 1 - 1 = 4, etc..
            for (int j = 0; j < size + 1 - i; j++)
                System.out.print(" ");
                //stars 1,3,5,7. i*2 + 1. 0 + 1 = 1. 
                for (int k = 0; k < i*2 + 1; k++){
                    System.out.print("*");
            }
            System.out.println();
        }
    }
    
    public static void tree3(int size){
        //"i" starts on different stars
        for (int i = 2; i <= size; i++){
            //4 + 1 + 0 = 5. 4 + 1 - 1 = 4, etc..
            for (int j = 0; j < size + 1 - i; j++)
                System.out.print(" ");
                //stars 5,7,9,12 
                for (int k = 0; k < i*2 + 1; k++){
                    System.out.print("*");
            }
            System.out.println();
        }
    }
    
    public static void trunk(int treeWidth){
        int spacing = (treeWidth - 2) / 2;
        
        for (int i = 0; i < 2; i++){
            for (int j = 0; j < spacing; j++){
                System.out.print(" ");
            }
            System.out.println("*");
        }
    }
    
    public static void base(int size){
        for (int i = 0; i < 1; i++){
            for (int j = 0; j < size/2; j++){
                System.out.print(" ");
            }
            for (int k = 0; k < 3; k++){
                System.out.print("*");
            }
        }
    }
}

Which outputs:

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

Desired output:

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

(note: the trunk and base are separate methods. they have to be fixed like that for any of the leaves, please keep that in mind.)


Solution

  • I've updated the trunk and base functions as follows (tree1, tree2 and tree3 functions are not changed).

    private static void trunk(int size) {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j <= size; j++) {
                System.out.print(" ");
            }
            System.out.println("*");
        }
    }
    
    private static void base(int size) {
        for (int j = 0; j < size; j++) {
            System.out.print(" ");
        }
        for (int k = 0; k < 3; k++) {
            System.out.print("*");
        }
    }
    

    For the trunk function, the number of initial spaces required is same as the value of size + 1 and for the base, it is size.

    Also now all functions work based only on the size parameter

    int size = 4;
    tree1(size);
    tree2(size);
    tree3(size);
    trunk(size);
    base(size);