×

Hollow Diamond Pattern in Java

Why are patterns important?

Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs.

Hollow Diamond Pattern

The initial and last rows of hollow diamond design each have just one star, whereas the remaining rows all have two stars. We can display any other symbol in replacement of the star (*).

We will separate the design into two pieces to construct the hollow diamond pattern, i.e., upper and lower. Additionally, we will independently create the logic for the top and lower portions. 

Approach:

The strategy is to print spaces before and after the star to increase the spacing between the stars steadily.

To display the box shape, we must print "-" for the very first row (i==1) and also in  the final row (i==n), and "|" for the very first column (j==1) and the very last row (j==n) (last column).

Algorithm:

1. If n is odd, raise it.

2. locate mid=n/2.

3. Go from 1 to middle to print this design's top half (say i).

4. To print the spaces for the upper-left corner of the outer box, go from 1 to mid-i (say j).

5. Print "*" if (i==1) (since for the first row, we need only one star).

6. When the loop is complete, print "*" and then traverse from 1 to 2*i-3 to print gaps for hollow diamonds (let's say j).

7. Navigate from 1 to mid-i and then print spaces once more for the box in the upper right corner (say j).

8. At stage 3, complete the loop.

9. To print the lower half of the design, go from mid+1 to n-1 (say i).

1. Move the cursor from 1 to i-mid (say j) for the lower leftmost outer rectangle.

2. If (I==n-1), print "*" (since for the last row, we need only one star).

3. If not, print "*" and then travel form 1 to 2*(n-i)-3 to print gaps for hollow diamonds (let's say, j), and then print "*" after the loop is finished.

4. For the lowest rightmost outside box, go from 1 to i-mid and print spaces once more (say j).

5. Finish step 9 to close this loop.

By using the for loop

Example: HollowDiamond1.java

//this program is for printing the hallow diamond in java
// importing the required packages
import java.util.Scanner;  
public class HollowDiamond1  
{  
public static void main(String args[])  
{  
Scanner sc=new Scanner(System.in);  
System.out.print("Please enter the number of rows: ");  
// an integer was read from the user 
int row=sc.nextInt();  
System.out.print("Enter the required symbol: ");   
// The required symbol is printed by the user
char sym=sc.next().charAt(0);  
//printing the top section of the pattern
for(int i=1; i<=row; i++)  
{  
for(int j=row; j>i; j--)  
{  
// the space is printed        
System.out.print(" ");  
}  
//the symbol is printed  
System.out.print(sym);  
for(int j=1; j<(i-1)*2; j++)  
{  
//the space for each character is printed     
System.out.print(" ");  
}  
if(i==1)  
{  
// the cursor is then moved to the next line      
System.out.print("\n");  
}  
else  
{  
//the following line is selected after the symbol is printed.          
System.out.print(sym+"\n");  
}  
}    
//logic for printing the lower section of the pattern  
for(int i=row-1; i>=1; i--)  
{  
for(int j=row; j>i; j--)  
{  
//the space (hallow) is printed          
System.out.print(" ");  
}  
//the selected symbol of the user is printed
System.out.print(sym);  
for(int j=1; j<(i-1)*2; j++)  
{  
// the required space      
System.out.print(" ");  
}  
if(i==1)  
{  
// the cursor is then moved to the next line       
System.out.print("\n");  
}  
else  
{  
// the following line is selected after the symbol is printed  
System.out.print(sym+"\n");  
}  
}  
}  
}  

Output

Hollow Diamond Pattern in Java

By using the while loop

The below program is used for printing the hollow diamond by using the while loop.

Example: HallowDiamond2.java

 //this program is for printing the hallow diamond in java
// importing the required packages
import java.util.Scanner;  
public class HallowDiamond2  
{  
public static void main(String args[])  
{  
int i,j;  
Scanner sc=new Scanner(System.in);  
System.out.print("Please enter the number of rows: ");  
int row=sc.nextInt();
// reading the integer value from the user to print num of rows
System.out.print("Enter the symbol required to print: ");   
//The required symbol is printed by the user
char sym=sc.next().charAt(0);  
i=1;  
// the upper part of the pattern is printed  
while(i<=row)  
{  
j=row;  
while(j>i)  
{  
System.out.print(" ");  
j--;  
} // end of the while loop  
System.out.print(sym);  
j=1;  
while(j<(i-1)*2)  
{  
System.out.print(" ");  
j++;  
} 
// the while loop is ended
if(i==1)  
{  
// the cursor is then moved to the next line          
System.out.print("\n");  
}  
else  
{  
// the symbol is printed, and the cursor is then moved to the next line           
System.out.print(sym+"\n");  
}  
i++;  
}
// end of the while loop
// the bottom part of the pattern is printed 
i=row-1;  
while( i>=1)  
{  
j=row;  
while(j>i)  
{  
System.out.print(" ");  
j--;  
}  
// the symbol is printed
System.out.print(sym);  
j=1;  
while(j<(i-1)*2)  
{  
System.out.print(" ");  
j++;  
} 
//ends the loop 
if(i==1)  
{  
// the cursor is moved to the line to the next         
System.out.print("\n");  
}  
else  
{  
// the following line is selected after the symbol is printed  
System.out.print(sym+"\n");  
}  
i--;  
}
// termination of the while loop
}  
}  

Output

Hollow Diamond Pattern in Java

By using the do-While loop

By using the do while loop, we can print hollow diamond patterns.

Example:HollowDiamond3.java

//this program is for printing the hallow diamond in java
// importing the required packages
import java.util.Scanner;  
public class HollowDiamond3  
{  
public static void main(String args[])  
{  
int i,j;  
Scanner sc=new Scanner(System.in);  
System.out.print("Please Enter the required number of rows: ");  
// an integer row in reading from the user for printing the rows  
int row=sc.nextInt();  
System.out.print("The symbol required : ");   
// the required symbol from the user is printed  
char sym=sc.next().charAt(0);  
i=1;  
//the top section of the pattern is printed
// the loop will iterate until the condition become false  
do  
{  
j=row;  
// it will continue until (j>=i) becomes as false
do  
{  
// the space between the symbol is printed     
System.out.print(" ");  
j--;  
}
//end of the do 
while(j>=i);  
System.out.print(sym);  
j=1;  
// the loop will continues until ( j<(i-1)*2 ) condition becomes false
do  
{  
// the space between the character is printed  
System.out.print(" ");  
j++;  
} 
// the do-while will ends
while(j<(i-1)*2);  
if(i==1)  
{  
// the cursor is then moved to the next line              
System.out.print("\n");  
}  
else  
{  
// after the symbol is printed cursor is moved to the next line              
System.out.print(sym+"\n");  
}  
i++;  
} 
// do loop will ends
while(i<=row);  
i=row-1;  
// the bottom part of the pattern is printed
// the loop will iterate until the condition becomes false  
do  
{  
j=row;  
//executes until the condition j>=i becomes false  
do  
{  
// the space is printed     
System.out.print(" ");  
j--;  
}  
while(j>=i);  
// the selected symbol is printed  
System.out.print(sym);  
j=1;  
// the loop will iterates until (j<(i-1)*2) condition is false  
do  
{  
// the spaces are printed      
System.out.print(" ");  
j++;  
}  // the do will end
while(j<(i-1)*2); //condition 
if(i==1)  
{  
// the cursor is then moved to the next line              
System.out.print("\n");  
}  
else  
{  
//after the symbol is printed, the cursor is then moved to the next line               
System.out.print(sym+"\n");  
}  
i--;  
}  
// the do will ends  
while(i>=1);  
}  
}

Output

Hollow Diamond Pattern in Java

Here is another hollow diamond pattern

Divided between upper and lower portions, the following hollow diamond design will be printed. The diamond-shaped design is divided into two sections, the top part including the first five rows and the bottom section containing the final five rows. The logic for the upper and lower portions will each be implemented independently.

We'll start by making a 10 by 10 matrix. Print the (*) sign in the first row. Two spots are located in the second row from the top. Space has to be calculated. The amount of space increases by two in the top portion while decreasing by two in the lower section.

Therefore, we may compute the space by applying the generalized formula 2*i-2. Let's say we need to determine the third row's space. 2*3-2 Equals 4 (space) (space). The variables i ,j, and k are used in the program below to represent rows, columns, and spaces.

Example: HollowDiamond4.java

//this program is for printing the hallow diamond in java
// importing the required packages
import java.io.*;
import java.util.*;
// a class HollowDiamond4 is created
public class HollowDiamond4  
{  
public static void main(String args[])  
{   
// logic for displaying the half of the pattern in the hallow diamond pattern 
for(int i=1; i<=11; i++)  
{  
for(int j=1; j<=11-i+1; j++)  
{  
System.out.print("*");  
}   
// loop for measuring the space required
for(int l=1; l<=2*i-2; l++)  
{  
System.out.print(" ");  
}   
for(int j1=1; j1<=11-i+1; j1++)  
{  
System.out.print("*");  
}   
System.out.println();  
}  
//  logic for displaying the half of the pattern in the hallow diamond pattern
for(int i=10; i>=1; i--)  
{  
for(int j=i; j<=11; j++)  
{  
System.out.print("*");  
}   
//loop for measuring the space required
for(int k1=1; k1<=(2*i)-2; k1++)  
{  
System.out.print(" ");  
}   
for(int j1=i; j1<=11; j1++)  
{  
System.out.print("*");  
}   
System.out.println();  
}  
}   
}  

Output

Hollow Diamond Pattern in Java

Related Topics

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Generic Linked List in Java

A linear data structure known as a Linked List stores values in nodes. As we already know, each node has two properties: its value and a link to the node...

6 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

String Declaration in Java

A string is a group of characters. In Java, the string can be treated as both class and datatype. In Java programming, the String class have many advantages. Everything in...

3 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

Hello Program in Java

Hello Program in Java The Hello program in Java displays the word Hello on the console. It is the basic program in Java. In this section, we will learn different ways...

3 minutes read.

Java Queue Interface

Queue interface is a subtype of Collection interface. All methods in the Collection interface are also available in the Queue interface. It provides operations of Collection and also some additional...

2 minutes read.

Java Math IEEEremainder() Method

The IEEEremainder() method of Math class calculates the remainder as prescribed by the IEEE754 standard. This method simply returns the remainder when f1 (dividend) is divided by f2 (divisor). Syntax: public static...

2 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 minutes read.

Least Operator to Express Number in Java

In this article, we will learn about how to obtain a target number using a single number or a single integer by leveraging least operators in Java. There can be...

3 minutes read.

Interface in Java

  Interface in Java In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also...

6 minutes read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.