diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index b3b079c2b062173bd0bedaac5e496815044b524c..b6c4338754d1c27f747881191cc2a16fff14fbd4 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -221,26 +221,29 @@ System.out.println("Done!");</programlisting></td> (block | statement)</programlisting> </figure> - <figure xml:id="sd1_fig_ifElse"> - <title><code language="java">if</code> ... <code - language="java">else</code> block execution selection</title> + <section xml:id="sd1_sect_else"> + <title>if-then-<code>else</code></title> - <informaltable border="1"> - <colgroup width="37%"/> + <figure xml:id="sd1_fig_ifElse"> + <title><code language="java">if</code> ... <code + language="java">else</code> block execution selection</title> - <colgroup width="63%"/> + <informaltable border="1"> + <colgroup width="37%"/> - <tr> - <td valign="top"><programlisting language="java">double saving = 320.00; + <colgroup width="63%"/> + + <tr> + <td valign="top"><programlisting language="java">double saving = 320.00; if (1000 <= saving <co linkends="sd1_fig_ifElse-1" - xml:id="sd1_fig_ifElse-1-co"/>) { <co - linkends="sd1_fig_ifElse-2" xml:id="sd1_fig_ifElse-2-co"/> + xml:id="sd1_fig_ifElse-1-co"/>) { <co + linkends="sd1_fig_ifElse-2" xml:id="sd1_fig_ifElse-2-co"/> // Rich customer, 1,2% interest rate System.out.println( "Interest:" + 1.2 * saving / 100); } <co linkends="sd1_fig_ifElse-3" xml:id="sd1_fig_ifElse-3-co"/> else { <co - linkends="sd1_fig_ifElse-4" xml:id="sd1_fig_ifElse-4-co"/> + linkends="sd1_fig_ifElse-4" xml:id="sd1_fig_ifElse-4-co"/> // Joe customer, 0.8% standard interest rate System.out.println( "Interest:" + 0.8 * saving / 100); @@ -248,134 +251,137 @@ if (1000 <= saving <co linkends="sd1_fig_ifElse-1" System.out.println("Done!");</programlisting><screen>Interest:2.56 Done!</screen></td> - <td valign="top"><mediaobject> - <imageobject> - <imagedata fileref="Ref/Statements/ifElse.svg"/> - </imageobject> - </mediaobject></td> - </tr> - </informaltable> + <td valign="top"><mediaobject> + <imageobject> + <imagedata fileref="Ref/Statements/ifElse.svg"/> + </imageobject> + </mediaobject></td> + </tr> + </informaltable> - <calloutlist role="slideExclude"> - <callout arearefs="sd1_fig_ifElse-1-co" xml:id="sd1_fig_ifElse-1"> - <para>Decision on boolean expression.</para> - </callout> - - <callout arearefs="sd1_fig_ifElse-2-co" xml:id="sd1_fig_ifElse-2"> - <para>Conditional execution of on block or single statement.</para> - </callout> - - <callout arearefs="sd1_fig_ifElse-3-co" xml:id="sd1_fig_ifElse-3"> - <para>Required branch being executed in case of boolean expression - being true.</para> - </callout> - - <callout arearefs="sd1_fig_ifElse-4-co" xml:id="sd1_fig_ifElse-4"> - <para>Optional branch corresponding to boolean expression being - false.</para> - </callout> - </calloutlist> - </figure> + <calloutlist role="slideExclude"> + <callout arearefs="sd1_fig_ifElse-1-co" xml:id="sd1_fig_ifElse-1"> + <para>Decision on boolean expression.</para> + </callout> - <figure xml:id="sd1_fig_ifElseSyntax"> - <title><code language="java">if ... else</code> execution syntax</title> + <callout arearefs="sd1_fig_ifElse-2-co" xml:id="sd1_fig_ifElse-2"> + <para>Conditional execution of on block or single + statement.</para> + </callout> - <programlisting language="java">if (booleanExpression) + <callout arearefs="sd1_fig_ifElse-3-co" xml:id="sd1_fig_ifElse-3"> + <para>Required branch being executed in case of boolean expression + being true.</para> + </callout> + + <callout arearefs="sd1_fig_ifElse-4-co" xml:id="sd1_fig_ifElse-4"> + <para>Optional branch corresponding to boolean expression being + false.</para> + </callout> + </calloutlist> + </figure> + + <figure xml:id="sd1_fig_ifElseSyntax"> + <title><code language="java">if ... else</code> execution + syntax</title> + + <programlisting language="java">if (booleanExpression) (block | statement) [else (block | statement) ] <co linkends="sd1_fig_ifElseSyntax-1" - xml:id="sd1_fig_ifElseSyntax-1-co"/></programlisting> - - <calloutlist role="slideExclude"> - <callout arearefs="sd1_fig_ifElseSyntax-1-co" - xml:id="sd1_fig_ifElseSyntax-1"> - <para>The <quote>[...]</quote> pair of braces denotes an optional - clause that may or may not be present.</para> - - <para>Thus only the first part <quote><code language="java">if - (booleanExpression) (block | statement)</code></quote> is - mandatory.</para> - </callout> - </calloutlist> - </figure> + xml:id="sd1_fig_ifElseSyntax-1-co"/></programlisting> + + <calloutlist role="slideExclude"> + <callout arearefs="sd1_fig_ifElseSyntax-1-co" + xml:id="sd1_fig_ifElseSyntax-1"> + <para>The <quote>[...]</quote> pair of braces denotes an optional + clause that may or may not be present.</para> + + <para>Thus only the first part <quote><code language="java">if + (booleanExpression) (block | statement)</code></quote> is + mandatory.</para> + </callout> + </calloutlist> + </figure> - <figure xml:id="sd1_fig_bestPracticeCompareEquals"> - <title>Best practices comparing for equality</title> + <figure xml:id="sd1_fig_bestPracticeCompareEquals"> + <title>Best practices comparing for equality</title> - <para>Always use</para> + <para>Always use</para> - <programlisting language="java">if (4 == variable) ...</programlisting> + <programlisting language="java">if (4 == variable) ...</programlisting> - <para>in favour of:</para> + <para>in favour of:</para> - <programlisting language="java">if (variable == 4) ...</programlisting> + <programlisting language="java">if (variable == 4) ...</programlisting> - <para>See <link - xlink:href="sd1_sect_statements_if.html#sd1_note_useLiteralEqualsVariable">detailed - explanation</link>.</para> - </figure> + <para>See <link + xlink:href="sd1_sect_statements_if.html#sd1_note_useLiteralEqualsVariable">detailed + explanation</link>.</para> + </figure> - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_betterSimpleMath"> - <title>Providing better display</title> + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_betterSimpleMath"> + <title>Providing better display</title> - <qandadiv> - <qandaentry> - <question> - <para>We reconsider <xref linkend="sd1_qanda_simpleMath"/>:</para> + <qandadiv> + <qandaentry> + <question> + <para>We reconsider <xref + linkend="sd1_qanda_simpleMath"/>:</para> - <informaltable border="0"> - <tr> - <th>Source code</th> + <informaltable border="0"> + <tr> + <th>Source code</th> - <th>Output</th> - </tr> + <th>Output</th> + </tr> - <tr> - <td valign="top"><programlisting language="java">int a = -4, + <tr> + <td valign="top"><programlisting language="java">int a = -4, b = 100; System.out.println(a + "+" + b+ "=" + (a + b));</programlisting></td> - <td valign="top"><screen>-4+100=96</screen></td> - </tr> - </informaltable> + <td valign="top"><screen>-4+100=96</screen></td> + </tr> + </informaltable> - <para>Unfortunately changing the two variables' values - yields:</para> + <para>Unfortunately changing the two variables' values + yields:</para> - <informaltable border="0"> - <tr> - <th>Source code</th> + <informaltable border="0"> + <tr> + <th>Source code</th> - <th>Output</th> - </tr> + <th>Output</th> + </tr> - <tr> - <td valign="top"><programlisting language="java">int a = 100, + <tr> + <td valign="top"><programlisting language="java">int a = 100, b = -4; System.out.println(a + "+" + b + "=" + (a + b));</programlisting></td> - <td valign="top"><screen>100+-4=96</screen></td> - </tr> - </informaltable> + <td valign="top"><screen>100+-4=96</screen></td> + </tr> + </informaltable> - <para>This result looks awkward. Modify the code to see - <code>100-4=96</code> in such cases.</para> - </question> + <para>This result looks awkward. Modify the code to see + <code>100-4=96</code> in such cases.</para> + </question> - <answer> - <para>The following simple solution does not work:</para> + <answer> + <para>The following simple solution does not work:</para> - <informaltable border="0"> - <tr> - <th>Source code</th> + <informaltable border="0"> + <tr> + <th>Source code</th> - <th>Output</th> - </tr> + <th>Output</th> + </tr> - <tr> - <td valign="top"><programlisting language="java">int a = 100, + <tr> + <td valign="top"><programlisting language="java">int a = 100, b = -4; if (b < 0) { @@ -384,159 +390,161 @@ if (b < 0) { System.out.println(a + "+" + b + "=" + (a + b)); }</programlisting></td> - <td valign="top"><screen>96=96</screen></td> - </tr> - </informaltable> - - <para>Since <code language="java">a</code> and <code - language="java">b</code> are both variables of type <code - language="java">int</code> they get added rather than string style - concatenated. Resolving this issue may be effected by adding an - empty string <coref - linkend="sd1_qanda_betterSimpleMathEmptyString-1-co"/> forcing - <xref linkend="glo_Java"/> to use the concatenation - <quote>+</quote> operator in favour of the arithmetic one:</para> + <td valign="top"><screen>96=96</screen></td> + </tr> + </informaltable> - <informaltable border="0"> - <tr> - <th>Source code</th> + <para>Since <code language="java">a</code> and <code + language="java">b</code> are both variables of type <code + language="java">int</code> they get added rather than string + style concatenated. Resolving this issue may be effected by + adding an empty string <coref + linkend="sd1_qanda_betterSimpleMathEmptyString-1-co"/> forcing + <xref linkend="glo_Java"/> to use the concatenation + <quote>+</quote> operator in favour of the arithmetic + one:</para> + + <informaltable border="0"> + <tr> + <th>Source code</th> - <th>Output</th> - </tr> + <th>Output</th> + </tr> - <tr> - <td valign="top"><programlisting language="java">int a = 100, + <tr> + <td valign="top"><programlisting language="java">int a = 100, b = -4; if (b < 0) { System.out.println(a + "" <co - xml:id="sd1_qanda_betterSimpleMathEmptyString-1-co"/> + b+ "=" + (a + b)); + xml:id="sd1_qanda_betterSimpleMathEmptyString-1-co"/> + b+ "=" + (a + b)); } else { System.out.println(a + "+" + b + "=" + (a + b)); }</programlisting></td> - <td valign="top"><screen>100-4=96</screen></td> - </tr> - </informaltable> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + <td valign="top"><screen>100-4=96</screen></td> + </tr> + </informaltable> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_booleanEqual"> - <title>Comparing for equality</title> + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_booleanEqual"> + <title>Comparing for equality</title> - <qandadiv> - <qandaentry> - <question> - <para>Copy the following snippet into your <xref - linkend="glo_IDE"/>:</para> + <qandadiv> + <qandaentry> + <question> + <para>Copy the following snippet into your <xref + linkend="glo_IDE"/>:</para> - <programlisting language="java">int count = 1; + <programlisting language="java">int count = 1; if (count = 4) { // is count equal to 4? System.out.println("count is o.K."); }</programlisting> - <para>The <xref linkend="glo_Java"/> compiler will indicate an - error:</para> + <para>The <xref linkend="glo_Java"/> compiler will indicate an + error:</para> - <screen>Incompatible types. + <screen>Incompatible types. Required: <emphasis role="red">boolean</emphasis> Found: <emphasis role="red">int</emphasis></screen> - <para>Explain its cause in detail by examining the underlying - expression.</para> - - <tip> - <para><xref linkend="glo_Java"/> provides two similar looking - operators <code language="java">=</code> and <code - language="java">==</code> having (totally) different - semantics.</para> - </tip> - </question> - - <answer> - <para>Java provides two seemingly similar but - <emphasis>completely</emphasis> unrelated operators:</para> - - <glosslist> - <glossentry> - <glossterm><code language="java">=</code></glossterm> - - <glossdef> - <para>This is being called the assignment operator. A - typical statement reads <code language="java">a = 34</code> - assigning the <code language="java">int</code> value <code - language="java">34</code> to a variable <code - language="java">a</code>.</para> + <para>Explain its cause in detail by examining the underlying + expression.</para> - <para>Note this operator's semantics being completely - different from even elementary math syntax. Consider:</para> + <tip> + <para><xref linkend="glo_Java"/> provides two similar looking + operators <code language="java">=</code> and <code + language="java">==</code> having (totally) different + semantics.</para> + </tip> + </question> - <informalequation> - <m:math display="block"> - <m:mrow> - <m:mi>x</m:mi> + <answer> + <para>Java provides two seemingly similar but + <emphasis>completely</emphasis> unrelated operators:</para> - <m:mo>=</m:mo> + <glosslist> + <glossentry> + <glossterm><code language="java">=</code></glossterm> - <m:mi>y</m:mi> - </m:mrow> + <glossdef> + <para>This is being called the assignment operator. A + typical statement reads <code language="java">a = + 34</code> assigning the <code language="java">int</code> + value <code language="java">34</code> to a variable <code + language="java">a</code>.</para> + + <para>Note this operator's semantics being completely + different from even elementary math syntax. + Consider:</para> + + <informalequation> + <m:math display="block"> + <m:mrow> + <m:mi>x</m:mi> - <m:mo>⇒</m:mo> + <m:mo>=</m:mo> - <m:mrow> - <m:msup> - <m:mi>x</m:mi> + <m:mi>y</m:mi> + </m:mrow> - <m:mi>2</m:mi> - </m:msup> + <m:mo>⇒</m:mo> - <m:mo>=</m:mo> + <m:mrow> + <m:msup> + <m:mi>x</m:mi> - <m:msup> - <m:mi>y</m:mi> + <m:mi>2</m:mi> + </m:msup> - <m:mi>2</m:mi> - </m:msup> - </m:mrow> - </m:math> - </informalequation> + <m:mo>=</m:mo> - <para>In math <quote>=</quote> denotes the equality of - objects <abbrev>e.g.</abbrev> values, sets, functions and so - on.</para> - </glossdef> - </glossentry> + <m:msup> + <m:mi>y</m:mi> - <glossentry> - <glossterm><code language="java">==</code></glossterm> + <m:mi>2</m:mi> + </m:msup> + </m:mrow> + </m:math> + </informalequation> - <glossdef> - <para>The comparison operator matching the usual math - semantics comparing:</para> + <para>In math <quote>=</quote> denotes the equality of + objects <abbrev>e.g.</abbrev> values, sets, functions and + so on.</para> + </glossdef> + </glossentry> - <itemizedlist> - <listitem> - <para><xref linkend="glo_Java"/> primitive types for - equality of value.</para> - </listitem> + <glossentry> + <glossterm><code language="java">==</code></glossterm> - <listitem> - <para><xref linkend="glo_Java"/> objects for - identity.</para> - </listitem> - </itemizedlist> - </glossdef> - </glossentry> - </glosslist> + <glossdef> + <para>The comparison operator matching the usual math + semantics comparing:</para> + + <itemizedlist> + <listitem> + <para><xref linkend="glo_Java"/> primitive types for + equality of value.</para> + </listitem> + + <listitem> + <para><xref linkend="glo_Java"/> objects for + identity.</para> + </listitem> + </itemizedlist> + </glossdef> + </glossentry> + </glosslist> - <para>Thus <code language="java">(count = 4)</code> is an - expression evaluating to 4. So the code in question may be - re-written as:</para> + <para>Thus <code language="java">(count = 4)</code> is an + expression evaluating to 4. So the code in question may be + re-written as:</para> - <programlisting language="java">int count = 1; + <programlisting language="java">int count = 1; int countAssignment = (count = 4); // Assigning expression count = 4 to variable countAssignment @@ -544,49 +552,49 @@ if (countAssignment) { <emphasis role="red">// Error: An int is not a boolean!</ System.out.println("count is o.K."); }</programlisting> - <para>Since the assignment operator is being evaluated from right - to left we do not need braces:</para> + <para>Since the assignment operator is being evaluated from + right to left we do not need braces:</para> - <programlisting language="java">... + <programlisting language="java">... int countAssignment = count = 4; // Assigning expression count = 4 to variable countAssignment ...</programlisting> - <para>This code is equivalent to its counterpart with respect to - compilation. The comment <quote>is count equal to 4?</quote> is - thus misleading: The intended comparison requires using the - <quote>==</quote> operator rather than an assignment operator - <quote>=</quote>. Changing it the resulting expression is indeed - of type <code language="java">boolean</code>:</para> + <para>This code is equivalent to its counterpart with respect to + compilation. The comment <quote>is count equal to 4?</quote> is + thus misleading: The intended comparison requires using the + <quote>==</quote> operator rather than an assignment operator + <quote>=</quote>. Changing it the resulting expression is indeed + of type <code language="java">boolean</code>:</para> - <programlisting language="java">int count = 4 + 3; + <programlisting language="java">int count = 4 + 3; final boolean test = (count == 4); // Now using "==" (comparison) in favour of "=" (assignment) System.out.println("test=" + test);</programlisting> - <para>Again we may omit braces here:</para> + <para>Again we may omit braces here:</para> - <programlisting language="java">... + <programlisting language="java">... final boolean test = count == 4; // Now using "==" (comparison) in favour of "=" (assignment) ...</programlisting> - <para>The <code language="java">boolean</code> variable <code - language="java">test</code> will receive a value of <code - language="java">false</code> as expected. Thus our initial code - just needs a tiny modification replacing the assignment operator - «<code language="java">=</code>» by the comparison operator «<code - language="java">==</code>»:</para> + <para>The <code language="java">boolean</code> variable <code + language="java">test</code> will receive a value of <code + language="java">false</code> as expected. Thus our initial code + just needs a tiny modification replacing the assignment operator + «<code language="java">=</code>» by the comparison operator + «<code language="java">==</code>»:</para> - <programlisting language="java">int count = 1; + <programlisting language="java">int count = 1; if (count == 4) { // is count equal to 4? System.out.println("count is o.K."); }</programlisting> - <note xml:id="sd1_note_useLiteralEqualsVariable"> - <para>In contrast to <xref linkend="glo_Java"/> some programming - languages like C and C++ allow for integer values in <code - language="java">if (...)</code> conditionals:</para> + <note xml:id="sd1_note_useLiteralEqualsVariable"> + <para>In contrast to <xref linkend="glo_Java"/> some + programming languages like C and C++ allow for integer values + in <code language="java">if (...)</code> conditionals:</para> - <programlisting language="c">#include <stdio.h> + <programlisting language="c">#include <stdio.h> int main(int argc, char **args) { @@ -596,63 +604,63 @@ int main(int argc, char **args) { } }</programlisting> - <para>The integer expression <code language="java">count = - 4</code> has got a value of 4. Integer values inside an <code - language="java">if (...)</code> statement will be evaluated - as:</para> + <para>The integer expression <code language="java">count = + 4</code> has got a value of 4. Integer values inside an <code + language="java">if (...)</code> statement will be evaluated + as:</para> - <glosslist> - <glossentry> - <glossterm>true</glossterm> + <glosslist> + <glossentry> + <glossterm>true</glossterm> - <glossdef> - <para>if the expression's value differs from zero</para> - </glossdef> - </glossentry> + <glossdef> + <para>if the expression's value differs from zero</para> + </glossdef> + </glossentry> - <glossentry> - <glossterm>false</glossterm> + <glossentry> + <glossterm>false</glossterm> - <glossdef> - <para>if the expression's value equals zero</para> - </glossdef> - </glossentry> - </glosslist> + <glossdef> + <para>if the expression's value equals zero</para> + </glossdef> + </glossentry> + </glosslist> - <para>Thus in C and C++ the expression <code - language="java">if(count = 4)</code> will always evaluate to - <code language="java">true</code> irrespective of the variable - <code language="java">count</code>'s initial value. Most - important: The C compiler will not issue an error or warning - unless non-default, more restrictive compile time warning - options are being activated. Consider this widely used «feature» - to be dangerous at best.</para> - - <para>For this reason it is good practice always using <code - language="java">if (4 == count)</code> rather than <code - language="java">if (count == 4)</code>: Even in C you cannot - assign a value to a constant literal. Thus an accidentally - mistyped <code language="java">if (4 = count)</code> statement - will definitively result in a compile time error most likely - saving its author from tedious debugging.</para> - </note> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + <para>Thus in C and C++ the expression <code + language="java">if(count = 4)</code> will always evaluate to + <code language="java">true</code> irrespective of the variable + <code language="java">count</code>'s initial value. Most + important: The C compiler will not issue an error or warning + unless non-default, more restrictive compile time warning + options are being activated. Consider this widely used + «feature» to be dangerous at best.</para> + + <para>For this reason it is good practice always using <code + language="java">if (4 == count)</code> rather than <code + language="java">if (count == 4)</code>: Even in C you cannot + assign a value to a constant literal. Thus an accidentally + mistyped <code language="java">if (4 = count)</code> statement + will definitively result in a compile time error most likely + saving its author from tedious debugging.</para> + </note> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_replaceElseIf"> - <title>Replacing <code language="java">else if (...){...}</code> by - nested <code language="java">if ... else</code> statements</title> + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_replaceElseIf"> + <title>Replacing <code language="java">else if (...){...}</code> by + nested <code language="java">if ... else</code> statements</title> - <qandadiv> - <qandaentry> - <question> - <para>A computer newbie did not yet read about the <code - language="java">else if(...)</code> branch construct but - nevertheless tries to implement the following logic:</para> + <qandadiv> + <qandaentry> + <question> + <para>A computer newbie did not yet read about the <code + language="java">else if(...)</code> branch construct but + nevertheless tries to implement the following logic:</para> - <programlisting language="java">if (a < 7) { + <programlisting language="java">if (a < 7) { System.out.println("o.K."); } else if (b == 5) { System.out.println("Maybe"); @@ -660,28 +668,28 @@ int main(int argc, char **args) { System.out.println("Wrong!"); }</programlisting> - <para><code language="java">a</code> and <code - language="java">b</code> are supposed to be <code - language="java">int</code> variables. Please help our newbie using - just <code language="java">if(...){...} else {...} </code> - avoiding <code language="java">else if(...) {}</code> branch - statements!</para> + <para><code language="java">a</code> and <code + language="java">b</code> are supposed to be <code + language="java">int</code> variables. Please help our newbie + using just <code language="java">if(...){...} else {...} </code> + avoiding <code language="java">else if(...) {}</code> branch + statements!</para> - <tip> - <para>As the title suggests you may want to nest an - <quote>inner</quote> <code language="java">if(...)</code> inside - an <quote>outer</quote> one.</para> - </tip> - </question> + <tip> + <para>As the title suggests you may want to nest an + <quote>inner</quote> <code language="java">if(...)</code> + inside an <quote>outer</quote> one.</para> + </tip> + </question> - <answer> - <para>The solution requires replacing the <code - language="java">else if(...)</code> branch by a nested <code - language="java">if(...){ ...} else</code> statement and moving the - final <code language="java">else</code> block into the nested - one.</para> + <answer> + <para>The solution requires replacing the <code + language="java">else if(...)</code> branch by a nested <code + language="java">if(...){ ...} else</code> statement and moving + the final <code language="java">else</code> block into the + nested one.</para> - <programlisting language="java">if (a < 7) { + <programlisting language="java">if (a < 7) { System.out.println("o.K."); } else { if (b == 5) { @@ -690,15 +698,15 @@ int main(int argc, char **args) { System.out.println("Wrong!"); } }</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <figure xml:id="sd1_fig_IfOmitBlocks"> - <title>Single statement branches</title> + <figure xml:id="sd1_fig_IfOmitBlocks"> + <title>Single statement branches</title> - <programlisting language="java">double initialAmount = 3200; + <programlisting language="java">double initialAmount = 3200; if (100000 <= initialAmount) System.out.println("Interest:" + 1.2 * initialAmount / 100); @@ -707,120 +715,120 @@ else if (1000 <= initialAmount) else System.out.println("Interest:" + 0);</programlisting> - <para>Branches containing exactly one statement don't require a block - definition.</para> - </figure> + <para>Branches containing exactly one statement don't require a block + definition.</para> + </figure> - <figure xml:id="sd1_fig_corporateSafety"> - <title>Aside: Corporate network security</title> + <figure xml:id="sd1_fig_corporateSafety"> + <title>Aside: Corporate network security</title> - <para>Would you access your online bank account using <emphasis - role="bold">Stuttgart Media University's</emphasis> network?</para> + <para>Would you access your online bank account using <emphasis + role="bold">Stuttgart Media University's</emphasis> network?</para> - <orderedlist> - <listitem> - <para>Yes</para> - </listitem> + <orderedlist> + <listitem> + <para>Yes</para> + </listitem> - <listitem> - <para>Only on private equipment (Smartphone, tablet, laptop, - ...)</para> - </listitem> + <listitem> + <para>Only on private equipment (Smart phone, tablet, laptop, + ...)</para> + </listitem> - <listitem> - <para>Never!</para> - </listitem> - </orderedlist> - </figure> + <listitem> + <para>Never!</para> + </listitem> + </orderedlist> + </figure> - <figure xml:id="sd1_fig_onlineBanking"> - <title>Online banking</title> + <figure xml:id="sd1_fig_onlineBanking"> + <title>Online banking</title> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Statements/onlineBanking.svg"/> - </imageobject> - </mediaobject> - </figure> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Statements/onlineBanking.svg"/> + </imageobject> + </mediaobject> + </figure> - <figure xml:id="sd1_fig_ipSpoofing"> - <title>DNS Spoofing</title> + <figure xml:id="sd1_fig_ipSpoofing"> + <title>DNS Spoofing</title> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Statements/dns-spoofing.png"/> - </imageobject> - </mediaobject> - </figure> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Statements/dns-spoofing.png"/> + </imageobject> + </mediaobject> + </figure> - <figure xml:id="sd1_fig_getIpToDns"> - <title>Turn <xref linkend="glo_DNS"/> name into IP address</title> + <figure xml:id="sd1_fig_getIpToDns"> + <title>Turn <xref linkend="glo_DNS"/> name into IP address</title> - <informaltable border="0"> - <colgroup width="37%"/> + <informaltable border="0"> + <colgroup width="37%"/> - <colgroup width="63%"/> + <colgroup width="63%"/> - <tr> - <td valign="top"><screen>~> nslookup + <tr> + <td valign="top"><screen>~> nslookup > server <emphasis role="red">141.62.1.5</emphasis> <co - linkends="sd1_fig_getIpToDns-1" - xml:id="sd1_fig_getIpToDns-1-co"/> + linkends="sd1_fig_getIpToDns-1" + xml:id="sd1_fig_getIpToDns-1-co"/> Address: 141.62.1.5#53 > > <emphasis role="red">meine.deutsche-bank.de</emphasis> <co - linkends="sd1_fig_getIpToDns-2" - xml:id="sd1_fig_getIpToDns-2-co"/> + linkends="sd1_fig_getIpToDns-2" + xml:id="sd1_fig_getIpToDns-2-co"/> Server: 141.62.1.5 Address: 141.62.1.5#53 Non-authoritative answer: Name: meine.deutsche-bank.de Address: <emphasis role="red">129.35.230.2</emphasis> <co - linkends="sd1_fig_getIpToDns-3" - xml:id="sd1_fig_getIpToDns-3-co"/></screen></td> - - <td valign="top"><calloutlist> - <callout arearefs="sd1_fig_getIpToDns-1-co" role="slideExclude" - xml:id="sd1_fig_getIpToDns-1"> - <para>Nameserver answering <xref linkend="glo_DNS"/> - queries.</para> - </callout> - - <callout arearefs="sd1_fig_getIpToDns-2-co" - xml:id="sd1_fig_getIpToDns-2"> - <para>Entering <xref linkend="glo_DNS"/> name.</para> - </callout> - - <callout arearefs="sd1_fig_getIpToDns-3-co" - xml:id="sd1_fig_getIpToDns-3"> - <para>IP address of <uri>meine.deutsche-bank.de</uri> is - <code>129.35.230.2</code>.</para> - - <para>Caution: This IP value may become subject to - tampering.</para> - </callout> - </calloutlist></td> - </tr> - </informaltable> - </figure> + linkends="sd1_fig_getIpToDns-3" + xml:id="sd1_fig_getIpToDns-3-co"/></screen></td> - <figure xml:id="sd1_fig_SSLCertificateError"> - <title><abbrev>SSL</abbrev> Certificate error</title> + <td valign="top"><calloutlist> + <callout arearefs="sd1_fig_getIpToDns-1-co" + role="slideExclude" xml:id="sd1_fig_getIpToDns-1"> + <para>Nameserver answering <xref linkend="glo_DNS"/> + queries.</para> + </callout> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Statements/sslCertificateError.svg"/> - </imageobject> - </mediaobject> - </figure> + <callout arearefs="sd1_fig_getIpToDns-2-co" + xml:id="sd1_fig_getIpToDns-2"> + <para>Entering <xref linkend="glo_DNS"/> name.</para> + </callout> - <figure xml:id="sd1_fig_IfOmitBewareIndent"> - <title>The <link - xlink:href="https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/">Apple - <quote><code>goto fail</code></quote> SSL bug</link></title> + <callout arearefs="sd1_fig_getIpToDns-3-co" + xml:id="sd1_fig_getIpToDns-3"> + <para>IP address of <uri>meine.deutsche-bank.de</uri> is + <code>129.35.230.2</code>.</para> - <programlisting language="c">static OSStatus SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, + <para>Caution: This IP value may become subject to + tampering.</para> + </callout> + </calloutlist></td> + </tr> + </informaltable> + </figure> + + <figure xml:id="sd1_fig_SSLCertificateError"> + <title><abbrev>SSL</abbrev> Certificate error</title> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Statements/sslCertificateError.svg"/> + </imageobject> + </mediaobject> + </figure> + + <figure xml:id="sd1_fig_IfOmitBewareIndent"> + <title>The <link + xlink:href="https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/">Apple + <quote><code>goto fail</code></quote> SSL bug</link></title> + + <programlisting language="c">static OSStatus SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams, uint8_t *signature, UInt16 signatureLen) { OSStatus err; ... @@ -838,51 +846,51 @@ fail: SSLFreeBuffer(&signedHashes); SSLFreeBuffer(&hashCtx); return err; }</programlisting> - </figure> + </figure> - <figure xml:id="sd1_fig_appleSslBugCodeReachability"> - <title>Static code analysis</title> + <figure xml:id="sd1_fig_appleSslBugCodeReachability"> + <title>Static code analysis</title> - <informaltable border="0"> - <tr> - <td><programlisting language="c">if ((err = SSLHashSHA1.update(...)) != 0) + <informaltable border="0"> + <tr> + <td><programlisting language="c">if ((err = SSLHashSHA1.update(...)) != 0) goto fail; goto fail;</programlisting></td> - <td>Reachable code, <code>err</code> yet 0</td> - </tr> + <td>Reachable code, <code>err</code> yet 0</td> + </tr> - <tr> - <td><programlisting language="c">if ((err = SSLHashSHA1.final(...)) != 0) + <tr> + <td><programlisting language="c">if ((err = SSLHashSHA1.final(...)) != 0) goto fail; err = sslRawVerify(...); ...</programlisting></td> - <td>Unreachable code: Variable <code language="java">err</code> - unchanged.</td> - </tr> + <td>Unreachable code: Variable <code language="java">err</code> + unchanged.</td> + </tr> - <tr> - <td><programlisting language="c">fail: + <tr> + <td><programlisting language="c">fail: SSLFreeBuffer(&signedHashes); SSLFreeBuffer(&hashCtx); return err;</programlisting></td> - <td>Reachable code, <code>err</code> possibly still 0</td> - </tr> - </informaltable> - </figure> + <td>Reachable code, <code>err</code> possibly still 0</td> + </tr> + </informaltable> + </figure> - <qandaset defaultlabel="qanda" - xml:id="sd1_qanda_staticCodeAnalysisUnreachable"> - <title>Static code analysis</title> + <qandaset defaultlabel="qanda" + xml:id="sd1_qanda_staticCodeAnalysisUnreachable"> + <title>Static code analysis</title> - <qandadiv> - <qandaentry> - <question> - <para>Consider the following snippet:</para> + <qandadiv> + <qandaentry> + <question> + <para>Consider the following snippet:</para> - <programlisting language="java">int count = 3; + <programlisting language="java">int count = 3; count++; if (count < 5) @@ -891,52 +899,53 @@ if (count < 5) System.out.println("Failed!");</programlisting> - <para>This <xref linkend="glo_Java"/> code mimics the <link - xlink:href="https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/">Apple - goto fail SSL bug</link>'s structural problem. Since <xref - linkend="glo_Java"/> does not offer <code>goto</code> statements - we use <code language="java">return</code> instead for terminating - the <quote>current</quote> context.</para> + <para>This <xref linkend="glo_Java"/> code mimics the <link + xlink:href="https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/">Apple + goto fail SSL bug</link>'s structural problem. Since <xref + linkend="glo_Java"/> does not offer <code>goto</code> statements + we use <code language="java">return</code> instead for + terminating the <quote>current</quote> context.</para> - <para>Copy this code into your <xref linkend="glo_IDE"/> - and:</para> + <para>Copy this code into your <xref linkend="glo_IDE"/> + and:</para> - <orderedlist> - <listitem> - <para>Explain the resulting error. What is wrong? How do you - correct this error?</para> - </listitem> + <orderedlist> + <listitem> + <para>Explain the resulting error. What is wrong? How do you + correct this error?</para> + </listitem> - <listitem> - <para>Explain the <code language="java">count++</code> - statement's purpose.</para> + <listitem> + <para>Explain the <code language="java">count++</code> + statement's purpose.</para> - <tip> - <para>There is no <quote>real</quote> application logic in - the given code. It is an example meant to explain formal - <xref linkend="glo_Java"/> language features. What happens - if you correct the compile time error and in addition purge - the <code language="java">count++</code> statement?</para> - </tip> - </listitem> + <tip> + <para>There is no <quote>real</quote> application logic in + the given code. It is an example meant to explain formal + <xref linkend="glo_Java"/> language features. What happens + if you correct the compile time error and in addition + purge the <code language="java">count++</code> + statement?</para> + </tip> + </listitem> - <listitem> - <para>What is the underlying idea of these warning and error - messages?</para> - </listitem> - </orderedlist> - </question> + <listitem> + <para>What is the underlying idea of these warning and error + messages?</para> + </listitem> + </orderedlist> + </question> - <answer> - <orderedlist> - <listitem> - <para>Like in the <link - xlink:href="https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/">Apple - goto fail SSL bug</link> the code is poorly indented. Using - your <xref linkend="glo_IDE"/>'s auto formatting feature - (how?) you get:</para> - - <programlisting language="java">int count = 3; + <answer> + <orderedlist> + <listitem> + <para>Like in the <link + xlink:href="https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/">Apple + goto fail SSL bug</link> the code is poorly indented. Using + your <xref linkend="glo_IDE"/>'s auto formatting feature + (how?) you get:</para> + + <programlisting language="java">int count = 3; count++; if (count < 5) @@ -945,20 +954,20 @@ return; System.out.println("Failed!");</programlisting> - <para>The second <code language="java">return</code> statement - will leave the context unconditionally regardless of the - preceding <code language="java">if (count < 5)</code> and - the <code language="java">count</code> variable's value in - particular. Thus the final <code - language="java">System.out.println("Failed!")</code> <emphasis - role="bold">can</emphasis> never be reached resulting in a - compile time error.</para> + <para>The second <code language="java">return</code> + statement will leave the context unconditionally regardless + of the preceding <code language="java">if (count < + 5)</code> and the <code language="java">count</code> + variable's value in particular. Thus the final <code + language="java">System.out.println("Failed!")</code> + <emphasis role="bold">can</emphasis> never be reached + resulting in a compile time error.</para> - <para>Removing the erroneous <code - language="java">return</code> resolves the error leaving us - with happily compiling code:</para> + <para>Removing the erroneous <code + language="java">return</code> resolves the error leaving us + with happily compiling code:</para> - <programlisting language="java">int count = 3; + <programlisting language="java">int count = 3; count++; if (count < 5) @@ -966,73 +975,74 @@ if (count < 5) System.out.println("Failed!");</programlisting> - <para>Conclusion: The <link - xlink:href="https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/">Apple - goto fail SSL bug</link> could not have been occurring in - <xref linkend="glo_Java"/> due to language features.</para> - </listitem> + <para>Conclusion: The <link + xlink:href="https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/">Apple + goto fail SSL bug</link> could not have been occurring in + <xref linkend="glo_Java"/> due to language features.</para> + </listitem> - <listitem> - <para>Removing <code language="java">count++</code> leaves us - with:</para> + <listitem> + <para>Removing <code language="java">count++</code> leaves + us with:</para> - <programlisting language="java">int count = 3; + <programlisting language="java">int count = 3; if (count < 5) <co linkends="sd1_callout_staticIfAlwaysTrue-1" - xml:id="sd1_callout_staticIfAlwaysTrue-1-co"/> + xml:id="sd1_callout_staticIfAlwaysTrue-1-co"/> return; System.out.println("Failed!");</programlisting> - <calloutlist> - <callout arearefs="sd1_callout_staticIfAlwaysTrue-1-co" - xml:id="sd1_callout_staticIfAlwaysTrue-1"> - <para>Compiler warning: <emphasis role="bold">Condition - 'count < 5' is always 'true'</emphasis>.</para> - </callout> - </calloutlist> - - <para>This warning is due to static compile time code - analysis: The <code language="java">count</code> variable's - value is not being altered throughout its scope. It could - actually be declared as <code language="java">final int count - = 3 disallowing sub</code>sequent assignments. Thus the <code - language="java">return</code> inside <code language="java">if - (count < 5)</code> will always be executed.</para> - - <para>Despite its simplicity having the count++ statement in - place defeats the compiler's ability to discover that <code - language="java">count</code> now having a value of 4 still - being smaller than 5.</para> - </listitem> - - <listitem> - <para>The underlying idea is preventing programmers from - coding carelessly: Even the <code language="java">if (count - < 5)</code> statement is most likely an unintended - bug.</para> - </listitem> - </orderedlist> - - <important> - <para>Conclusion: Watch out for compiler warning messages and do - not ignore them!</para> - - <para>In many cases compiler warnings reveal serious flaws. - Correction on average will save you lots of cumbersome time - debugging your code.</para> - </important> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + <calloutlist> + <callout arearefs="sd1_callout_staticIfAlwaysTrue-1-co" + xml:id="sd1_callout_staticIfAlwaysTrue-1"> + <para>Compiler warning: <emphasis role="bold">Condition + 'count < 5' is always 'true'</emphasis>.</para> + </callout> + </calloutlist> + + <para>This warning is due to static compile time code + analysis: The <code language="java">count</code> variable's + value is not being altered throughout its scope. It could + actually be declared as <code language="java">final int + count = 3 disallowing sub</code>sequent assignments. Thus + the <code language="java">return</code> inside <code + language="java">if (count < 5)</code> will always be + executed.</para> + + <para>Despite its simplicity having the count++ statement in + place defeats the compiler's ability to discover that <code + language="java">count</code> now having a value of 4 still + being smaller than 5.</para> + </listitem> - <figure xml:id="sd1_fig_ifElseNested"> - <title>Nested <code language="java">if ... else</code></title> + <listitem> + <para>The underlying idea is preventing programmers from + coding carelessly: Even the <code language="java">if (count + < 5)</code> statement is most likely an unintended + bug.</para> + </listitem> + </orderedlist> - <informaltable border="1"> - <tr> - <td valign="top"><programlisting language="java">if ('A' == grade || 'B' == grade) { + <important> + <para>Conclusion: Watch out for compiler warning messages and + do not ignore them!</para> + + <para>In many cases compiler warnings reveal serious flaws. + Correction on average will save you lots of cumbersome time + debugging your code.</para> + </important> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <figure xml:id="sd1_fig_ifElseNested"> + <title>Nested <code language="java">if ... else</code></title> + + <informaltable border="1"> + <tr> + <td valign="top"><programlisting language="java">if ('A' == grade || 'B' == grade) { result = "Excellent"; } else { if ('C' == grade) { @@ -1046,22 +1056,26 @@ System.out.println("Failed!");</programlisting> } }</programlisting></td> - <td valign="top"><mediaobject> - <imageobject> - <imagedata fileref="Ref/Statements/if_elseIf_else.svg"/> - </imageobject> - </mediaobject></td> - </tr> - </informaltable> - </figure> + <td valign="top"><mediaobject> + <imageobject> + <imagedata fileref="Ref/Statements/if_elseIf_else.svg"/> + </imageobject> + </mediaobject></td> + </tr> + </informaltable> + </figure> + </section> - <figure xml:id="sd1_fig_ifElse_else"> - <title>Equivalent: <code language="java">if ... else if ... - else</code></title> + <section xml:id="sd1_sect_elseif"> + <title>Using <code language="java">else if</code></title> - <informaltable border="1"> - <tr> - <td valign="top"><programlisting language="java">if ('A' == grade || 'B' == grade) { + <figure xml:id="sd1_fig_ifElse_else"> + <title>Equivalent: <code language="java">if ... else if ... + else</code></title> + + <informaltable border="1"> + <tr> + <td valign="top"><programlisting language="java">if ('A' == grade || 'B' == grade) { result = "Excellent"; } else if ('C' == grade) { result = "O.k."; @@ -1071,58 +1085,58 @@ System.out.println("Failed!");</programlisting> result = "Failed"; }</programlisting></td> - <td valign="top"><mediaobject> - <imageobject> - <imagedata fileref="Ref/Statements/if_elseIf_else.svg"/> - </imageobject> - </mediaobject></td> - </tr> - </informaltable> - </figure> + <td valign="top"><mediaobject> + <imageobject> + <imagedata fileref="Ref/Statements/if_elseIf_else.svg"/> + </imageobject> + </mediaobject></td> + </tr> + </informaltable> + </figure> - <figure xml:id="sd1_fig_ifElse_elseSyntax"> - <title><code language="java">if ... else if ... else</code> - syntax</title> + <figure xml:id="sd1_fig_ifElse_elseSyntax"> + <title><code language="java">if ... else if ... else</code> + syntax</title> - <programlisting language="java">if (booleanExpression) + <programlisting language="java">if (booleanExpression) (block | statement) [else if (booleanExpression) (block | statement) ]* <co linkends="sd1_fig_ifElse_elseSyntax-1" - xml:id="sd1_fig_ifElse_elseSyntax-1-co"/> + xml:id="sd1_fig_ifElse_elseSyntax-1-co"/> [else (block | statement) ] <co linkends="sd1_fig_ifElse_elseSyntax-2" - xml:id="sd1_fig_ifElse_elseSyntax-2-co"/></programlisting> - - <calloutlist role="slideExclude"> - <callout arearefs="sd1_fig_ifElse_elseSyntax-1-co" - xml:id="sd1_fig_ifElse_elseSyntax-1"> - <para>The pair of braces [...] indicates an optional clause. The - asterisk <quote>*</quote> indicates an arbitrary number of - repetitions (zero to infinity).</para> - </callout> - - <callout arearefs="sd1_fig_ifElse_elseSyntax-2-co" - xml:id="sd1_fig_ifElse_elseSyntax-2"> - <para>The second pair of braces [...] again indicates an optional - clause.</para> - - <para>Thus only the <quote><code language="java">if - (booleanExpression) (block | statement)</code></quote> clause is - mandatory.</para> - </callout> - </calloutlist> - </figure> + xml:id="sd1_fig_ifElse_elseSyntax-2-co"/></programlisting> + + <calloutlist role="slideExclude"> + <callout arearefs="sd1_fig_ifElse_elseSyntax-1-co" + xml:id="sd1_fig_ifElse_elseSyntax-1"> + <para>The pair of braces [...] indicates an optional clause. The + asterisk <quote>*</quote> indicates an arbitrary number of + repetitions (zero to infinity).</para> + </callout> - <figure xml:id="sd1_fig_useScannerClass"> - <title>User input recipe</title> + <callout arearefs="sd1_fig_ifElse_elseSyntax-2-co" + xml:id="sd1_fig_ifElse_elseSyntax-2"> + <para>The second pair of braces [...] again indicates an optional + clause.</para> - <informaltable border="0"> - <colgroup width="64%"/> + <para>Thus only the <quote><code language="java">if + (booleanExpression) (block | statement)</code></quote> clause is + mandatory.</para> + </callout> + </calloutlist> + </figure> - <colgroup width="36%"/> + <figure xml:id="sd1_fig_useScannerClass"> + <title>User input recipe</title> - <tr> - <td valign="top"><programlisting language="java">import java.util.Scanner; + <informaltable border="0"> + <colgroup width="64%"/> + + <colgroup width="36%"/> + + <tr> + <td valign="top"><programlisting language="java">import java.util.Scanner; public class App { public static void main(String[] args){ @@ -1139,41 +1153,41 @@ public class App { } }</programlisting></td> - <td valign="top"><screen>Enter a value:123 + <td valign="top"><screen>Enter a value:123 You entered 123</screen><para>See <methodname - xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html#nextBoolean()">nextBoolean()</methodname>, - <methodname><link - xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html#nextByte()">nextByte()</link></methodname> - and friends.</para></td> - </tr> - </informaltable> - </figure> + xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html#nextBoolean()">nextBoolean()</methodname>, + <methodname><link + xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html#nextByte()">nextByte()</link></methodname> + and friends.</para></td> + </tr> + </informaltable> + </figure> - <qandaset defaultlabel="qanda" xml:id="sw1QandaPostExamBonuspoints"> - <title>Post modifying an exam's marking</title> + <qandaset defaultlabel="qanda" xml:id="sw1QandaPostExamBonuspoints"> + <title>Post modifying an exam's marking</title> - <qandadiv> - <qandaentry> - <question> - <para>After completely marking an examination a lecturer decides - to globally add a number extra bonus points to a specific task . - The task does have an upper limit of points to be awarded at - maximum. We provide an example:</para> + <qandadiv> + <qandaentry> + <question> + <para>After completely marking an examination a lecturer decides + to globally add a number extra bonus points to a specific task . + The task does have an upper limit of points to be awarded at + maximum. We provide an example:</para> - <para>On completely fulfilling a given task 12 points will be - awarded. So after marking the exam participants' points range from - 0 to 12 points being represented by the variable <code - language="java">pointsReached</code>.</para> + <para>On completely fulfilling a given task 12 points will be + awarded. So after marking the exam participants' points range + from 0 to 12 points being represented by the variable <code + language="java">pointsReached</code>.</para> - <para>After completing his marking our lecturer wants to add three - more points to all participants without breaching the 12 point - limit.</para> + <para>After completing his marking our lecturer wants to add + three more points to all participants without breaching the 12 + point limit.</para> - <para>Complete the following code by assigning this modified - number of points to the variable <code - language="java">newResult</code>.</para> + <para>Complete the following code by assigning this modified + number of points to the variable <code + language="java">newResult</code>.</para> - <programlisting language="java">public static void main(String[] args) { + <programlisting language="java">public static void main(String[] args) { int pointsReached = 1; int maximumPoints = 12; @@ -1185,15 +1199,15 @@ You entered 123</screen><para>See <methodname System.out.println("New Result:" + newResult); }</programlisting> - </question> + </question> - <answer> - <para>The basic task is to add up the values of <code - language="java">pointsReached</code> and <code - language="java">pointsToAdd</code>. When exceeding the limit we - just assign the limit itself:</para> + <answer> + <para>The basic task is to add up the values of <code + language="java">pointsReached</code> and <code + language="java">pointsToAdd</code>. When exceeding the limit we + just assign the limit itself:</para> - <programlisting language="java">public static void main(String[] args) { + <programlisting language="java">public static void main(String[] args) { final int pointsReached = 1; final int maximumPoints = 12; @@ -1210,98 +1224,99 @@ You entered 123</screen><para>See <methodname System.out.println("New Result:" + newResult); }</programlisting> - <para>This basically means calculating the minimum of the two - expressions <code language="java">pointsReached + - pointsToAdd</code> and <code language="java">maximumPoints</code>. - This may as well be be implemented by:</para> + <para>This basically means calculating the minimum of the two + expressions <code language="java">pointsReached + + pointsToAdd</code> and <code + language="java">maximumPoints</code>. This may as well be be + implemented by:</para> - <programlisting language="java">public static void main(String[] args) { + <programlisting language="java">public static void main(String[] args) { final int pointsReached = 1; final int maximumPoints = 12; final int pointsToAdd = 3; final int newResult = <link - xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html#min(int,int)">Math.min</link>(maximumPoints, pointsReached + pointsToAdd); + xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html#min(int,int)">Math.min</link>(maximumPoints, pointsReached + pointsToAdd); System.out.println("New Result:" + newResult); }</programlisting> - <para>You will fully understand the above expression <classname - xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html">Math</classname>.<methodname - xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html#min(int,int)">min(...)</methodname> - after finishing the <quote>Static Final Variables</quote> section - of <xref linkend="bib_Kurniawan"/>.</para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + <para>You will fully understand the above expression <classname + xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html">Math</classname>.<methodname + xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html#min(int,int)">min(...)</methodname> + after finishing the <quote>Static Final Variables</quote> + section of <xref linkend="bib_Kurniawan"/>.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <qandaset defaultlabel="qanda" xml:id="sd1QandaAtTheBar"> - <title>At the bar</title> + <qandaset defaultlabel="qanda" xml:id="sd1QandaAtTheBar"> + <title>At the bar</title> - <qandadiv> - <qandaentry> - <question> - <para>This example uses existing program code to be explained - later. You'll implement an interactive application which - implements a dialogue with a user asking for input to be entered - in a terminal like window as being shown in the following - video:</para> - - <figure xml:id="sd1VideoUsingScannerClass"> - <title>Using a <classname - xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html">Scanner</classname> - class collecting user input.</title> - - <mediaobject> - <videoobject> - <videodata fileref="Ref/Video/scannerUsage.webm"/> - </videoobject> - </mediaobject> - </figure> - - <para>A bar uses a software system for picking up orders. The bar - will serve just orange juice and beer. For legal reasons the - latter will only be served to persons of at least 16 years of age. - We show three possible user dialogues:</para> - - <orderedlist> - <listitem> - <screen>On offer: + <qandadiv> + <qandaentry> + <question> + <para>This example uses existing program code to be explained + later. You'll implement an interactive application which + implements a dialogue with a user asking for input to be entered + in a terminal like window as being shown in the following + video:</para> + + <figure xml:id="sd1VideoUsingScannerClass"> + <title>Using a <classname + xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html">Scanner</classname> + class collecting user input.</title> + + <mediaobject> + <videoobject> + <videodata fileref="Ref/Video/scannerUsage.webm"/> + </videoobject> + </mediaobject> + </figure> + + <para>A bar uses a software system for picking up orders. The + bar will serve just orange juice and beer. For legal reasons the + latter will only be served to persons of at least 16 years of + age. We show three possible user dialogues:</para> + + <orderedlist> + <listitem> + <screen>On offer: 1=Beer 2=Orange juice Your choice:>1 Tell me your age please:>15 Sorry, we are not allowed to serve beer to underage customers</screen> - </listitem> + </listitem> - <listitem> - <screen>On offer: + <listitem> + <screen>On offer: 1=Beer 2=Orange juice Your choice:>2 o.K.</screen> - </listitem> + </listitem> - <listitem> - <screen>On offer: + <listitem> + <screen>On offer: 1=Beer 2=Orange juice Your choice:>4 Sorry, invalid choice</screen> - </listitem> - </orderedlist> + </listitem> + </orderedlist> - <para>Since you may not yet know how to enable <xref - linkend="glo_Java"/> applications asking for user input simply use - the following recipe to get started:</para> + <para>Since you may not yet know how to enable <xref + linkend="glo_Java"/> applications asking for user input simply + use the following recipe to get started:</para> - <programlisting language="java" - xml:id="sd1_listing_scannerBoilerplate">public static void main(String[] args) { + <programlisting language="java" + xml:id="sd1_listing_scannerBoilerplate">public static void main(String[] args) { try (final Scanner scan = new Scanner(System.in)) { // Creating a scanner for reading user input @@ -1314,21 +1329,22 @@ Sorry, invalid choice</screen> } // Auto closing scanner }</programlisting> - <para>Copy this boilerplate code into your <xref - linkend="glo_IDE"/>. The <xref linkend="glo_IDE"/> will assist you - adding a required <code language="java">import - java.util.Scanner;</code> statement in your code's header section. - Execute this code. You should see a dialogue like:</para> + <para>Copy this boilerplate code into your <xref + linkend="glo_IDE"/>. The <xref linkend="glo_IDE"/> will assist + you adding a required <code language="java">import + java.util.Scanner;</code> statement in your code's header + section. Execute this code. You should see a dialogue + like:</para> - <screen>Please enter a value:112 + <screen>Please enter a value:112 You entered: 112</screen> - <para>Then extend the above code implementing the desired - behaviour.</para> - </question> + <para>Then extend the above code implementing the desired + behaviour.</para> + </question> - <answer> - <programlisting language="java">package start; + <answer> + <programlisting language="java">package start; import java.util.Scanner; @@ -1363,41 +1379,41 @@ public class BarOrder { scan.close(); } }</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <qandaset defaultlabel="qanda" xml:id="sd1QandaRomanNumeralsIf"> - <title>Roman numerals</title> + <qandaset defaultlabel="qanda" xml:id="sd1QandaRomanNumeralsIf"> + <title>Roman numerals</title> - <qandadiv> - <qandaentry> - <question> - <para>Write an application which turns a positive integer values - up to and including 10 into <link - xlink:href="https://en.wikipedia.org/wiki/Roman_numerals">Roman - numeral</link> representation:</para> + <qandadiv> + <qandaentry> + <question> + <para>Write an application which turns a positive integer values + up to and including 10 into <link + xlink:href="https://en.wikipedia.org/wiki/Roman_numerals">Roman + numeral</link> representation:</para> - <screen>Enter a number:>9 + <screen>Enter a number:>9 IX</screen> - <para>Regarding user input you may start from <xref - linkend="sd1_listing_scannerBoilerplate"/> again. If the user - enters a value smaller than one or greater than ten the following - output is to be expected:</para> + <para>Regarding user input you may start from <xref + linkend="sd1_listing_scannerBoilerplate"/> again. If the user + enters a value smaller than one or greater than ten the + following output is to be expected:</para> - <screen>Enter a number:>11 + <screen>Enter a number:>11 Decimal value 11 not yet implemented</screen> - <tip> - <para>You may use a series of <code language="java">if () {...} - else if () {...} ...</code> statements.</para> - </tip> - </question> + <tip> + <para>You may use a series of <code language="java">if () + {...} else if () {...} ...</code> statements.</para> + </tip> + </question> - <answer> - <para><programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { + <answer> + <para><programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { System.out.print("Enter a number:>"); final int number = scan.nextInt(); @@ -1426,74 +1442,74 @@ Decimal value 11 not yet implemented</screen> System.out.println("Decimal value " + number + " not yet implemented"); } }</programlisting></para> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + </answer> + </qandaentry> + </qandadiv> + </qandaset> - <figure xml:id="sd1_fig_problemNumber2Dayname"> - <title>Converting numbers to day's names</title> + <figure xml:id="sd1_fig_problemNumber2Dayname"> + <title>Converting numbers to day's names</title> - <informaltable border="0"> - <colgroup width="28%"/> + <informaltable border="0"> + <colgroup width="28%"/> - <colgroup width="72%"/> + <colgroup width="72%"/> - <tr> - <td valign="top"><para>Task: Convert day's numbers to day's - names</para></td> + <tr> + <td valign="top"><para>Task: Convert day's numbers to day's + names</para></td> - <td valign="top"><informaltable border="1"> - <tr> - <td>1</td> + <td valign="top"><informaltable border="1"> + <tr> + <td>1</td> - <td>Monday</td> - </tr> + <td>Monday</td> + </tr> - <tr> - <td>2</td> + <tr> + <td>2</td> - <td>Tuesday</td> - </tr> + <td>Tuesday</td> + </tr> - <tr> - <td>3</td> + <tr> + <td>3</td> - <td>Wednesday</td> - </tr> + <td>Wednesday</td> + </tr> - <tr> - <td>4</td> + <tr> + <td>4</td> - <td>Thursday</td> - </tr> + <td>Thursday</td> + </tr> - <tr> - <td>5</td> + <tr> + <td>5</td> - <td>Friday</td> - </tr> + <td>Friday</td> + </tr> - <tr> - <td>6</td> + <tr> + <td>6</td> - <td>Saturday</td> - </tr> + <td>Saturday</td> + </tr> - <tr> - <td>7</td> + <tr> + <td>7</td> - <td>Sunday</td> - </tr> - </informaltable></td> - </tr> - </informaltable> - </figure> + <td>Sunday</td> + </tr> + </informaltable></td> + </tr> + </informaltable> + </figure> - <figure xml:id="sd1_fig_dayNumber2NameByIfElseif"> - <title>Implementing number to day's names</title> + <figure xml:id="sd1_fig_dayNumber2NameByIfElseif"> + <title>Implementing number to day's names</title> - <programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { + <programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { System.out.print("Enter a weekday number (1=Monday, 2=Tuesday,...) : "); final int number = scan.nextInt(); @@ -1510,7 +1526,8 @@ Decimal value 11 not yet implemented</screen> System.out.println("Invalid number " + number); } }</programlisting> - </figure> + </figure> + </section> </section> <section xml:id="sd1_sect_switch">