aakash2310

PyramidDemo.java

Feb 18th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. // class draw pyramid sync - pass char
  2. class PyramidDemo
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         Draw d = new Draw();
  7.         DrawStar ds = new DrawStar(d,'*');
  8.         DrawDollar dd = new DrawDollar(d,'$');
  9.         ds.start();
  10.         dd.start();
  11.     }
  12. }
  13. class Draw
  14. {
  15.     synchronized void drawPyramid(char ch)
  16.     {
  17.         for(int i=0;i<5;i++)
  18.                 {
  19.                     for(int j=5-i;j>1;j--)
  20.                     {
  21.                         System.out.print(" ");
  22.                     }
  23.                     for(int j=0;j<=i;j++)
  24.                     {
  25.                         System.out.print(ch + " ");
  26.                     }
  27.                     System.out.println(" ");
  28.         }
  29.     }
  30. }
  31.  
  32. class DrawStar extends Thread
  33. {
  34.         Draw d;
  35.         char ch;
  36.         DrawStar(Draw d,char ch)
  37.         {
  38.             this.d = d;
  39.             this.ch = ch;
  40.         }
  41.         public void run()
  42.         {
  43.             d.drawPyramid(ch);
  44.         }
  45. }
  46. class DrawDollar extends Thread
  47. {
  48.         Draw d;
  49.         char ch;
  50.         DrawDollar(Draw d,char ch)
  51.         {
  52.             this.d = d;
  53.             this.ch = ch;
  54.         }
  55.         public void run()
  56.         {
  57.             d.drawPyramid(ch);
  58.         }
  59. }
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment