Write an application that displays the following patterns separately, one below
ID: 3923687 • 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.println(); 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 two patterns. There should be no other output statements in the program. Save the file as TrianglePrinting.java. Again, Don't forget to create the application/project TrianglePrintingTest.java Class that has the main method and an object to use the TrianglePrinting class.Explanation / Answer
/****************TrianglePrinting.java****************/
public class TrianglePrinting {
/*
* Printing triangle print pattern1 according question
*/
public static void pattern1(int number) {
/* For loop start outer */
for (int i = 0; i < number; i++) {
/* Printing space */
/* for loop inner start for space */
for (int space = 0; space < i; space++) {
System.out.print(" ");
}
/* for loop inner End for space */
/* Printing star */
/* for loop start for star */
for (int star = i; star < number; star++) {
System.out.print("*");
}
/* for loop End for star */
System.out.println();// for new line
}
}
/*
* Printing triangle print pattern2 according question
*/
public static void pattern2(int number) {
/* For loop start outer */
for (int i = number - 1; i >= 0; i--) {
/* Printing space */
/* for loop inner start for space */
for (int space = 0; space < i; space++) {
System.out.print(" ");
}
/* for loop inner End for space */
/* Printing star */
/* for loop start for star */
for (int star = i; star < number; star++) {
System.out.print("*");
}
/* for loop End for star */
System.out.println();// for new line
}
}
}
/************************TrianglePrintingTest*******************/
public class TrianglePrintingTest {
/* Main method start */
public static void main(String[] args) {
/* Calling first pattern */
System.out.println(" Pattern 1: ");
TrianglePrinting.pattern1(9);
System.out.println(" Pattern 2: ");
/* calling second pattern */
TrianglePrinting.pattern2(9);
}/* Main method end */
}
/************output start****************/
Pattern 1:
*********
********
*******
******
*****
****
***
**
*
Pattern 2:
*
**
***
****
*****
******
*******
********
*********
/***************output End*****************/
If you have any query please feel free to ask.
Thanks a lot
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.