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

Write an application that displays the following patterns separately, one below

ID: 3924041 • 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 two patterns. There should be no other output statements m in the program. Save the file as TrianglePrinting.java. Again, Don't forget to create the application/project TrianglePrinting.java class that has the main method and an object to use the TrianglePrinting class.

Explanation / Answer

public class TrianglePrintingTest {

   public static void main(String[] args) {

       TrianglePrinting t =new TrianglePrinting();
       t.pattern1(); // method for printing pattern 1
       t.pattern2(); // method for printing pattern 2

   }

}

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

public class TrianglePrinting {
   public void pattern1()
       {
       int i, j, k;
       for(i=9;i>=1;i--)
       {
       for(j=9;j>i;j--)
       {
       System.out.print(" ");
       }
       for(k=1;k<=i;k++)
       {
       System.out.print("*");
       }
       System.out.println();
       }
       }
   public void pattern2(){
       int i, j, k=16;
   for(i=0; i<9; i++)
   {
   for(j=0; j<k; j++)
   {
   System.out.print(" ");
   }
   k = k - 2;
   for(j=0; j<=i; j++)
   {
   System.out.print("* ");
   }
   System.out.println();
   }
       }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote