Write an application that displays the following patterns separately, one below
ID: 3640619 • Letter: W
Question
Write an application that displays the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form System.out.print( ‘*’ ); which causes the asterisks to print side by side. A statement of the form System.out.println(); can be used to move to the next line. A statement of the form System.out.print( ‘ ’ ); can be used to display a space for the last two patterns. There should be no other output statements in the program. [Hint: The last two patterns require that each line begin with an appropriate number of blank spaces.]a) b)
* **********
** *********
*** ********
**** *******
***** ******
****** *****
******* ****
******** ***
********* **
********** *
c)
**********
*********
********
*******
******
*****
****
***
**
*
d)
*
**
***
****
*****
******
*******
********
*********
**********
Explanation / Answer
public static void main(String[] args) {
/** a)
* Line index 1-10
* Number of asterisks per line equals to line index
*/
for (int line = 1; line <= 10; line++) { //line 1-10
for (int asterisk = 0; asterisk < line; asterisk++) {
System.out.print('*');
}
System.out.println();
}
//Line spacing between a) and b)
System.out.println();
/** b)
* Line index 10-1
* Number of asterisks per line equals to line index
*/
for (int line = 10; line >= 1; line--) { //line 10-1
for (int asterisk = 0; asterisk < line; asterisk++) {
System.out.print('*');
}
System.out.println();
}
//Line spacing between b) and c)
System.out.println();
/** c)
* Line index 10-1
* Number of spaces per line equals to (10 - line index)
* Number of asterisks per line equals to line index
*/
for (int line = 10; line >= 1; line--) { //line 10-1
for (int space = 0; space < 10 - line; space++) {
System.out.print(' ');
}
for (int asterisk = 0; asterisk < line; asterisk++) {
System.out.print('*');
}
System.out.println();
}
//Line spacing between c) and d)
System.out.println();
/** d)
* Line index 1-10
* Number of spaces per line equals to (10 - line index)
* Number of asterisks per line equals to line index
*/
for (int line = 1; line <= 10; line++) { //line 10-1
for (int space = 0; space < 10 - line; space++) {
System.out.print(' ');
}
for (int asterisk = 0; asterisk < line; asterisk++) {
System.out.print('*');
}
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.