Patterns

Kushagra Ojha
9 min readJun 8, 2023

--

A pattern is a common solution to a recurring problem. Some popular patterns in Java include:

  1. Creational patterns: These patterns deal with object creation mechanisms, aiming to create objects in a manner suitable to the situation. Examples include Singleton, Factory, Abstract Factory, and Builder.
  2. Structural patterns: These patterns deal with object and class composition. Examples include Adapter, Composite, and Decorator.
  3. Behavioral patterns: These patterns deal with communication between objects, what goes on between objects. Examples include Observer, Template Method, and State.
  4. Concurrency patterns: These patterns deal with multi-threaded programming. Examples include Thread Pool, Future, and Synchronization.

These patterns provide a way to write maintainable and reusable code, making it easier to develop complex systems.

Problems

  1. Inverted Right Triangle
public class Pattern1 {
public static void printPatternRecursive(int n) {
if (n > 0) {
printPatternRecursive(n - 1);
printStars(n);
System.out.println();
}
}

public static void printStars(int n) {
if (n > 0) {
System.out.print("*");
printStars(n - 1);
}
}

public static void main(String[] args) {
int n = 5;
printPatternRecursive(n);
}
}
*****
****
***
**
*

2. Right Triangle

import java.util.Scanner;

public class Pattern2 {
public static void printPatternRecursive(int rows, int currentRow) {
if (currentRow > rows) {
return;
}

printSpaces(rows, currentRow);
printStars(currentRow);
System.out.println();

printPatternRecursive(rows, currentRow + 1);
}

public static void printSpaces(int rows, int currentRow) {
if (currentRow < rows) {
System.out.print(" ");
printSpaces(rows, currentRow + 1);
}
}

public static void printStars(int currentRow) {
if (currentRow > 0) {
System.out.print("*");
printStars(currentRow - 1);
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows: ");
int rows = sc.nextInt();

printPatternRecursive(rows, 0);

sc.close();
}
}
     *
**
***
****
*****

3. Right down Mirror Star Pattern

import java.util.Scanner;

public class Pattern3 {
public static void printPatternRecursive(int rows, int currentRow) {
if (currentRow < 1) {
return;
}

printSpaces(rows, currentRow);
printStars(currentRow);
System.out.println();

printPatternRecursive(rows, currentRow - 1);
}

public static void printSpaces(int rows, int currentRow) {
if (currentRow < rows) {
System.out.print(" ");
printSpaces(rows, currentRow + 1);
}
}

public static void printStars(int currentRow) {
if (currentRow > 0) {
System.out.print("*");
printStars(currentRow - 1);
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows: ");
int rows = sc.nextInt();

printPatternRecursive(rows, rows);

sc.close();
}
}
*****
****
***
**
*

4. Right Pascal’s Triangle

public class Pattern4 {
public static void printPatternRecursive(int numberOfRows, int currentRow) {
if (currentRow < 0) {
return;
}

printStars(currentRow);
System.out.println();

printPatternRecursive(numberOfRows, currentRow - 1);

if (currentRow != numberOfRows - 1) {
printStars(currentRow);
System.out.println();
}
}

public static void printStars(int count) {
if (count < 1) {
return;
}

System.out.print("* ");
printStars(count - 1);
}

public static void main(String[] args) {
int numberOfRows = 5;

printPatternRecursive(numberOfRows, numberOfRows - 1);
}
}
* 
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

5. K Pattern

public class Pattern5 {
public static void printPatternRecursive(int rows, int currentRow) {
if (currentRow == 0) {
return;
}

printStars(currentRow);
System.out.println();

printPatternRecursive(rows, currentRow - 1);

printStars(currentRow);
System.out.println();
}

public static void printStars(int count) {
if (count == 0) {
return;
}

System.out.print("* ");
printStars(count - 1);
}

public static void main(String[] args) {
int rows = 5;

printPatternRecursive(rows, rows);
}
}
* * * * * 
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *

6. Square Pattern

public class Pattern6 {
public static void printSquarePatternRecursive(int size, int row) {
if (row == size) {
return;
}

printStars(size);
System.out.println();

printSquarePatternRecursive(size, row + 1);
}

public static void printStars(int count) {
if (count == 0) {
return;
}

System.out.print("*");
printStars(count - 1);
}

public static void main(String[] args) {
int size = 5;
printSquarePatternRecursive(size, 0);
}
}
*****
*****
*****
*****
*****

7.Hour Glass

import java.util.Scanner;

public class Pattern7 {
private static Scanner sc;

public static void printSandglassPatternRecursive(int rows, int currentRow) {
if (currentRow > rows) {
return;
}

printSpaces(currentRow - 1);
printStars(rows - currentRow + 1);
System.out.println();

printSandglassPatternRecursive(rows, currentRow + 1);

printSpaces(currentRow - 1);
printStars(rows - currentRow + 1);
System.out.println();
}

public static void printSpaces(int count) {
if (count <= 0) {
return;
}

System.out.print(" ");
printSpaces(count - 1);
}

public static void printStars(int count) {
if (count <= 0) {
return;
}

System.out.print("* ");
printStars(count - 1);
}

public static void main(String[] args) {
sc = new Scanner(System.in);

System.out.print("Enter Sandglass Star Pattern Rows: ");
int rows = sc.nextInt();


printSandglassPatternRecursive(rows, 1);
}
}
* * * * * 
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *

8. Diamond Pattern

public class Pattern8 {
public static void main(String[] args) {
int number = 7;
printDiamond(number, 1);
}

public static void printDiamond(int number, int row) {
if (row <= number) {
printSpaces(number - row, 1);
printStars(row * 2 - 1, 1);
System.out.println();
printDiamond(number, row + 1);
} else {
printLowerDiamond(number - 1, number - 1);
}
}

public static void printLowerDiamond(int number, int row) {
if (row >= 1) {
printSpaces(number - row + 1, 1);
printStars(row * 2 - 1, 1);
System.out.println();
printLowerDiamond(number, row - 1);
}
}

public static void printSpaces(int count, int col) {
if (col <= count) {
System.out.print(" ");
printSpaces(count, col + 1);
}
}

public static void printStars(int count, int col) {
if (col <= count) {
System.out.print("*");
printStars(count, col + 1);
}
}
}
      *
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*

9.Rhombus Pattern

import java.util.Scanner;

public class Pattern9 {
private static Scanner sc;

public static void main(String[] args) {
sc = new Scanner(System.in);

System.out.print("Please Enter Rhombus Pattern Rows = ");
int rows = sc.nextInt();

System.out.println("-- Printing Rhombus Pattern of Stars ---");

printRhombus(rows, rows, true);
}

public static void printRhombus(int currentRow, int totalRows, boolean isIncreasing) {
if (currentRow > 0) {
printSpaces(totalRows - currentRow, 1);
printStars(totalRows, 1);
System.out.println();
if (currentRow == 1) {
printRhombus(currentRow - 1, totalRows, false);
} else if (isIncreasing) {
printRhombus(currentRow - 1, totalRows, true);
} else {
printRhombus(currentRow - 1, totalRows, false);
}
}
}

public static void printSpaces(int count, int col) {
if (col <= count) {
System.out.print(" ");
printSpaces(count, col + 1);
}
}

public static void printStars(int count, int col) {
if (col <= count) {
System.out.print("*");
printStars(count, col + 1);
}
}
}
*****
*****
*****
*****
*****

10. Heart Pattern

public class Pattern10 {
public static void main(String[] args) {
final int size = 4;
printHeartPattern(size, 0, 0);
}

public static void printHeartPattern(int size, int row, int col) {
if (row < size) {
if (col <= 4 * size) {
double pos1 = Math.sqrt(Math.pow(row - size, 2) + Math.pow(col - size, 2));
double pos2 = Math.sqrt(Math.pow(row - size, 2) + Math.pow(col - 3 * size, 2));

if (pos1 < size + 0.5 || pos2 < size + 0.5) {
System.out.print('*');
} else {
System.out.print(' ');
}
printHeartPattern(size, row, col + 1);
} else {
System.out.println();
printHeartPattern(size, row + 1, 0);
}
} else {
printLowerHeartPattern(size, 1);
}
}

public static void printLowerHeartPattern(int size, int row) {
if (row <= 2 * size) {
printSpaces(row, 0);
printStars(4 * size + 1 - 2 * row);
System.out.println();
printLowerHeartPattern(size, row + 1);
}
}

public static void printSpaces(int count, int col) {
if (col < count) {
System.out.print(' ');
printSpaces(count, col + 1);
}
}

public static void printStars(int count) {
if (count > 0) {
System.out.print('*');
printStars(count - 1);
}
}
}
  *****   *****  
******* *******
*****************
*****************
***************
*************
***********
*********
*******
*****
***
*

11. Triangle

public class Pattern11 {
public static void main(String[] args) {
int size = 5;
printPattern(size, 0);
}

public static void printPattern(int size, int row) {
if (row < size) {
printSpaces(size - row - 1, 0);
printStars(2 * row + 1);
System.out.println();
printPattern(size, row + 1);
}
}

public static void printSpaces(int count, int col) {
if (col < count) {
System.out.print(" ");
printSpaces(count, col + 1);
}
}

public static void printStars(int count) {
if (count > 0) {
System.out.print("*");
printStars(count - 1);
}
}
}
    *
***
*****
*******
*********

12. Butterfly Pattern

public class Pattern12 {
public static void main(String[] args) {
int rows = 4;
printButterfly(rows);
}

public static void printButterfly(int rows) {
printUpperPattern(rows, 1);
printLowerPattern(rows, 1);
}

public static void printUpperPattern(int rows, int currRow) {
if (currRow <= rows) {
printStars(currRow);
printSpaces(2 * (rows - currRow));
printStars(currRow);
System.out.println();
printUpperPattern(rows, currRow + 1);
}
}

public static void printLowerPattern(int rows, int currRow) {
if (currRow <= rows) {
printStars(rows - currRow + 1);
printSpaces(2 * (currRow - 1));
printStars(rows - currRow + 1);
System.out.println();
printLowerPattern(rows, currRow + 1);
}
}

public static void printStars(int count) {
if (count > 0) {
System.out.print("*");
printStars(count - 1);
}
}

public static void printSpaces(int count) {
if (count > 0) {
System.out.print(" ");
printSpaces(count - 1);
}
}
}
*      *
** **
*** ***
********
********
*** ***
** **
* *

13. Hollow Square

public class Pattern13 {

static void printStarsInRow(int currentRow, int stars) {
if (stars == 0) {
return;
}

if (currentRow == 1 || currentRow == 5) {
System.out.print("* ");
} else {
if (stars == 1 || stars == 5) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}

printStarsInRow(currentRow, stars - 1);
}

static void printLines(int currentRow, int stars) {
if (currentRow == 0) {
return;
}

printStarsInRow(currentRow, stars);
System.out.println();

printLines(currentRow - 1, stars);
}

public static void main(String[] args) {
printLines(5, 5);
}
}
* * * * * 
* *
* *
* *
* * * * *

14. Hollow Diamond

public class Pattern14 {

static void printStars(int stars) {
if (stars == 0) {
return;
}

System.out.print("* ");
printStars(stars - 1);
}

static void printSpaces(int spaces) {
if (spaces == 0) {
return;
}

System.out.print(" ");
printSpaces(spaces - 1);
}

static void printLines(int rows, int spaces) {
if (rows == 0) {
return;
}

printStars(rows);
printSpaces(spaces);
printSpaces(spaces);
printStars(rows);
System.out.println();
printLines(rows - 1, spaces + 1);
}

static void printLowerLines(int rows, int spaces) {
if (rows == 0) {
return;
}

printLowerLines(rows - 1, spaces + 1);
printStars(rows);
printSpaces(spaces);
printSpaces(spaces);
printStars(rows);
System.out.println();
}

public static void main(String[] args) {
printLines(5, 0);
printLowerLines(5, 0);
}
}
* * * * * * * * * * 
* * * * * * * *
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *

15. Character Pattern

public class Pattern15 {

static void printChars(int chars, int value) {
if (chars == 0) {
return;
}

System.out.print(value + " ");
printChars(chars - 1, value + 1);
}

static void printLines(int rows, int incrementFact) {
if (rows == 5) {
return;
}

printChars(rows, incrementFact);
System.out.println();
printLines(rows + 1, rows + incrementFact);
}

public static void main(String[] args) {
printLines(1, 1);
}
}
1 
2 3
4 5 6
7 8 9 10

16. Diagonal Line

public class Pattern16 {
static void printSpacesOrStarsRecursive(int spaces) {
if (spaces == 0) {
return;
}

printSpacesOrStarsRecursive(spaces - 1);
System.out.print(" ");
}

static void printLinesRecursive(int rows, int spaces) {
if (rows == 0) {
return;
}

printLinesRecursive(rows - 1, spaces + 1);
printSpacesOrStarsRecursive(spaces);
System.out.print("* ");
System.out.println();
}

public static void main(String[] args) {
// Call the function to print lines using the recursive approach
printLinesRecursive(5, 0);
}
}
        * 
*
*
*
*

17. Cross Diagonal Line

public class Pattern17 {
static void printPatternRecursive(int rows, int starsOrSpaces) {
if (rows == 0) {
return;
}

printPatternRecursive(rows - 1, starsOrSpaces);
printStarsOrSpaces(rows, starsOrSpaces);
System.out.println();
}

static void printStarsOrSpaces(int rows, int starsOrSpaces) {
if (starsOrSpaces == 0) {
return;
}

printStarsOrSpaces(rows, starsOrSpaces - 1);

if (rows + starsOrSpaces == 6 || rows == starsOrSpaces) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}

public static void main(String[] args) {
// Call the function to print the pattern using the recursive approach
printPatternRecursive(5, 5);
}
}
*       * 
* *
*
* *
* *

18. Mirrored Pascal


public class Pattern18 {

static void stars(int stars){
if(stars == 0){
return;
}

System.out.print("*");
stars(stars - 1);
}

static void spaces(int spaces){
if(spaces == 0){
return;
}
System.out.print(" ");
spaces(spaces - 1);
}

static void printUpperLines(int rows , int spaces){
if(rows == 0){
return;
}

printUpperLines(rows - 1, spaces +1);
spaces(spaces);
stars(rows);
System.out.println();
}

static void printLowerLines(int rows , int spaces){
if(rows == 0){
return;
}

spaces(spaces);
stars(rows);
System.out.println();
printLowerLines(rows - 1, spaces + 1);
}

public static void main(String[] args) {
printUpperLines(5, 0);
printLowerLines(4, 1);
}
}
    *
**
***
****
*****
****
***
**
*

19. Left Arrow


public class Pattern19 {

static void printStars(int stars){
if(stars == 0){
return;
}

System.out.print("*");
printStars(stars - 1);
}

static void printSpaces(int spaces){
if(spaces == 0){
return;
}

System.out.print(" ");
printSpaces(spaces - 1);
}

static void printLines(int rows){
if(rows == 0){
return;
}

printSpaces(rows - 1);
printStars(rows);
System.out.println();
printLines(rows - 1);
}

static void printLowerLines(int rows){
if(rows == 1){
return;
}

printLowerLines(rows - 1);
printSpaces(rows - 1);
printStars(rows);
System.out.println();

}
public static void main(String[] args) {
printLines(5);
printLowerLines(5);
}
}
    *****
****
***
**
*
**
***
****
*****

20. Right Arrow


public class Pattern20 {

static void printStars(int stars){
if(stars == 0){
return;
}

System.out.print("*");
printStars(stars - 1);
}

static void printSpaces(int spaces){
if(spaces == 0){
return;
}

System.out.print(" ");
printSpaces(spaces - 1);
}

static void printLines(int rows , int spaces){
if(rows == 0){
return;
}

printSpaces( 2*spaces - 2);
printStars(rows);
System.out.println();
printLines(rows - 1 , spaces + 1);

}

static void printLowerLines(int rows , int spaces){
if(rows == 1){
return;
}

printLowerLines(rows - 1, spaces + 1);
printSpaces(2*spaces - 2);
printStars(rows);
System.out.println();
}

public static void main(String[] args) {
printLines(5, 1);
printLowerLines(5, 1);
}
}
*****
****
***
**
*
**
***
****
*****

Thanks for Reading ❤❤

--

--

Kushagra Ojha
Kushagra Ojha

Written by Kushagra Ojha

Exploit Developer | Malware Analyst | Security Researcher

No responses yet