Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
G
GoikLectures
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Dr. Martin Goik
GoikLectures
Commits
c46bc861
Commit
c46bc861
authored
Sep 27, 2020
by
Dr. Martin Goik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Java 15 version of Ascii Xmas tree
parent
30b37a97
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
86 deletions
+70
-86
Doc/Sd1/Ref/Statements/MoreFunXmasTree/pom.xml
Doc/Sd1/Ref/Statements/MoreFunXmasTree/pom.xml
+2
-2
Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/Xmas.java
...nXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/Xmas.java
+2
-1
Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/XmasUsingFormat.java
...rc/main/java/de/hdm_stuttgart/mi/sd1/XmasUsingFormat.java
+10
-11
Doc/Sd1/statements.xml
Doc/Sd1/statements.xml
+56
-72
No files found.
Doc/Sd1/Ref/Statements/MoreFunXmasTree/pom.xml
View file @
c46bc861
...
...
@@ -25,8 +25,8 @@
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
3.8.1
</version>
<configuration>
<source>
1
1
</source>
<target>
1
1
</target>
<source>
1
5
</source>
<target>
1
5
</target>
</configuration>
</plugin>
...
...
Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/Xmas.java
View file @
c46bc861
package
de.hdm_stuttgart.mi.sd1
;
/**
* Beginner's way of implementing the »More fun with X-mas trees« exercise.
* Beginner's way of implementing the »More fun with X-mas trees« exercise. This solution just uses
* »bare-bone« print statements.
*/
public
class
Xmas
{
...
...
Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/XmasUsingFormat.java
View file @
c46bc861
...
...
@@ -2,7 +2,7 @@ package de.hdm_stuttgart.mi.sd1;
/**
* Simplified implementation replacing loops by {@link
java.io.PrintStream#format(String, Object...
)} and
* Simplified implementation replacing loops by {@link
String#indent(int
)} and
* {@link String#repeat(int)}.
*/
public
class
XmasUsingFormat
{
...
...
@@ -28,25 +28,24 @@ public class XmasUsingFormat {
final
int
numberOfRowGroups
=
5
;
// You may easily change this parameter.
// Printing the tree's top
System
.
out
.
format
(
"%"
+
(
numberOfRowGroups
+
5
)
+
"s\n"
,
"\\ /"
);
System
.
out
.
format
(
"%"
+
(
numberOfRowGroups
+
7
)
+
"s\n"
,
"-->*<--"
);
System
.
out
.
format
(
"%"
+
(
numberOfRowGroups
+
5
)
+
"s\n"
,
"/_\\"
);
// Printing the tree's top. We have to escape the backslash using \\.
System
.
out
.
print
(
"""
\\ /
-->*<--
/_\\"""
.
indent
(
numberOfRowGroups
));
// Printing the tree's body
// Loop printing the tree's body row group wise.
for
(
int
rowGroup
=
0
;
rowGroup
<
numberOfRowGroups
;
rowGroup
++)
{
final
int
indentation
=
numberOfRowGroups
-
rowGroup
+
1
;
// First body line of current group
System
.
out
.
format
(
"%"
+
(
numberOfRowGroups
+
rowGroup
+
6
)
+
"s\n"
,
"/"
+
"_\\"
.
repeat
(
rowGroup
+
2
));
System
.
out
.
print
((
'/'
+
"_\\"
.
repeat
(
rowGroup
+
2
)).
indent
(
indentation
));
// Second body line of current group
System
.
out
.
format
(
"%"
+
(
numberOfRowGroups
+
rowGroup
+
6
)
+
"s\\\n"
,
"/_"
.
repeat
(
rowGroup
+
3
));
System
.
out
.
print
((
"/_"
.
repeat
(
rowGroup
+
2
)
+
'\\'
).
indent
(
indentation
));
}
// Printing the tree's trunk
System
.
out
.
format
(
"%"
+
(
numberOfRowGroups
+
6
)
+
"s\n"
,
"[___]"
);
System
.
out
.
print
(
"[___]"
.
indent
(
numberOfRowGroups
+
1
)
);
}
}
Doc/Sd1/statements.xml
View file @
c46bc861
...
...
@@ -3045,7 +3045,8 @@ for (int row = 0; row < numberOfRows; row++) { <co
<answer>
<para>
We start coding the proposed helper exercise. Each line
has to be indented using space
<code>
' '
</code>
characters being
represented by
<quote>
␣
</quote>
in the right diagram:
</para>
represented by
<quote><emphasis
role=
"red"
>
␣
</emphasis></quote>
in the right diagram:
</para>
<informaltable
border=
"1"
>
<col
width=
"47%"
/>
...
...
@@ -3071,12 +3072,12 @@ for (int row = 0; row < numberOfRows; row++) {
<td
align=
"center"
>
⟹
</td>
<td
valign=
"top"
><screen>
␣␣␣␣␣␣
*
␣␣␣␣␣
**
␣␣␣␣
***
␣␣␣
****
␣␣
*****
␣
******
<td
valign=
"top"
><screen>
<emphasis
role=
"red"
>
␣␣␣␣␣␣
</emphasis>
*
<emphasis
role=
"red"
>
␣␣␣␣␣
</emphasis>
**
<emphasis
role=
"red"
>
␣␣␣␣
</emphasis>
***
<emphasis
role=
"red"
>
␣␣␣
</emphasis>
****
<emphasis
role=
"red"
>
␣␣
</emphasis>
*****
<emphasis
role=
"red"
>
␣
</emphasis>
******
*******
</screen></td>
</tr>
</informaltable>
...
...
@@ -3102,23 +3103,23 @@ Row index 5 ***********
<para>
We require the precise indentation values when
<abbrev>
e.g.
</abbrev>
starting the tree's body. The following
sketch shows two trees of different sizes representing invisible
spaces by
<quote>
␣
</quote>
. In the »bigger« tree's first line we
need
<abbrev>
e.g.
</abbrev>
5 spaces before actually printing the
tree's very top
<emphasis
role=
"red"
><code>
X
</code></emphasis>
</para>
spaces by
<quote>
<emphasis
role=
"red"
>
␣
</emphasis></quote>
. In
the »bigger« tree's first line we need
<abbrev>
e.g.
</abbrev>
5
spaces before actually printing the tree's very top
<code>
'X'
</code>
character.
</para>
<screen
language=
"none"
>
A tree with
A tree with
<screen
language=
"none"
>
A tree with A tree with
6 body rows 2 row groups
␣␣␣␣␣
<emphasis
role=
"red"
>
X
</emphasis>
␣␣␣
<emphasis
role=
"red"
>
X
</emphasis>
␣␣␣␣␣* ␣␣␣*
␣␣␣␣*** ␣␣***
␣␣␣***** ␣*****
␣␣
******* *******
␣********* ␣␣
<emphasis
role=
"red"
>
###
</emphasis>
<emphasis
role=
"red"
>
␣␣␣␣␣
</emphasis>
X
<emphasis
role=
"red"
>
␣␣␣
</emphasis>
X
<emphasis
role=
"red"
>
␣␣␣␣␣
</emphasis>
*
<emphasis
role=
"red"
>
␣␣␣
</emphasis>
*
<emphasis
role=
"red"
>
␣␣␣␣
</emphasis>
***
<emphasis
role=
"red"
>
␣␣
</emphasis>
***
<emphasis
role=
"red"
>
␣␣␣
</emphasis>
*****
<emphasis
role=
"red"
>
␣
</emphasis>
*****
<emphasis
role=
"red"
>
␣␣
</emphasis>
******* *******
<emphasis
role=
"red"
>
␣
</emphasis>
*********
<emphasis
role=
"red"
>
␣␣
</emphasis>
###
***********
␣␣␣␣
<emphasis
role=
"red"
>
###
</emphasis>
␣␣␣␣
<emphasis
role=
"red"
>
###
</emphasis>
</screen>
<emphasis
role=
"red"
>
␣␣␣␣
</emphasis>
###
<emphasis
role=
"red"
>
␣␣␣␣
</emphasis>
###
</screen>
<para>
The precise amounts of these indentations depend on the
tree's size. Printing larger trees requires larger indentation
...
...
@@ -3129,7 +3130,7 @@ Row index 5 ***********
<para>
We start printing the tree's top. This requires
<code
language=
"java"
>
numberOfRows
</code>
space characters followed by
the top's
<
emphasis
role=
"red"
>
X
</emphasis
>
character:
</para>
the top's
<
code
role=
"red"
>
'X'
</code
>
character:
</para>
<informaltable
border=
"1"
>
<col
width=
"80%"
/>
...
...
@@ -3147,11 +3148,11 @@ Row index 5 ***********
role=
"red"
>
X
</emphasis>
" top. We need
System.out.print(' '); // numberOfRowGroups preceding spaces (␣)
} // before eventually printing the
System.out.println(
"
<emphasis
role=
"red"
>
X
</emphasis>
"
); // "
<emphasis
System.out.println(
'
<emphasis
role=
"red"
>
X
</emphasis>
'
); // "
<emphasis
role=
"red"
>
X
</emphasis>
" String followed by a newline (print
<emphasis
role=
"red"
>
ln
</emphasis>
).
</programlisting></td>
<td
valign=
"top"
><screen>
␣␣␣␣␣
<emphasis
role=
"red"
>
X
</emphasis>
</screen><para>
(
<quote>
␣
</quote>
<td
valign=
"top"
><screen>
<emphasis
role=
"red"
>
␣␣␣␣␣
</emphasis>
X
</screen><para>
(
<quote>
␣
</quote>
denoting space)
</para></td>
</tr>
</informaltable>
...
...
@@ -3182,7 +3183,7 @@ System.out.println("<emphasis role="red">X</emphasis>");
System.out.print(' '); // (numberOfRows - row)
} // space (␣) characters ...
for (int x = 0; x
<
2 * row + 1; x ++) { // .. then printing (2
*row+
1)
for (int x = 0; x
<
2 * row + 1; x ++) { // .. then printing (2
* row +
1)
// asterisk('
<emphasis
role=
"red"
>
*
</emphasis>
') characters ...
System.out.print('
<emphasis
role=
"red"
>
*
</emphasis>
'); // (May try '
<emphasis
...
...
@@ -3191,28 +3192,13 @@ System.out.println("<emphasis role="red">X</emphasis>");
System.out.print("\n"); // ... and finally terminating the
} // current body row.
</programlisting></td>
<td
valign=
"top"
><screen>
␣␣␣␣␣X
␣␣␣␣␣
<emphasis
role=
"red"
>
*
</emphasis>
␣␣␣␣
<emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis>
␣␣␣
<emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis>
␣␣
<emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis>
␣
<emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis>
<emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis><emphasis
role=
"red"
>
*
</emphasis></screen></td>
<td
valign=
"top"
><screen><emphasis
role=
"red"
>
␣␣␣␣␣
</emphasis>
X
<emphasis
role=
"red"
>
␣␣␣␣␣
</emphasis>
*
<emphasis
role=
"red"
>
␣␣␣␣
</emphasis>
***
<emphasis
role=
"red"
>
␣␣␣
</emphasis>
*****
<emphasis
role=
"red"
>
␣␣
</emphasis>
*******
<emphasis
role=
"red"
>
␣
</emphasis>
*********
***********
</screen></td>
</tr>
</informaltable>
...
...
@@ -3426,11 +3412,11 @@ System.out.format("%"+ (numberOfRows + 1) + "s\n", "###");</programlisting>
</question>
<answer>
<para>
A complete solution is
available at the
<link
xlink:href=
"https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/Xmas.java"
>
MI
Gitlab
</link>
repository. We start again from a version being
fully covered by our current knowledge. This time we try
printing the following lines:
</para>
<para>
A complete solution is
provided by
<link
xlink:href=
"https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/Xmas.java"
>
Xmas.java
</link>
.
You may want to import the whole project. We start again from a
version being fully covered by our current knowledge. This time
we try
printing the following lines:
</para>
<programlisting
language=
"none"
>
Looping through
<emphasis
role=
"red"
>
\ /
</emphasis>
<emphasis
role=
"red"
>
Part 1: The tree's top.
</emphasis>
...
...
@@ -3724,40 +3710,38 @@ System.out.println("<emphasis role="red">[___]</emphasis>");
</tr>
</informaltable>
<para>
Like in
the previous exercise
<xref
linkend=
"sd1QandaXmasTree"
/>
we now replace all inner loops
by
<classname>
System
</classname><code>
.
</code><property>
out
</property><code>
.
</code>
<methodname
xlink:href=
"https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/
io/PrintStream.html#format(java.lang.String,java.lang.Object...)"
>
forma
t()
</methodname>
<para>
Similar to
the previous exercise
<xref
linkend=
"sd1QandaXmasTree"
/>
this time we replace all inner
loops by
<classname>
using
</classname><classname
xlink:href=
"https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html"
>
String
</classname>
.
<methodname
xlink:href=
"https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/
lang/String.html#indent(int)"
>
inden
t()
</methodname>
and
<classname
xlink:href=
"https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html"
>
String
</classname>
.
<methodname
xlink:href=
"https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html#repeat(int)"
>
repeat()
</methodname>
.
This variants full source code is on offer at
<link
This variant
'
s full source code is on offer at
<link
xlink:href=
"https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/Statements/MoreFunXmasTree/src/main/java/de/hdm_stuttgart/mi/sd1/XmasUsingFormat.java"
>
XmasUsingFormat.java
</link>
.
</para>
<programlisting
language=
"java"
>
final int numberOfRowGroups = 5;
// You may easily change this parameter.
<programlisting
language=
"java"
>
final int numberOfRowGroups = 5; // You may easily change this parameter.
// Printing the tree's top
System.out.format("%"+ (numberOfRowGroups + 5) + "s\n", "\\ /");
System.out.format("%"+ (numberOfRowGroups + 7) + "s\n", "--
>
*
<
--");
System.out.format("%"+ (numberOfRowGroups + 5) + "s\n", "/_\\");
// Printing the tree's top. We have to escape the backslash using \\.
System.out.print("""
\\ /
--
>
*
<
--
/_\\""".indent(numberOfRowGroups));
// Printing the tree's body
// Loop printing the tree's body row group wise.
for (int rowGroup = 0; rowGroup
<
numberOfRowGroups; rowGroup++) {
final int indentation = numberOfRowGroups - rowGroup + 1;
// First body line of current group
System.out.print(('/' + "_\\".repeat(rowGroup + 2)).indent(indentation));
// First body line of current group
System.out.format("%"+ (numberOfRowGroups + rowGroup + 6) + "s\n", "/" + "_\\".repeat(rowGroup + 2));
// Second body line of current group
System.out.format
("%"+ (numberOfRowGroups + rowGroup + 6) + "s\\\n",
"/_".repeat(rowGroup + 3));
}
// Second body line of current group
System.out.print(("/_".repeat(rowGroup + 2) + '\\').indent(indentation));
}
// Printing the tree's trunk
System.out.format("%"+ (numberOfRowGroups + 6) + "s\n", "[___]");
</programlisting>
System.out.print("[___]".indent(numberOfRowGroups + 1));
</programlisting>
<para>
Both implementation variants allow for setting e.g.
<code
language=
"java"
>
final int numberOfRowGroups = 8
</code>
creating
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment