Bouncing Ball pattern program in java
Hello friends, welcome to programmingscoop. Today i'm going share my new pattern design program written in java. As we all know pattern designing is the best way to enhance our logical building skill. so try to learn form today's code for understand logic building. Comments and suggestion are most welcome.
About program
In this program we will create a pattern in which there are '*' (Start) shape balls are there and they move in square form. I recommend you to download the full code and run it on your pc for better understanding of this program. you can download the full code from here. if you have any query so feel free to ask in comment section. so let's understand the program.
Let's start
In this program i have created only one class namely square_pattern for designing the pattern. It contains two methods main() and design_pattern().
Main() Method
In main method we create the object of square_pattern class and call its method design_pattern().
- /* main method */
- public static void main(String args[])
- {
- square_pattern obj=new square_pattern(); //creating object
- obj.design_pattern(); //calling method
- System.out.println();
- }
design_pattern() Method
This method do the main functioning of program it create the pattern on screen.I have used many comments so you can easily understand the code but if you still have any query ask me from comment section any time.
- class square_pattern
- {
- int width=80; //width of terminal
- int height=24; //height of terminal
- int i; //used for loop
- //int f,s,t;
- int nl; //used for taking new line
- int loop=1; //used for continuous of printting
- int rev=0; //used to reverse the ball
- int rwidth=0; //represent right side width of ball
- int lwidth=0; //represent left side width of ball
- /* used for desining pattern */
- public void design_pattern()
- {
- /* this while loop will repeat the complete pattern again & again */
- while(loop!=0)
- {
- rev=0;rwidth=0;lwidth=0; //reinitializing of variables
- /* this loop will print the complete pattern once*/
- for(i=1;i<=height-3;i++)
- {
- /* for clearing and holding the screen */
- try{
- Thread.sleep(50); //for holding screen
- System.out.print("\033[H\033[2J"); //for clearing screen
- System.out.flush();
- }
- catch(Exception e)
- {
- }
- /****** printing of first ball ********/
- //this will move '*' downward
- if(i<=(height/2)-1)
- {
- for(nl=1;nl<i;nl++)
- System.out.println(" ");
- }
- //this will move '*' upward
- if(i>(height/2)-1)
- {
- rev=rev+2;
- for(nl=1;nl<i-rev;nl++)
- System.out.println(" ");
- }
- System.out.println();
- // calculating spaces to print before '*'
- for(int space=1;space<=width/2;space++)
- System.out.print(" ");
- //printing first star
- System.out.print("*");
- /****** printing middle line *******/
- //this maintain the distance between first '*' and middle line
- if(i<=(height/2)-1)
- {
- for(nl=i;nl<=(height/2)-2;nl++)
- System.out.println(" ");
- }
- if(i>(height/2)-1)
- {
- for(nl=i;nl>(height/2)-1;nl--)
- System.out.println(" ");
- }
- System.out.println();
- //this will move the first middle line '*' towards right
- if(i<=11)
- {
- lwidth=lwidth+3;
- for(int space=1;space<=lwidth;space++)
- System.out.print(" ");
- }
- //this will move the first middle line '*' towards left
- if(i>11)
- {
- lwidth=lwidth-3;
- if(i==21){lwidth=lwidth-1;}
- for(int space=1;space<=lwidth;space++)
- System.out.print(" ");
- }
- //printing first middle line '*'
- System.out.print("*");
- //this will maintain the distance between first and second middle line '*'
- for(int space=1;space<=(width/2)-(lwidth+1);space++)
- System.out.print(" ");
- //printing second middle line '*'
- System.out.print("*");
- //this will move the third middle line '*' towards left
- if(i<=11)
- { rwidth=rwidth+3;
- for(int space=1;space<=(width/2)-rwidth;space++)
- System.out.print(" ");
- }
- //this will move the third middle line '*' towards right
- if(i>11)
- { rwidth=rwidth-3;
- for(int space=1;space<=(width/2)-rwidth;space++)
- System.out.print(" ");
- }
- //printing third middle line star
- System.out.print("*");
- /****** printing last ball ******/
- //this will move the '*' towards upward
- if(i<=(height/2)-1)
- {
- for(nl=i;nl<(height/2)-1;nl++)
- System.out.println(" ");
- }
- //this will move the '*' towards downward
- if(i>(height/2)-1)
- {
- for(nl=i;nl>(height/2)-1;nl--)
- System.out.println(" ");
- }
- System.out.println();
- //this will calculate no. of spaces before printing '*'
- for(int space=1;space<=width/2;space++)
- System.out.print(" ");
- //printing last '*'
- System.out.print("*");
- }
- }
- }
Now I hope you learn something from this program and it help tell me your views by commenting. you can download the full code form here.
Visit our other post to enhance or learn your programming.
1 Comments
Nice
ReplyDelete