Write an application that displays the following patterns se

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.

Solution

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();
   }
       }
}

 Write an application that displays the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site