Follow all instructions exactly given below: Listing 6.4 in the text book demons
ID: 3806944 • Letter: F
Question
Follow all instructions exactly given below:
Listing 6.4 in the text book demonstrates the use of a nested for loop by printing a pattern of asterisks (stars), starting from the least number of stars per row (1) to the most number of stars per row (10). You will modify this program so that it does the following:
Listing 6.4:
//********************************************************************
// Stars.java Author: Lewis/Loftus
//
// Demonstrates the use of nested for loops.
//********************************************************************
public class Stars
{
//-----------------------------------------------------------------
// Prints a triangle shape using asterisk (star) characters.
//-----------------------------------------------------------------
public static void main(String[] args)
{
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++)
{
for (int star = 1; star <= row; star++)
System.out.print("*");
System.out.println();
}
}
}
Asks the user how many rows of stars they want.
Prints the stars starting with the most stars per row and ending with the least stars per row, but aligned along the right margin (i.e. flipped vertically). You must create a void method called starpattern3 that does this part. (Hint: your outer for loop must contain two inner for loops – one to create a pattern of spaces and another to create a pattern of stars.)
Sample Output Enter the number of rows: 6Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
//********************************************************************
//Stars.java Author: Lewis/Loftus
//
//Demonstrates the use of nested for loops.
//********************************************************************
public class Stars
{
//-----------------------------------------------------------------
//Prints a triangle shape using asterisk (star) characters.
//-----------------------------------------------------------------
public static void main(String[] args)
{
final int MAX_ROWS = 10;
for (int row = MAX_ROWS; row >= 1; row--)
{
for(int space =1; space <=(MAX_ROWS-row); space++)
System.out.print(" ");
for (int star = 1; star <= row; star++)
System.out.print("*");
System.out.println();
}
}
}
/*
Sample run:
**********
*********
********
*******
******
*****
****
***
**
*
*/