pattern program in java

Pattern programs in Java

Pattern programs in java are the programs which have a series of numbers, alphabets or symbols in a particular fashion. We use loops to form pattern programs in java. Pattern class in java contains flags which are basically integer constants which can help us matching the specific patterns.strategy makers, builders, observers etc. use design patterns. Some easy steps to create patterns in java are:

  • First of all, the coder analyses the symmetry of lines of the pattern. If you have to draw a symmetrical pattern, then there should be symmetry on both horizontal as well as vertical sides.
  • We first start with forming the upper part of the pattern towards the lower part.
  • The next step is to identify the element for each row and each column. i is the denotion of row set and j is the denotion for column set usually.
  • We then try to find the relation between the element and (i,j). The value of total number will start with the number of elements and will keep decreasing as the pattern forms.

Pyramid Pattern of Stars in Java

import java.io.*; 
public class DeveloperHelps 
{ 
public static void printSymbols(int s) 
{ 
int i, j;
for(i=0; i<s; i++) { 
for(j=0; j<=i; j++){ 
System.out.print("* "); 
} 
System.out.println(); 
} 
} 
public static void main(String args[]) { 
int s = 7; 
printSymbols(s); 
} 
}

The output of the above code will be:

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

Pyramid Pattern of Numbers

In the above program, we have passed an argument to print 7 symbols (s=7). The β€˜for’ loop helps in executing this pattern program in java and well as in other OOPs languages. We can print the number pattern in this form as well. In that case, the β€˜for’ loop will work like this:

import java.io.*; 
public class DeveloperHelps
{ 
public static void printNumbers(int s) 
{ 
int i, j,num; 
for(i=0; i<s; i++) 
{ 
num=1; 
for(j=0; j<=i; j++) 
{ 
System.out.print(num+ " ");  
num++; 
}    
System.out.println(); 
} 
}  
public static void main(String args[]) 
{ 
int s = 7; 
printNumbers(s); 
} 
}

The output of this program will be:

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7

There are specific problems for which we need specific design patterns. Say, if we have a problem where we want to create a class for which only single object has to be created which can be used for multiple classes.

For this problem, we will use singleton pattern because it solves the above problem better than other pattern designs such as reduction pattern.

Pattern programs in Java have a number of significance while making a program or even in real life. Some of the uses of pattern program in java are:

  • While designing something, a design pattern can be a solution to the repeatedly occurring problem.
  • A pattern design is also called as forever unfinished design. We can be simply modify it and then transform it directly into the code.
  • It is a simple solution in the form of a template on how to solve a query which can be used in multiple situations.
  • Object oriented developers perform these practices to make the software development easy.
  • They are re usable and used in multiple projects.
  • With the help of pattern designs, we can maintain good structures.
  • They are transparent solutions for the application design.

Leave a comment

Your email address will not be published. Required fields are marked *