Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I’m trying to output figures of Christmas trees using a method that accepts two

ID: 3881781 • Letter: I

Question

I’m trying to output figures of Christmas trees using a method that accepts two parameters, segments and heights. I just can’t get the spacing right!! They should be centered, and I can only use very basic level java Help 2 public class Christmastrees f 4B public static void main(String args) { / TODO Auto-generated method stub drowTree(3,4); System. out.printInO; drawTree(2,5); 10 segnents, int height) f forCint i-1; ic-segments; i+) t for Cint line-1; linec height;linem) t 12 13 14 15 slidk 17 .0.1 18 1.8.0 19 8.0 20 s/jdk1 21 8.0.122 forCint spaces-8; spaces o -(height-line)ispaces) t System. out.print( for (int dots-1, dotsa (2"line-12*i-1)dots) t System.out.printC); System.out.printlnO: dk1.8 24 1.8.0 27 esfidl 28 dk1 29 System. out.println System.out.println System.out println Machin 31 k1.8. ProplomsJavadoc Deciaration Console 3 cterminated, Christmastrees (Java Applicationl /Library.JavalJavavirtual Machines/jidk1.8.0-144 jak/Contents/Home/binjav oseee Writab'e Smart insert 28:36

Explanation / Answer

public class ChristmasTrees
{
public static void main(String[] args)
{
drawTree(3, 4);
System.out.println();
drawTree(2, 5);
}

public static void drawTree(int segments, int height)
{  

int maxStars = 2*height+2*segement-3;
  
String maxStr = "";

for (int len=0; len < maxStars; len++){
maxStr+=" ";
}

for (int i=1; i <= segement; i++){
  
for (int line=1; line <= height; line++){
String starStr = "";
for (int j=1; j <= 2*line+2*i-3; j++){
starStr+="*";
}
for (int space=0; space <= maxStars-(height+line+i); space++){
starStr = " " + starStr;
}
System.out.println(starStr);
}
}

for (int i=0; i <= maxStars/2;i++){
System.out.print(" ");
}
System.out.print("* ");
for (int i=0; i <= maxStars/2;i++){
System.out.print(" ");
}
System.out.print("* ");
for (int i=0; i <= maxStars/2-3;i++){
System.out.print(" ");
}
System.out.print("******* ");
}


}