Commit 5e727775 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Mjulti-line hint in second Xmas tree exercise

parent c46bc861
......@@ -16,7 +16,7 @@ public class App {
<head>
<title>A square table</title>
<style>
table,td,th {
table, td, th {
border: 1px solid black;
}
table {
......
......@@ -3407,17 +3407,49 @@ System.out.format("%"+ (numberOfRows + 1) + "s\n", "###");</programlisting>
xlink:href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html#repeat(int)">repeat(int)</methodname>
in the previous exercises solution.</para>
</listitem>
<listitem>
<para>The <methodname
xlink:href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html#indent(int)">indent(int)</methodname>
method allows for indenting multi line blocks:</para>
<informaltable border="1">
<tr>
<th>Code</th>
<th>Result ('␣' denoting space)</th>
</tr>
<tr>
<td valign="top"><programlisting language="none">final String block = """
<emphasis role="red">A simple block
consisting of
three lines</emphasis>""";
System.out.println(block);
System.out.println(
"\n------------------------\n");
System.out.println(block.indent(10));</programlisting></td>
<td valign="top"><screen><emphasis role="red">A simple block
consisting of
three lines</emphasis>
------------------------
␣␣␣␣␣␣␣␣␣␣<emphasis role="red">A simple block</emphasis>
␣␣␣␣␣␣␣␣␣␣<emphasis role="red">consisting of</emphasis>
␣␣␣␣␣␣␣␣␣␣<emphasis role="red">three lines</emphasis></screen></td>
</tr>
</informaltable>
</listitem>
</orderedlist>
</tip>
</question>
<answer>
<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>
groups of 2 lines <emphasis role="red">--&gt;*&lt;--</emphasis>
......@@ -3990,7 +4022,7 @@ System.out.format("Start:<emphasis role="red">%5d</emphasis>:End", <emphasis
&lt;!-- CSS based styling --&gt;
&lt;style&gt;
table,td,th {
table, td, th {
border: 1px solid black;
}
table {
......@@ -4030,29 +4062,7 @@ System.out.format("Start:<emphasis role="red">%5d</emphasis>:End", <emphasis
This solution uses both <xref linkend="glo_Java"/> 15 multi
line blocks and the String.<link
xlink:href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html#indent(int)">indent(..)</link>
method. Consider the following example:</para>
<programlisting language="java">final String block = """
A simple block
consisting of
three lines""";
System.out.println(block);
System.out.println("\nAgain using indentation of 10 spaces:");
System.out.println(block.indent(10));
</programlisting>
<para>This creates:</para>
<screen>A simple block
consisting of
three lines
Again using indentation of 10 spaces:
A simple block
consisting of
three lines</screen>
method.</para>
</answer>
</qandaentry>
</qandadiv>
......
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