SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Introduction To Parameters And Objects
Exercise:
Programming Projects
Question:1 | ISBN:9780136091813 | Edition: 2

Question

Write a program that produces images of Christmas trees as output. It should have a method with two parameters: one for the number of segments in the tree and one for the height of each segment. For example, the tree shown here on the left has three segments of height 4 and the one on the right has two segments of height 5:

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

import java.util.Scanner;

public class CharistmasTreeTest{
    
    // Method to generate the Christmas tree image
    public static void generateTree(int segments, int height) {
        // Calculate the width of the tree
        int width = height * 2 - 1;
        
        // Iterate through each segment
        for (int segment = 1; segment <= segments; segment++) {
            // Iterate through each row of the segment
            for (int row = 1; row <= height; row++) {
                // Calculate the number of spaces and asterisks for each row
                int numSpaces = height - row;
                int numAsterisks = row * 2 - 1;
                
                // Print the spaces
                for (int i = 1; i <= numSpaces; i++) {
                    System.out.print(" ");
                }
                
                // Print the asterisks
                for (int i = 1; i <= numAsterisks; i++) {
                    System.out.print("*");
                }
                
                // Move to the next line
                System.out.println();
            }
        }
        
        // Print the trunk of the tree
        for (int i = 1; i <= segments; i++) {
            for (int j = 1; j <= width / 2 - 1; j++) {
                System.out.print(" ");
            }
            System.out.println("| |");
        }
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Prompt the user to enter the number of segments and height
        System.out.print("Enter the number of segments: ");
        int segments = scanner.nextInt();
        
        System.out.print("Enter the height of each segment: ");
        int height = scanner.nextInt();
        
        // Generate the Christmas tree
        generateTree(segments, height);
        
        scanner.close();
    }
}

Executed Output:

Enter the number of segments: 4
Enter the height of each segment: 5
    *
   ***
  *****
 *******
*********
    *
   ***
  *****
 *******
*********
    *
   ***
  *****
 *******
*********
    *
   ***
  *****
 *******
*********
   | |
   | |
   | |
   | |

 

0 0

Discussions

Post the discussion to improve the above solution.