Commit c46bc861 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Java 15 version of Ascii Xmas tree

parent 30b37a97
......@@ -25,8 +25,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<source>15</source>
<target>15</target>
</configuration>
</plugin>
......
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 {
......
......@@ -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));
}
}
......@@ -3045,7 +3045,8 @@ for (int row = 0; row &lt; 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 &lt; 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
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 &lt; 2 * row + 1; x ++) { // .. then printing (2*row+1)
for (int x = 0; x &lt; 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...)">format()</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)">indent()</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.
// Printing the tree's top
System.out.format("%"+ (numberOfRowGroups + 5) + "s\n", "\\ /");
System.out.format("%"+ (numberOfRowGroups + 7) + "s\n", "--&gt;*&lt;--");
System.out.format("%"+ (numberOfRowGroups + 5) + "s\n", "/_\\");
// Printing the tree's top. We have to escape the backslash using \\.
System.out.print("""
\\ /
--&gt;*&lt;--
/_\\""".indent(numberOfRowGroups));
// Printing the tree's body
// Loop printing the tree's body row group wise.
for (int rowGroup = 0; rowGroup &lt; 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", "[___]");
</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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment