pattern program in java

Pattern programs in Java

Pattern programs in java are the programs that 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 match 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 by 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 denotation of row set and j is the denotation for column set usually.
  • We then try to find the relation between the element and (i,j). The value of the 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

Odd number pattern program in Java

In the below program, only odd number pattern will be print in the form of Triangle.

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=num+2; 
}    
System.out.println(); 
} 
}  
public static void main(String args[]) 
{ 
int s = 11; // Number of lines
printNumbers(s); 
} 
}

Output :

1 
1 3 
1 3 5 
1 3 5 7 
1 3 5 7 9 
1 3 5 7 9 11 
1 3 5 7 9 11 13

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 a 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 Uses

  • While designing something, a design pattern can be a solution to the repeatedly occurring problem.
  • Pattern design is also called as forever unfinished design. We can 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 that can be used in multiple situations.
  • Object-oriented developers perform these practices to make software development easy.
  • They are reusable and used in multiple projects.
  • With the help of pattern designs, we can maintain good structures.
  • They are transparent solutions for the application design.

One comment on “Pattern programs in Java

Leave a comment

Your email address will not be published.