Learn Java Coding

Everything You Have To Know About Java

Pyramids

In the previous page (loop from A to Z), you saw that you could true the characters. This page will explain how to create different models in Java by looping and printing characters. I will show in this tutorial how to print a pyramid of characters From “A” to “Z”.

The idea for this project is to get 5 á 6 methods, let’s say you want a pyramid of 15 lines:

java pyramid

  1. The first method will be your start method. You sketch a plan in the start method:
      1. you want a loop that goes from 1 – 15
      2. then you have to make a method to let Java/Eclipse know what you want per line. It is not necessary to make a new method but I will do that because you have a better structure and view. We call the (optional) method: printingPerLine. Now, you start method will look like this:
      1. void start() {
        	for (int i = 0; i < NUMBER_OF_ROWS; i++) {
        		printingPerLine(i);
        	}
        }

    You see that your start method contains the loop (for-loop), you loop through every line and then the method printingPerLine will plan what to do every line.

  1. The second method printingPerLine has to contain 4 elements/methods per row
    1. Calculate the number of spaces
    2. Calculate the number of letters
    3. Compute the letter
    4. Printing method
      void printingPerLine (int i) {
      	//print a number of spaces per line
      	//print a number of letters per line
      	//after a line put a enter (\n), to go the next line
      }
  2. For the method printingPerLine, first, we have to know, how the print method is working. We want a loop that print the desired characters.
    void printMethod(int number, char character) {
    	for (int i = 1; i <= number; i++) {
    		out.printf("%c", character);
    	}
    }

    So, we got as input a character and the number of times the character has to be print. (As you can see, the loop will play the number of times the character has to be printed.)

    Now we can create the printingPerLine method. Like I mentioned before, we will first write the line, in which we print the number of spaces. The second line will be the number of letters per line. We also create a line which sets us to the next row ( out.print(“/n”) ).

    void printingPerLine (int i) {
    	printMethod(calculateNumberOfSpaces(i), SPACE);
    	printMethod(calculateNumberOfLetters(i), computeLetter(i));
    	out.printf("\n");
    }
  3. The fourth method, will be the method in which you calculate how many spaces you got in one line and return the number of spaces.
    int calculateNumberOfSpaces(int i) {
    	return NUMBER_OF_ROWS - i;
    }
  4. The fifth method will be the method in which you calculate how many letters you got in one line and return the number of letters.
    int calculateNumberOfLetters(int i) {
    	return 2 * i + 1;
    }
  5. The sixth method will be the method in which you calculate how many letters you got in one line and compute the letter you want to print.
    char computeLetter(int i) {
    	return (char) ('A' + i);
    }

When you delete the ” + i”, the method will only print A. And you will get this result:

pyramid java

The return line, returns the values to the method printingPerLine. Then it will be sent to the method which prints everything. The result is the first pyramid.

The whole program is:

package Pyramid;

import java.io.PrintStream;

class Pyramid {
	static final int NUMBER_OF_ROWS = 15;
	static final char SPACE = ' ';
	
	PrintStream out;

	Pyramid() {
		out = new PrintStream(System.out);
	}

	void start() {
		for (int i = 0; i < NUMBER_OF_ROWS; i++) {
			printingPerLine(i);
		}
	}

	void printingPerLine (int i) {
		printMethod(calculateNumberOfSpaces(i), SPACE);
		printMethod(calculateNumberOfLetters(i), computeLetter(i));
		out.printf("\n");
	}
	
	int calculateNumberOfSpaces(int i) {
		return NUMBER_OF_ROWS - i;
	}

	int calculateNumberOfLetters(int i) {
		return 2 * i + 1;
	}
	
	char computeLetter(int i) {
		return (char) ('A' + i);
	}

	void printMethod(int number, char character) {
		for (int i = 1; i <= number; i++) {
			out.printf("%c", character);
		}
	}

	public static void main(String[] argv) {
		new Pyramid().start();
	}
}

output:

java pyramid

Next Post

Previous Post

© 2024 Learn Java Coding