1 / 5100%
CSE 110: Principles of Programming Languages
Assignment 4
Overview
In this assignment you will write a program that will use nested loops to print either a pyramid or
a diamond of *’s of a given size. It will ask the user for the height, and then if they want a
diamond pattern instead. If the user want to do the diamond, the program prints out an upside
down pyramid under the first to make the diamond shape. For example:
OR
Requirements
Your program must do the following in order to receive full credit on this assignment.
1. Ask the user to provide the height of the pyramid.
a. This must be at least 1 and no greater than 25.
2. Store the height that the user has provided.
3. Verify that the user has typed in a valid height.
a. If they have not, accept new values from the user until they enter a valid height.
4. Ask the user if they would like a diamond instead of a pyramid.
a. The user must type “Yes” or “No”.
b. If the user types neither of these, ask again until they provide appropriate input.
5. Set a boolean to store whether they want a pyramid or a diamond.
a. Print out a message to show the user which you will be printing.
6. Start a main loop to print out a pyramid. This will need to run once for every line of the
pyramid. Note that you will need to do this whether they asked for a diamond or not (see
below).
a. Add the appropriate number of preceding spaces for the current line.
i. For example, on the first line this will be height - 1.
b. Add the appropriate number of *’s for that line.
i. On the first line, this would be 1 * which is height - 1 spaces away from
the left edge, which will be the start of the middle column.
ii. Each additional line will need one more * in front of the middle column,
and one more after the column.
1. So, the second line would have 3 *’s.
iii. The last line will have ((height * 2) - 1) *’s on it.
7. Now that the pyramid has been printed, print the bottom half if the user asked for you to
print a diamond.
a. Again, you need an outer loop.
b. This time you will need one fewer *’s per line before and after the column until
you end with just 1 * on the last line.
i. The opposite goes for preceding spaces, in that you need more per line.
ii. This will all be almost the same logic, just in reverse.
iii. Make sure you don’t print the last line of the pyramid a second time when
making a diamond.
Example Inputs
Below are five example runs of the program with the inputs and outputs. Remember, the graders
will be testing your program against these as well as their own, so make sure you test these and
come up with your own before submitting your program.
#1
#2
#3
#4
#5
Submission
Please submit your Assignment4.java file to the Assignment 4 link on Blackboard under the
Assignments tab. You may submit as many times as you want prior to the due date, in case you
later find and fix an error, but only the last one is graded.
Students also viewed