Write a function called treeTop that prints the top of a tree. It should take no
ID: 3595712 • Letter: W
Question
Write a function called treeTop that prints the top of a tree. It should take no parameters and return void. Write a second function called treeTrunk that prints the trunk of a tree. It should take one integer as a parameter for height (how many lines tall the trunk is), and also have a return type of void Finally, create a function called drawTrees that takes two integer parameters, one for height and one for the number of trees to draw. drawTrees should use treeTop and treeTrunk. Have the user enter the height of the trees (same for all in a "batch") and number of trees to draw. Remember that to draw a backslash you need two backslashes. After drawing the trees requested by the user sk the user if they want more trees, and if they enter "Y" or 'y' repeat the process (enter number and height, then draw). After drawing each "batch" of trees also tell the user how many trees have been drawn overall.Explanation / Answer
************************************************************************
The code is given below.
A sample input output has been given at the end of the code.
Hope this helps.
If you want to change the design of the tree, just change the
System.out.println() in treeTop and treeTrunk.
Other things will remain same.
*************************************************************************
import java.io.*;
public class Tree {
public static void main(String[] args) throws NumberFormatException, IOException {
int total=0;
while(true) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter the height of the tree");
int height = Integer.parseInt(br.readLine());
System.out.println("Enter the number of the trees to draw");
int number = Integer.parseInt(br.readLine());
total+=number;
drawTrees(height, number);
System.out.println("Total trees drawn:"+total);
char ch;
System.out.println("Do you want to draw more trees?[Y|y|N|n]");
ch=br.readLine().charAt(0);
if(ch=='Y'|| ch=='y')
continue;
else
break;
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Invalid Input");
}
}
}
public static void treeTop() {
System.out.println(" "
+ " /\ " +
" / \ " +
" / \ " +
" / \ " +
" ---------");
}
public static void treeTrunk(int height) {
for(int i=1;i<=height;i++) {
System.out.println(" | | ");
}
}
public static void drawTrees(int height, int number) {
for(int i=1;i<=number;i++) {
System.out.println("******************************");
treeTop();
treeTrunk(height);
System.out.println("******************************");
}
}
}
************************************************************
SAMPLE INPUT / OUTPUT:
************************************************************
Enter the height of the tree
5
Enter the number of the trees to draw
5
******************************
/
/
/
/
---------
| |
| |
| |
| |
| |
******************************
******************************
/
/
/
/
---------
| |
| |
| |
| |
| |
******************************
******************************
/
/
/
/
---------
| |
| |
| |
| |
| |
******************************
******************************
/
/
/
/
---------
| |
| |
| |
| |
| |
******************************
******************************
/
/
/
/
---------
| |
| |
| |
| |
| |
******************************
Total trees drawn:5
Do you want to draw more trees?[Y|y|N|n]
y
Enter the height of the tree
6
Enter the number of the trees to draw
7
******************************
/
/
/
/
---------
| |
| |
| |
| |
| |
| |
******************************
******************************
/
/
/
/
---------
| |
| |
| |
| |
| |
| |
******************************
******************************
/
/
/
/
---------
| |
| |
| |
| |
| |
| |
******************************
******************************
/
/
/
/
---------
| |
| |
| |
| |
| |
| |
******************************
******************************
/
/
/
/
---------
| |
| |
| |
| |
| |
| |
******************************
******************************
/
/
/
/
---------
| |
| |
| |
| |
| |
| |
******************************
******************************
/
/
/
/
---------
| |
| |
| |
| |
| |
| |
******************************
Total trees drawn:12
Do you want to draw more trees?[Y|y|N|n]
n
*************************************************************
I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.