Object-Oriented Programming with Java, part I – Exercise 40

This program gets you to create three methods, one for white-spaces and one for stars. The third method combines the other two to create Christmas tree shapes.

My Code


public class PrintingLikeBoss {

    // copy or rewrite the method of Assignment 39.1 here
    public static void printStars(int amount) {
        while (amount > 0) {
            System.out.print("*");
            amount--;

        }
        System.out.println("");
    }

    public static void printWhitespaces(int amount) {
        // 40.1
        while (amount > 0) {
            System.out.print(" ");
            amount--;
        }
    }

    public static void printTriangle(int size) {
        // 40.2
        int star = 1;
        int tick = size;
        while (tick >= star) {
            printWhitespaces(size - 1);
            printStars(star);
            size--;
            star++;
        }
    }

    public static void xmasTree(int height) {
        // 40.3
        int stars = 1;
        while (height > 0) {
            printWhitespaces(height -1);
            printStars(stars);
            stars += 2;
            height--;
        }
        printWhitespaces(stars / 2 - 2);
        printStars(3);
        printWhitespaces(stars / 2 - 2);
        printStars(3);
    }

    public static void main(String[] args) {
        // Tests do not use main, yo can write code here freely!

        printTriangle(5);
        System.out.println("---");
        xmasTree(4);
        System.out.println("---");
        xmasTree(10);
    }
}

Model Code

public class PrintingLikeBoss {

 

    // copy or rewrite the method of Assignment 39.1 here

    public static void printStars(int amount) {

        int printed = 0;

        while (printed < amount) {

            System.out.print("*");

            printed = printed + 1;

        }

        System.out.println("");

    }

 

    public static void printWhitespaces(int amount) {

        // 40.1

        int i = 0;

        while (i < amount) {

            System.out.print(" ");

            i++;

        }

    }

 

    public static void printTriangle(int size) {

        // 40.2

        int i = 1;

        while (i <= size) {

            printWhitespaces(size - i);

            printStars(i);

            i++;

        }

    }

 

    public static void xmasTree(int height) {

        // 40.3

        int i = 1;

        while (i <= height) {

            printWhitespaces(height - i);

            printStars(i + (i - 1));

            i++;

        }

 

        // the stand:

        printWhitespaces(height - 2);

        printStars(3);

        printWhitespaces(height - 2);

        printStars(3);

    }

 

    public static void main(String[] args) {

        // Tests do not use main, yo can write code here freely!

 

        printTriangle(5);

        System.out.println("---");

        xmasTree(4);

        System.out.println("---");

        xmasTree(10);

    }

}

Comments

Mine is very messy compared to the model solution.

Leave a Reply

Your email address will not be published. Required fields are marked *