Question: I have done all the work in JAVA Blue J Your program must do the following in order to receive full credit on this assignment.
I have done all the work in JAVA Blue J
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.
Store the height that the user has provided.
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.
Ask the user if they would like a diamond instead of a pyramid.
The user must type Yes or No.
If the user types neither of these, ask again until they provide appropriate input.
Set a boolean to store whether they want a pyramid or a diamond.
a. Print out a message to show that 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.
On the first line, this would be 1 * which is height spaces away from the left edge, which will be the start of the middle column.
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.
Again, you need an outer loop.
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.
The opposite goes for preceding spaces, in that you need more per line.
This will all be almost the same logic, just in reverse.
Make sure you dont print the last line of the pyramid a second time when
making a diamond.
MY CODE is but it wont compile
/** * * @author Me * @version 4.0 */ import java.util.*; #include
public class HomeWork4 { void pyramid() { int i, space, rows, star = 0;
printf("Please type in the height of the pyramid"); printf("It must be at least 1 high and no more than 25"); scanf(" %d", &rows); for(i = 1; i <= rows; i++){ for(space = 1; space <= rows-i; space++){ printf(" "); } while(star !=(2*i - 1)){ printf("*"); star++;; } star = 0; printf(" "); } } void diamond() { int i, space, rows = 7, star = 0; for(i = 1; i <= rows; i++){ for(space =1; space <= rows-i; space++){ printf(" "); } while(star != (2*i - 1)){ printf("*"); star++;; } star = 0; printf(" "); } rows--; for(i = rows; i >= 1; i--){ for(space = 0; space <= rows-i; space++){ print("*"); star++; } printf(" "); } } int main(){ char a[3]; prinf("Enter Yes tp print diamond Enter No to print Pyramid Enter your choice: "); scanf(" %s",&a); if(strcmp(a,"Yes") == 0) diamond(); else if(strcmp(a,"No")==0) pyramid(); else printf(" Wrong Input"); return 0; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
