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) {
<emphasis><emphasis role="bold">precisely</emphasis></emphasis> to
avoid a common pitfall.</para>
<para>Do you have a comment choosing the right type of
loop?</para>
<para>Which of these three loop implementations is your favourite?
Give a reason.</para>
</question>
<answer>
......@@ -5023,10 +5023,10 @@ public static void doWhileSquareNumbers(final int limit) {
implement:</para>
<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
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");
int i = 4;
while (i &lt; limit) {
......@@ -5041,7 +5041,7 @@ public static void doWhileSquareNumbers(final int limit) {
<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");
int i = 4;
do {
......@@ -5051,15 +5051,17 @@ public static void doWhileSquareNumbers(final int limit) {
System.out.println("Finished computing square numbers");
} ...</programlisting>
<para>This implementation however is flawed: If we call
doWhile_squareNumbers(3) we still receive one line of output. But
according to the documentation no output is to be expected.
Whatever the argument is, at least one output line gets printed.
To avoid this error the loop has to be enclosed by an if-
<para>This implementation is however flawed: If we call
<methodname>doWhileSquareNumbers(3)</methodname> we still receive
one line of output. But according to the documentation no output
is to be expected. Whatever the argument is, at least one line of
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>
<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");
int i = 4;
if (i &lt; limit) { // Needed !!!
......@@ -5071,16 +5073,28 @@ public static void doWhileSquareNumbers(final int limit) {
System.out.println("Finished computing square numbers");
}</programlisting>
<para>This required if-clause reminds us that a <code
language="java">do {...} while (...)</code> is an ill-suited
choice here in comparison to a <code
<para>This <code language="java">if</code>-clause reminds us that
a <code language="java">do {...} while (...)</code> is an
ill-suited choice here: Both 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
best choice here since the number of iterations is known in
advance, the increment is constant and it allows for
initialization</code>.</para>
<para>Actually a <code language="java">for(...){...} loop is our
best choice here since:</code><orderedlist>
<listitem>
<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>
</qandaentry>
</qandadiv>
......
......@@ -5,8 +5,8 @@ public class LoopExample {
public static void main(String [] args) {
final int upperLimit = 14;
while_squareNumbers(upperLimit);
doWhile_squareNumbers(upperLimit);
whileSquareNumbers(upperLimit);
doWhileSquareNumbers(upperLimit);
}
/**
......@@ -16,7 +16,7 @@ public class LoopExample {
*
* @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");
int i = 4;
while (i < limit) {
......@@ -33,10 +33,10 @@ public class LoopExample {
*
* @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");
int i = 4;
if (i < limit) { // Needed !!!
if (i < limit) { // Required !!!
do {
System.out.println("i = " + i + ", i * i = " + i * i);
i += 3;
......
......@@ -183,19 +183,22 @@
<itemizedlist>
<listitem>
<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/{.}">
<xsl:value-of select="."/>
</filename>
<xsl:text> below lecture notes' source code root, see </xsl:text>
<link linkend="sd1ImportMavenSolutions"> hints regarding import</link>
<xsl:text>.</xsl:text>
</para>
</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>
</itemizedlist>
</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