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

Cosmetics

parent 6dc1158e
...@@ -5010,8 +5010,8 @@ public static void doWhileSquareNumbers(final int limit) { ...@@ -5010,8 +5010,8 @@ public static void doWhileSquareNumbers(final int limit) {
<emphasis><emphasis role="bold">precisely</emphasis></emphasis> to <emphasis><emphasis role="bold">precisely</emphasis></emphasis> to
avoid a common pitfall.</para> avoid a common pitfall.</para>
<para>Do you have a comment choosing the right type of <para>Which of these three loop implementations is your favourite?
loop?</para> Give a reason.</para>
</question> </question>
<answer> <answer>
...@@ -5023,10 +5023,10 @@ public static void doWhileSquareNumbers(final int limit) { ...@@ -5023,10 +5023,10 @@ public static void doWhileSquareNumbers(final int limit) {
implement:</para> implement:</para>
<programlisting language="java">public class <link <programlisting language="java">public class <link
xlink:href="P/Sd1/loop/answer/target/site/apidocs/de/hdm_stuttgart/sd1/loop/LoopExample.html">LoopExample</link> { xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/-/blob/master/P/Sd1/loop/answer/src/main/java/de/hdm_stuttgart/sd1/loop/LoopExample.java">LoopExample</link> {
... ...
public static void <link public static void <link
xlink:href="P/Sd1/loop/answer/target/site/apidocs/de/hdm_stuttgart/sd1/loop/LoopExample.html#doWhile_squareNumbers-int-">while_squareNumbers(final int limit)</link> { xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/-/blob/master/P/Sd1/loop/answer/src/main/java/de/hdm_stuttgart/sd1/loop/LoopExample.java#L19">whileSquareNumbers(final int limit)</link> {
System.out.println("Computing square numbers"); System.out.println("Computing square numbers");
int i = 4; int i = 4;
while (i &lt; limit) { while (i &lt; limit) {
...@@ -5041,7 +5041,7 @@ public static void doWhileSquareNumbers(final int limit) { ...@@ -5041,7 +5041,7 @@ public static void doWhileSquareNumbers(final int limit) {
<programlisting language="java">public class LoopExample { <programlisting language="java">public class LoopExample {
... ...
public static void doWhile_squareNumbers(final int limit) { public static void doWhileSquareNumbers(final int limit) {
System.out.println("Computing square numbers"); System.out.println("Computing square numbers");
int i = 4; int i = 4;
do { do {
...@@ -5051,15 +5051,17 @@ public static void doWhileSquareNumbers(final int limit) { ...@@ -5051,15 +5051,17 @@ public static void doWhileSquareNumbers(final int limit) {
System.out.println("Finished computing square numbers"); System.out.println("Finished computing square numbers");
} ...</programlisting> } ...</programlisting>
<para>This implementation however is flawed: If we call <para>This implementation is however flawed: If we call
doWhile_squareNumbers(3) we still receive one line of output. But <methodname>doWhileSquareNumbers(3)</methodname> we still receive
according to the documentation no output is to be expected. one line of output. But according to the documentation no output
Whatever the argument is, at least one output line gets printed. is to be expected. Whatever the argument is, at least one line of
To avoid this error the loop has to be enclosed by an if- output is being printed contradicting the method's <xref
linkend="glo_Javadoc"/>. Avoiding this error requires the loop to
be enclosed in an <code language="java">if</code>-
statement:</para> statement:</para>
<programlisting language="java">public static void <link <programlisting language="java">public static void <link
xlink:href="P/Sd1/loop/answer/target/site/apidocs/de/hdm_stuttgart/sd1/loop/LoopExample.html#doWhile_squareNumbers-int-">doWhile_squareNumbers</link>(final int limit) { xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/-/blob/master/P/Sd1/loop/answer/src/main/java/de/hdm_stuttgart/sd1/loop/LoopExample.java#L36">doWhileSquareNumbers(final int limit)</link>(final int limit) {
System.out.println("Computing square numbers"); System.out.println("Computing square numbers");
int i = 4; int i = 4;
if (i &lt; limit) { // Needed !!! if (i &lt; limit) { // Needed !!!
...@@ -5071,16 +5073,28 @@ public static void doWhileSquareNumbers(final int limit) { ...@@ -5071,16 +5073,28 @@ public static void doWhileSquareNumbers(final int limit) {
System.out.println("Finished computing square numbers"); System.out.println("Finished computing square numbers");
}</programlisting> }</programlisting>
<para>This required if-clause reminds us that a <code <para>This <code language="java">if</code>-clause reminds us that
language="java">do {...} while (...)</code> is an ill-suited a <code language="java">do {...} while (...)</code> is an
choice here in comparison to a <code ill-suited choice here: Both a <code
language="java">while(...){...}</code> or a <code language="java">while(...){...}</code> or a <code
language="java">for(...){...}</code> loop.</para> language="java">for(...){...}</code> loop avoid this problem in
the first place rather than requiring its symptoms to be
cured.</para>
<para>Actually a <code language="java">for(...){...} loop is the <para>Actually a <code language="java">for(...){...} loop is our
best choice here since the number of iterations is known in best choice here since:</code><orderedlist>
advance, the increment is constant and it allows for <listitem>
initialization</code>.</para> <para>The number of iterations is known in advance.</para>
</listitem>
<listitem>
<para>The increment is constant.</para>
</listitem>
<listitem>
<para>It allows for prior initialization.</para>
</listitem>
</orderedlist>.</para>
</answer> </answer>
</qandaentry> </qandaentry>
</qandadiv> </qandadiv>
......
...@@ -5,8 +5,8 @@ public class LoopExample { ...@@ -5,8 +5,8 @@ public class LoopExample {
public static void main(String [] args) { public static void main(String [] args) {
final int upperLimit = 14; final int upperLimit = 14;
while_squareNumbers(upperLimit); whileSquareNumbers(upperLimit);
doWhile_squareNumbers(upperLimit); doWhileSquareNumbers(upperLimit);
} }
/** /**
...@@ -16,7 +16,7 @@ public class LoopExample { ...@@ -16,7 +16,7 @@ public class LoopExample {
* *
* @param limit An upper exclusive bound for the highest number to be squared.. * @param limit An upper exclusive bound for the highest number to be squared..
*/ */
public static void while_squareNumbers(final int limit) { public static void whileSquareNumbers(final int limit) {
System.out.println("Computing square numbers"); System.out.println("Computing square numbers");
int i = 4; int i = 4;
while (i < limit) { while (i < limit) {
...@@ -33,10 +33,10 @@ public class LoopExample { ...@@ -33,10 +33,10 @@ public class LoopExample {
* *
* @param limit An upper exclusive bound for the highest number to be squared.. * @param limit An upper exclusive bound for the highest number to be squared..
*/ */
public static void doWhile_squareNumbers(final int limit) { public static void doWhileSquareNumbers(final int limit) {
System.out.println("Computing square numbers"); System.out.println("Computing square numbers");
int i = 4; int i = 4;
if (i < limit) { // Needed !!! if (i < limit) { // Required !!!
do { do {
System.out.println("i = " + i + ", i * i = " + i * i); System.out.println("i = " + i + ", i * i = " + i * i);
i += 3; i += 3;
......
...@@ -183,19 +183,22 @@ ...@@ -183,19 +183,22 @@
<itemizedlist> <itemizedlist>
<listitem> <listitem>
<para> <para>
<xsl:text>Maven module source code available at sub directory </xsl:text> <xsl:text>Maven module source code available at </xsl:text>
<filename xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/{.}"> <filename xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/{.}">
<xsl:value-of select="."/> <xsl:value-of select="."/>
</filename> </filename>
<xsl:text> below lecture notes' source code root, see </xsl:text>
<link linkend="sd1ImportMavenSolutions"> hints regarding import</link>
<xsl:text>.</xsl:text> <xsl:text>.</xsl:text>
</para> </para>
</listitem> </listitem>
<listitem> <listitem>
<para>Online browsing of <link xlink:href="{.}/target/site/apidocs">API and implementation</link>.</para> <para>
<xsl:text>See </xsl:text>
<link linkend="sd1ImportMavenSolutions"> hints regarding import</link>
<xsl:text>.</xsl:text>
</para>
</listitem> </listitem>
</itemizedlist> </itemizedlist>
</xsl:template> </xsl:template>
......
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