Exercise: Let's create Heart pattern -
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | |
1 | 5 | 5 | 5 | 5 | |||||||||||||||
2 | 4 | 4 | 4 | 4 | |||||||||||||||
3 | 3 | 3 | 3 | 3 | |||||||||||||||
4 | 2 | 2 | 2 | 2 | |||||||||||||||
5 | 1 | 1 | 1 | ||||||||||||||||
6 | 1 | 1 | |||||||||||||||||
7 | 2 | 2 | |||||||||||||||||
8 | 3 | 3 | |||||||||||||||||
9 | 4 | 4 | |||||||||||||||||
10 | 5 | 5 | |||||||||||||||||
11 | 5 | 5 | |||||||||||||||||
12 | 4 | 4 | |||||||||||||||||
13 | 3 | 3 | |||||||||||||||||
14 | 2 | 2 | |||||||||||||||||
15 | 1 |
Solution:
Step 1: Count the rows and columns needed - 15 rows and 19 columns.
Step 2: There will be a for loop for 15 rows. See the pattern of numbers how they are ordered. There are 3 different patterns - row 1 - 5, row 6 - 10 and row 11 - 15. Using if conditions, separate code for each pattern.
Step 3: Start declaring 4 variables having column numbers in first row, which are not empty and one variable having value for that row. In first row, we have four cells - 5, 6, 14, 15 which are having number 5 as value and other cells have spaces.
int cell1 = 5, cell2 = 6, cell3 = 14, cell4 = 15, val = 5;
Step 4: Start writing a for loop which will run 15 times, one loop is equivalent to one row. Last statement in the loop should be println() statement, so next iteration will start for next row. Divide the loop in 3 sections for three patterns using if condition with row number. The code will look like this -
for (int i = 1; i <= 15; i++) { if(i <= 5) { } else if(i >=6 && i <=10){ } else if(i >=11 && i <=15){ } System.out.println(""); }
Step 5: For row numbers 1 to 5, there should be a for loop for 19 columns (19 cells) and for each iteration, there are few if conditions -
if cell number is less than cell1, then spaces -
Before loop starts, increase cell1 and decrease cell4 by 1, and val increase by 1 -
if cell number is less than cell1, then spaces -
Before loop starts, increase cell1 and decrease cell4 by 1,
if cell number is less than cell1, then spaces -
After each for loop, decrease val by 1 -
if cell number is less than cell1, then spaces -
if(j < cell1) System.out.print(" ");if cell number is greater than cell2 and cell number is less than cell3, then spaces -
if(j > cell2 && j < cell3) System.out.print(" ");if cell number isequal to cell1 or cell2 or cell3 or cell4, then print value as val -
if(j == cell1 || j == cell2 || j == cell3 || j == cell4) System.out.print(val + " ");if cell number is greater than cell1 and cell number is less than cell2, then spaces -
if(j > cell1 && j < cell2) System.out.print(" ");if cell number is greater than cell3 and cell number is less than cell4, then spaces -
if(j > cell3 && j < cell4) System.out.print(" ");After finishing the for loop, decrease cell1 and cell3 by 1 and increase cell2 and cell4 by 1, and decrease value by 1 -
cell1--; cell2++; cell3--; cell4++; val--;When you integrate the code, it will look like this -
if(i <= 5) { for (int j = 1; j <= 19; j++) { if(j < cell1) System.out.print(" "); if(j > cell2 && j < cell3) System.out.print(" "); if(j == cell1 || j == cell2 || j == cell3 || j == cell4) System.out.print(val + " "); if(j > cell1 && j < cell2) System.out.print(" "); if(j > cell3 && j < cell4) System.out.print(" "); } cell1--; cell2++; cell3--; cell4++; val--; }Step 6: For row numbers 6 to 10, there should be a for loop for 19 columns (19 cells) and for each iteration, in these rows we use only two cells - cell1 and cell4 there are few if conditions -
Before loop starts, increase cell1 and decrease cell4 by 1, and val increase by 1 -
cell1++;cell4--;val++;In the for loop -
if cell number is less than cell1, then spaces -
if(j < cell1) System.out.print(" ");if cell number is equal to cell1 or cell4, then print value as val -
if(j == cell1 || j == cell4) System.out.print(val + " ");if cell number is greater than cell1 and cell number is less than cell4, then spaces -
if(j > cell1 && j < cell4) System.out.print(" ");When you integrate the code, it will look like this -
else if(i >=6 && i <=10){ cell1++;cell4--;val++; for (int j = 1; j <= 19; j++) { if(j < cell1) System.out.print(" "); if(j == cell1 || j == cell4) System.out.print(val + " "); if(j > cell1 && j < cell4) System.out.print(" "); } }Step 7: For row numbers 11 to 15, there should be a for loop for 19 columns (19 cells) and for each iteration, there are few if conditions -
Before loop starts, increase cell1 and decrease cell4 by 1,
cell1++;cell4--;In the for loop -
if cell number is less than cell1, then spaces -
if(j < cell1) System.out.print(" ");if cell number is equal to cell1 or cell4, then print value as val -
if(j == cell1 || j == cell4) System.out.print(val + " ");if cell number is greater than cell1 and cell number is less than cell4, then spaces -
if(j > cell1 && j < cell4) System.out.print(" ");When you integrate the code, it will look like this -
else if(i >=11 && i <=15){ cell1++;cell4--; for (int j = 1; j <= 19; j++) { if(j < cell1) System.out.print(" "); if(j == cell1 || j == cell4) System.out.print(val + " "); if(j > cell1 && j < cell4) System.out.print(" "); } val--; }
val--;Step 8: The whole integrated code will look like as follows. Save the program file as CreateHeartPattern.java file.
Class CreateHeartPattern (CreateHeartPattern.java)
public class CreateHeartPattern { public static void main(String args[]) { int cell1 = 5, cell2 = 6, cell3 = 14, cell4 = 15, val = 5; for (int i = 1; i <= 15; i++) { if(i <= 5) { for (int j = 1; j <= 19; j++) { if(j < cell1) System.out.print(" "); if(j > cell2 && j < cell3) System.out.print(" "); if(j == cell1 || j == cell2 || j == cell3 || j == cell4) System.out.print(val + " "); if(j > cell1 && j < cell2) System.out.print(" "); if(j > cell3 && j < cell4) System.out.print(" "); } cell1--; cell2++; cell3--; cell4++; val--; } else if(i >=6 && i <=10){ cell1++;cell4--;val++; for (int j = 1; j <= 19; j++) { if(j < cell1) System.out.print(" "); if(j == cell1 || j == cell4) System.out.print(val + " "); if(j > cell1 && j < cell4) System.out.print(" "); } } else if(i >=11 && i <=15){ cell1++;cell4--; for (int j = 1; j <= 19; j++) { if(j < cell1) System.out.print(" "); if(j == cell1 || j == cell4) System.out.print(val + " "); if(j > cell1 && j < cell4) System.out.print(" "); } val--; } System.out.println(""); } } }The result will look like as follows -
5 | 5 | 5 | 5 | |||||||||||||||
4 | 4 | 4 | 4 | |||||||||||||||
3 | 3 | 3 | 3 | |||||||||||||||
2 | 2 | 2 | 2 | |||||||||||||||
1 | 1 | 1 | ||||||||||||||||
1 | 1 | |||||||||||||||||
2 | 2 | |||||||||||||||||
3 | 3 | |||||||||||||||||
4 | 4 | |||||||||||||||||
5 | 5 | |||||||||||||||||
5 | 5 | |||||||||||||||||
4 | 4 | |||||||||||||||||
3 | 3 | |||||||||||||||||
2 | 2 | |||||||||||||||||
1 |