Java Program to Print a Heart Shape Star Pattern

1. Introduction

In this blog post, we will create a Java program that prints a heart pattern on the console. This pattern is generated using mathematical calculations to place asterisks (*) in a heart shape. The size of the heart is determined by user input, making this program interactive. It's a great example of combining loops, conditions, and mathematical operations to create a complex shape.

2. Program Steps

1. Import the Scanner class to get the size of the heart from the user.

2. Prompt the user to enter the size of the heart.

3. Use loops and mathematical operations to generate the upper and lower parts of the heart shape.

4. Close the scanner to prevent resource leaks.

5. Compile and execute the program, input the size, and see the heart shape on the console.

3. Code Program

// Importing the Scanner class
import java.util.Scanner;

// Define the HeartShapePyramidPattern class
public class HeartShapePyramidPattern {
    // Main method - entry point of the program
    public static void main(String[] args) {
        // Creating a Scanner object to read input
        Scanner sc = new Scanner(System.in);

        // Prompting the user and reading the size of the heart pattern
        System.out.println("Heart Shape Using Pyramid Patterns Generator");
        System.out.println("Enter the size of the heart:");
        int size = sc.nextInt();

        // Informing the user that the heart pattern is being generated
        System.out.println("Generating heart shape....");

        // Upper part of the heart shape
        for (int i = 0; i < size; i++) {
            for (int j = 0; j <= 4 * size; j++) {
                // Using the distance formula to determine the boundary of the heart
                double d1 = Math.sqrt(Math.pow(i - size, 2) + Math.pow(j - size, 2));
                double d2 = Math.sqrt(Math.pow(i - size, 2) + Math.pow(j - 3 * size, 2));

                // Printing '*' within the boundaries of the heart shape
                if (d1 < size + 0.5 || d2 < size + 0.5) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            // Newline after each row
            System.out.println();
        }

        // Lower part of the heart shape
        for (int i = 1; i < 2 * size; i++) {
            // Printing leading spaces for the lower part
            for (int j = 0; j < i; j++) {
                System.out.print(" ");
            }
            // Printing '*' for the lower part of the heart
            for (int j = 0; j < 4 * size + 1 - 2 * i; j++) {
                System.out.print("*");
            }
            // Newline after each row
            System.out.println();
        }

        // Closing the scanner object
        sc.close();
    }
}

Output:

Heart Shape Using Pyramid Patterns Generator
Enter the size of the heart:
10
Generating heart shape....
       *******             *******
     ***********         ***********
    *************       *************
   ***************     ***************
  *****************   *****************
 ******************* *******************
 ******************* *******************
*****************************************
*****************************************
*****************************************
 ***************************************
  *************************************
   ***********************************
    *********************************
     *******************************
      *****************************
       ***************************
        *************************
         ***********************
          *********************
           *******************
            *****************
             ***************
              *************
               ***********
                *********
                 *******
                  *****
                   ***

Explanation:

1. The program begins by importing the Scanner class and initializing it to capture user input, which specifies the size of the heart.

2. The user is prompted to enter the size, and the program outputs a message indicating that the heart pattern generation is starting.

3. For the upper part of the heart, two nested loops calculate the distance from the center of the heart to each point. If the point is within the radius of the heart, an asterisk is printed; otherwise, a space is printed.

4. For the lower part of the heart, the program calculates the necessary spaces and asterisks to complete the heart shape, tapering down to a point.

5. After the pattern is complete, the Scanner object is closed to prevent resource leaks.

Comments