Question: Tree Structures (Nested loops) Summary Write a program that draws two tree like structures-- a flat rectangular shaped tree and an Xmas- style tree. The

 Tree Structures (Nested loops) Summary Write a program that draws two
tree like structures-- a flat rectangular shaped tree and an Xmas- style
tree. The program TreeStructures.java asks the user for the height of a
tree and uses the height information to draw the trees. It draws
a rectangle/triangle to display the top portion of the tree, followed by

Tree Structures (Nested loops) Summary Write a program that draws two tree like structures-- a flat rectangular shaped tree and an Xmas- style tree. The program TreeStructures.java asks the user for the height of a tree and uses the height information to draw the trees. It draws a rectangle/triangle to display the top portion of the tree, followed by a rectangle to represent the trunk the tree. For e.g., How tall should the top of the tree be? 5 Flat Tree: Xmas Tree: Put the programs in a03 package. Be sure to use suitable constants for your programs. Details See the sample outputs below for how your program should perform. Note that it begins by printing out its title and author information. After that it prints a description of what it does. (You can use the same.) Then it asks the user for how tall the tree should be (just the canopy (green part) at the top, actually). The size needs to be between 5 and 20. If the user enters an invalid size, the program objects and ends -- pausing to ensure that the user sees the message. Otherwise the program Flat Tree: The first tree consists of a rectangle to represent the branches (top part), and a rectangle to represent the trunk (bottom part). Tree: The second tree consists of a triangle to represent the branches, and a 3 of 6 gle to represent the trunk. se rectangle/triangle has as many rows as the number the user entered. The width of the rectangle/triangle is one less than twice the height of the tree (for example, the tree of height 5 has a width of 9). In the case of triangle, the base of the top part is right up against the left margin, and each line above it is indented one space further, and is two characters shorter. The result is an isosceles triangle that looks a bit like the top of a spruce or fir tree. Bottom part: The rectangle below is centred under the rectangle (for Flat tree) or triangle (for Xmas tree). Its height is one more than one fifth of the height of the top part. For example, the rectangle of the tree above has two rows, since 9 + 5 + 1 is 2. The width of the rectangle is one third of the width of the tree -- but add one if that width comes out even. For example, the triangle of height 5 has a width of 9, so the width of the rectangle is 3 (that's 9 + 3). The tree of height 4, however, has a base of width 7. Its rectangle would be 3 wide (i.e., 7+3 is 2, which is even, so change it to 3). You can check if a number is even by finding its remainder when divided by 2. Even numbers have a remainder of zero. Thus: // if n is even if ( n2 == 0) Any number that's not even is odd. The rectangle needs to be indented a certain number of spaces. To get that number, take the width of the top rectangle/triangle, subtract the width of the rectangle, and divide by two. So for the triangle of height 5, the number of spaces to indent the rectangle is (9-3)+2, which is 3. For the tree of height 4, indent (7-3) = 2 = 2 spaces. See below for some sample outputs. Sample outputs The following are some of the possible outcomes of running the TreeStructures.java program for A04 Sample outputi: Tree Structures This program prints two tree 11ke structures! A "Flat" tree and a "Christmas" tree. You choose how big the trees will be. By Jiju Poovvancheri (A00000000) How tall should the top of the tree be? 7 Flat Tree: AR +++++++++++++ . . . Sample output: Tree Structures This program prints two tree like structures! A "Flat" tree and a "Christmas" tree. You choose how big the trees will be. By Jiju Poovvancheri (A00000000) How tall should the top of the tree be? 7 Flat Tree: ***** ..... Xmas Tree: ***** ***

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!