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

I have done all the work in JAVA Blue J Your program must do the following in or

ID: 3873773 • Letter: I

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.

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 don’t 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;
}
}

Explanation / Answer

Please find mmy implementation.

/**

*

* @author Me

* @version 4.0

*/

import java.util.*;

public class HomeWork4

{

   static void pyramid(int n)

   {

       int i, j;

       // outer loop to handle number of rows

       // n in this case

       for(i=0; i<n; i++)

       {

           // inner loop to handle number of columns

           // values changing acc. to outer loop

           for(j=0; j<=i; j++)

           {

               // printing stars

               System.out.print("* ");

           }

           // ending line after each row

           System.out.println();

       }

   }

   static void diamond(int n)

   {

       int space = n-1;

       // run loop (parent loop) till number of rows

       for (int i=0; i<n; i++)

       {

           // loop for initially space, before

           // star printing

           for (int j=0;j< space;j++)

               System.out.print(" ");

           // Print i+1 stars

           for (int j=0;j<=i;j++)

               System.out.print("* ");

           System.out.print(" ");

           space--;

       }

       // Repeat again in reverse order

       space = 0;

       // run loop (parent loop) till number of rows

       for (int i=n; i>0; i--)

       {

           // loop for initially space, before

           // star printing

           for (int j=0; j<space; j++)

               System.out.print(" ");

           // Print i stars

           for (int j=0;j< i;j++)

               System.out.print("* ");

           System.out.print(" ");

           space++;

       }

   }

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       System.out.println("Please type in the height of the pyramid");

       System.out.println("It must be at least 1 high and no more than 25");

       int rows = sc.nextInt();

       // validating user input for height

       while(rows < 1 || rows > 25) {

           System.out.print("It must be at least 1 high and no more than 25");

           rows = sc.nextInt();

       }

       System.out.print("Enter Yes to print diamond Enter No to print Pyramid Enter your choice: ");

       String a = sc.next();

       while(!(a.equalsIgnoreCase("yes") || a.equalsIgnoreCase("no"))) {

           System.out.print("Enter Yes to print diamond or No to print Pyramid : ");

           a = sc.next();

       }

       sc.close();

       if("yes".equalsIgnoreCase(a))

           diamond(rows);

       else

           pyramid(rows);

   }

}

/*

Sample run:

Please type in the height of the pyramid

It must be at least 1 high and no more than 25

13

Enter Yes to print diamond

Enter No to print Pyramid

Enter your choice: no

*

* *

* * *

* * * *

* * * * *

* * * * * *

* * * * * * *

* * * * * * * *

* * * * * * * * *

* * * * * * * * * *

* * * * * * * * * * *

* * * * * * * * * * * *

* * * * * * * * * * * * *

*/