Loops - Drawing a tree In this programming assignment, you’ll draw a tree based
ID: 3760936 • Letter: L
Question
Loops - Drawing a tree
In this programming assignment, you’ll draw a tree based on user specifications. The starter program draws a tree, using a triangle with three levels as the leaves and a rectangle with four levels as the trunk.
1. Modify the above program to ask the user to specify a number of tree trunk levels (“Enter trunk height: “), then use a loop to draw that many levels.
Testing suggestion: If the user specifies 4 tree trunk levels, then the original tree should be drawn.
2. Modify the program again to ask the user to specify a number of tree trunk *’s per level (Enter trunk width: “), then use a loop to draw that many *’s per level.
You’ll need to use a nested loop in which the inner loop draws the *’s, and the outer loop iterates a number of times equal to the number of tree trunk levels.
3. Modify the program to ask the user to specify a number of tree leaves levels (“Enter leaves width: “), then use a nested loop to draw that many levels.
You’ll need two inner loops for drawing the leaves: one for outputting spaces and one for outputting *’s. The outer loop iterates a number of times equal to the number of tree leaves levels.
The top level of the leaves must have at least one *.
Here is an example program execution (user input is highlighted here for clarity): Enter trunk height: 10
Enter trunk width: 5
Enter leaves width: 11
*
***
*****
*******
*********
***********
*****
*****
*****
*****
*****
*****
*****
*****
*****
*****
Here is an example program execution (user input is highlighted here for clarity). Note that the trunk is misaligned with the leaves when the trunk and leaves widths are neither both odd nor both even:
Enter trunk height: 5
Enter trunk width: 2 Enter leaves width: 5
*
***
*****
**
**
**
**
**
Here is an example program execution (user input is highlighted here for clarity). Note that the top of the leaves is misaligned when the leaves width is even.:
Enter trunk height: 3
Enter trunk width: 3
Enter leaves width: 6
*
**
****
******
***
***
***
Explanation / Answer
DrawTree.java
import java.util.Scanner;
public class DrawTree {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter trunk hegith: ");
int h = scan.nextInt();
System.out.print("Enter trunk width: ");
int w = scan.nextInt();
System.out.print("Enter leaves width: ");
int l = scan.nextInt();
for(int i=1;i<=l;i+=2) {
for(int k=1;k<=(l-i)/2;k++){
System.out.print(" ");
}
for(int j=1;j<=i;j++) {
System.out.print("*");
}
System.out.println();
}
for(int i=1; i<=h;i++){
for(int k=1;k<=(l-w)/2;k++){
System.out.print(" ");
}
for(int j=1;j<=w;j++){
System.out.print("*");
}
System.out.println();
}
}
}
Output:
Enter trunk hegith: 3
Enter trunk width: 3
Enter leaves width: 6
*
***
*****
***
***
***
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.