Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELP IN JAVA: Write a program that draws an inverted pyramid of Xs of any given height, like this for height 4: //// //// ///

HELP IN JAVA: Write a program that draws an inverted pyramid of Xs of any given height, like this for height 4:
////
////
///
///
//
//
/
/
Note that each row of Xs takes two lines in the output. Each X is made of two slashes and two backslashes.
There are no spaces on the first row.
The program must use a recursive method to draw the pyramid.
To complete this assignment, follow these steps:
1. Create a main method that calls your recursive method.
2. Create the recursive method. Every recursive method has two parts: the base case and the recursive case.
a. In this problem, the base case is where the number of Xs to print is 0. We dont need to do anything in this case.
b. The recursive case is when the number of X is greater than 0. Use an if statement to check. The recursive case has to do two things: Print one row of the pyramid, and make a recursive call.
3. To print the top row of the pyramid:
a. We need to print two lines. Each line a sequence of spaces before the backslash or slash.
b. Use a for loop to print the spaces.
c. Use another for loop to print the backslashes and slashes.
d. Use a for loop to print the spaces for the bottom line of this row.
e. Use another for loop to print the slashes and backslashes.
4. Make a recursive call to print a smaller pyramid:
a. If you think about building the pyramid from the top, notice that as you go down the pyramid, there is a decreasing number of slashes, but an increasing number of spaces to the left of the line.
b. Use two parameters for the recursive method: one that holds the number of spaces to print before the X, and one that holds the number of Xs to print.
c. The recursive call should subtract 1 from the number of Xs, but it should add 1 to the number of spaces.
5. Finally, in your main method, call the recursive method with initially 0 spaces and height Xs.
6. Test the program with height value 4, 5, and anything else you want to try.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions