Skip to content
Snippets Groups Projects
Commit b62d4c61 authored by Goik Martin's avatar Goik Martin
Browse files

Simple X-mas tree

parent f37dfde1
Branches
No related tags found
No related merge requests found
......@@ -9,6 +9,122 @@
xmlns:db="http://docbook.org/ns/docbook">
<title>Statements</title>
<section xml:id="sw1SectXmas">
<title>Merry Xmas</title>
<qandaset defaultlabel="qanda" xml:id="sd1QandaXmasTree">
<qandadiv>
<qandaentry>
<question>
<para>Write an application to print an ASCII art Xmas tree.
Provide a configurable parameter which allows for controlling the
tree's size. The following example shows a tree spanning 6 rows
excluding top (X) and bottom (###):</para>
<programlisting language="none"> X
*
***
*****
*******
*********
***********
###
###</programlisting>
<tip>
<para>The following example printing a triangle may serve as a
starting point:</para>
<informaltable border="1">
<colgroup width="47%"/>
<colgroup width="5%"/>
<colgroup width="48%"/>
<tr>
<td valign="top"><programlisting language="none"> public static void main(String[] args) {
int numberOfRows = 7;
for (int row = 0; row &lt; numberOfRows; row++) {
for (int x = 0; x &lt;row; x++) {
System.out.print('*');
}
System.out.println('*');
}
}</programlisting></td>
<td align="center"></td>
<td valign="top"><programlisting language="none">*
**
***
****
*****
******
*******</programlisting></td>
</tr>
</informaltable>
</tip>
</question>
<answer>
<programlisting language="none"> public static void main(String[] args) {
// Example: 6 rows, tree's body loop index ranging from 0 to 5
//
// X The tree's top.
// 0 *
// 1 ***
// 2 *****
// 3 ******* The tree's body.
// 4 *********
// 5 ***********
// III The tree's two bottom trunk lines.
// III
final int numberOfRows = 6; // You may easily change this value.
// Part one: the tree's top
//
for (int x = 0; x &lt; numberOfRows; x++) { // Printing the tree's top. We need
System.out.print(' '); // numberOfRows preceding spaces
} // before printing the
System.out.println("X"); // 'X' (top) character.
// Part two: The tree's body
//
for (int row = 0; row &lt; numberOfRows ; row++) { // Outer loop printing the tree's body.
for (int x = 0; x &lt; numberOfRows - row;x++) { // Starting each line with (numberOfRows - row)
System.out.print(' '); // space characters ...
}
for (int x = 0; x &lt; 2 * row + 1; x ++) { // .. then printing (2*row+1) body ('*') characters ...
System.out.print('*'); // May try '▲' instead
}
System.out.print("\n"); // ... and finally terminating the current body row.
}
// Part three: The tree's bottom trunk
//
for (int x = 0; x &lt; numberOfRows-1; x++) { // Preparing the first line of bottom trunk part ...
System.out.print(' ');
}
System.out.println("###"); // ... finished.
for (int x = 0; x &lt; numberOfRows-1; x++) { // Preparing the second line of bottom trunk part ...
System.out.print(' ');
}
System.out.println("###"); // ... finished.
}</programlisting>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
<section xml:id="sd1SectSquareNumberTable">
<title>A table of square numbers</title>
......@@ -189,8 +305,8 @@
different values just by changing tis single value.</para>
<tip>
<para>You'll need a loop nested within an outer one like:
</para>
<para>You'll need a loop nested within an outer one
like:</para>
<programlisting language="none">for (int y=0; &lt; limit; y++){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment