Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
b62d4c61
Commit
b62d4c61
authored
10 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Simple X-mas tree
parent
f37dfde1
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Doc/Sd1/statements.xml
+118
-2
118 additions, 2 deletions
Doc/Sd1/statements.xml
with
118 additions
and
2 deletions
Doc/Sd1/statements.xml
+
118
−
2
View file @
b62d4c61
...
...
@@ -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
<
numberOfRows; row++) {
for (int x = 0; x
<
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
<
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
<
numberOfRows ; row++) { // Outer loop printing the tree's body.
for (int x = 0; x
<
numberOfRows - row;x++) { // Starting each line with (numberOfRows - row)
System.out.print(' '); // space characters ...
}
for (int x = 0; x
<
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
<
numberOfRows-1; x++) { // Preparing the first line of bottom trunk part ...
System.out.print(' ');
}
System.out.println("###"); // ... finished.
for (int x = 0; x
<
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;
<
limit; y++){
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment