Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a new Java class called: NumberPyramid Write a program that will print a

ID: 3602515 • Letter: C

Question

Create a new Java class called: NumberPyramid Write a program that will print a pyramid of integers to the screen The program should begin by asking the user for the number of lines to be printed. The user is expected to enter a number from 1 to 9. As long as the user does not enter a number from 1 to 9, the program should continue asking for a number from 1 to 9 Once a correct number has been entered, the program should print a pyramid of integers as shown in the examples below rogram runs. The The following is an example of what your MIGHT see on the screen when your p exact output depends on what values that the user types in while the program runs. The user's inputted values are shown below in italics Enter the number of lines (1-9) 7 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 45 6 5 4 3 2 1 2 3 4 5 6 7 65 43 2 1 2 3 45 6 7 Here is another example run Enter the number of lines (1-9) 0 Enter the number of lines (1-9) 10 Enter the number of lines (1-9) 4 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 Technical Notes & Hints You will need to use several loops to complete this program some of them are nested Use a loop to keep asking the user to enter the number of lines, until a correct number has been entered .Use a loop to count through the number of lines to be printed. Inside this loop, use several nested loops to o Print the correct number of spaces at the beginning of the line o Print the numbers starting at the current line number and counting down to 1 o Print the numbers from 1, and counting back up to the current line number

Explanation / Answer

//NumberPyramid.java

import java.util.Scanner;

public class NumberPyramid {

      public static void main(String[] args) {

            showMenu(); /*showing the prompt*/

      }

      public static void showMenu() {

            Scanner scanner = new Scanner(System.in);

            try {

                  System.out.println("Enter the number of lines (1-9): ");

                  int choice = Integer.parseInt(scanner.nextLine());

                  if (choice < 1 || choice > 9) {

                        throw new Exception();

                  } else {

                        displayPyramid(choice); /*create and display the pyramid*/

                  }

            } catch (Exception e) {

                  showMenu();

                  /**

                  * recalling the function in case of wrong values

                  */

            }

      }

      public static void displayPyramid(int n) {

            for (int i = 1; i <= n; i++) {

                  /**

                  * loop from 1 to n

                  */

                  int space = n - i;

                  for (int k = 0; k < space; k++) {

                        /**

                        * for printing the white spaces before the numbers

                        */

                        System.out.printf("%2s", " ");

                  }

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

                        /**

                        * for printing the left half of the pyramid

                        */

                        System.out.print(j + " ");

                  }

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

                        /**

                        * for printing the right half of the pyramid

                        */

                        System.out.print(j + " ");

                  }

                  System.out.println();

            }

      }

}

//Output

Enter the number of lines (1-9):

0

Enter the number of lines (1-9):

10

Enter the number of lines (1-9):

9

                1

              2 1 2

            3 2 1 2 3

          4 3 2 1 2 3 4

        5 4 3 2 1 2 3 4 5

      6 5 4 3 2 1 2 3 4 5 6

    7 6 5 4 3 2 1 2 3 4 5 6 7

8 7 6 5 4 3 2 1 2 3 4 5 6 7 8

9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote