diff --git a/Doc/Sd1/Ref/Statements/onlineBanking.png b/Doc/Sd1/Ref/Statements/onlineBanking.png new file mode 100644 index 0000000000000000000000000000000000000000..b99c307d3c2b7e92184653aae98db8ac9c1eb735 Binary files /dev/null and b/Doc/Sd1/Ref/Statements/onlineBanking.png differ diff --git a/Doc/Sd1/objectsClasses.xml b/Doc/Sd1/objectsClasses.xml index 136125d8b6c75d2cef94171571b96e0c52d6d312..68b3cef053b8028d5cddef635fce37f91dd90c3c 100644 --- a/Doc/Sd1/objectsClasses.xml +++ b/Doc/Sd1/objectsClasses.xml @@ -39,13 +39,13 @@ </listitem> <listitem> - <para>allow for tailored access to attributes.</para> + <para>allow for tailored access to methods and attributes.</para> </listitem> </itemizedlist> </figure> <figure xml:id="sd1_fig_classRectangle"> - <title>Class describing rectangles</title> + <title>A class describing rectangles</title> <programlisting language="java">public class Rectangle { int width; diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index 9cf8c01a953a372cc24c5c77cf28bdf8bea1340f..fdb9bb0242797255d156d117f57143d0bfb06169 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -166,7 +166,9 @@ if (100000 <= initialAmount) { // Rich customer, 1,2% interest rate System.out.println( "Interest:" + 1.2 * initialAmount / 100); -} </programlisting><screen>Interest:3840.0</screen></td> +} +System.out.println("Done!");</programlisting><screen>Interest:3840.0 +Done!</screen></td> <td valign="top"><mediaobject> <imageobject> @@ -192,7 +194,9 @@ if (100000 <= initialAmount) { // Joe customer, 0.8% standard interest rate System.out.println( "Interest:" + 0.8 * initialAmount / 100); -}</programlisting><screen>Interest:3.36</screen></td> +} +System.out.println("Done!");</programlisting><screen>Interest:3.36 +Done!</screen></td> <td valign="top"><mediaobject> <imageobject> @@ -227,6 +231,20 @@ if (100000 <= initialAmount) { </informaltable> </figure> + <figure xml:id="sd1_fig_bestPracticeCompareEquals"> + <title><code>Best practices comparing for equality</code></title> + + <para>Always use</para> + + <programlisting language="java">if (4 == variable) ...</programlisting> + + <para>in favour of:</para> + + <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> + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_booleanEqual"> <title>Comparing for equality</title> @@ -304,25 +322,11 @@ int main(int argc, char **args) { </glossentry> </glosslist> - <para>Thus <code>if(count = 4)</code> will always be true irrespective of the variable <code>count</code>'s initial value. Most important: The C compiler will not issue an error or warning unless non-default, more restrictive compile time directive are being activated.</para> + <para>Thus <code>if(count = 4)</code> will always be true irrespective of the variable <code>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.</para> <para>For this reason it is good practice using <code>if (4 == count)</code> instead: Even in C an accidentally mistyped <code>if (4 = count)</code> statement will definitively result in a compile time error most likely saving its author from tedious debugging.</para> </note> - <figure xml:id="sd1_fig_bestPracticeCompareEquals"> - <title><code>Best practices comparing for equality</code></title> - - <para>Always use</para> - - <programlisting language="java">if (4 == variable) ...</programlisting> - - <para>in favour of:</para> - - <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> - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_replaceElseIf"> <title>Replacing <code>if else (...)</code> by introducing a nested <code>if ... else</code></title> @@ -392,6 +396,16 @@ else </itemizedlist> </figure> + <figure xml:id="sd1_fig_onlineBanking"> + <title>Online banking</title> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Statements/onlineBanking.png"/> + </imageobject> + </mediaobject> + </figure> + <figure xml:id="sd1_fig_ipSpoofing"> <title>IP Spoofing</title> @@ -413,7 +427,7 @@ else </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 goto fail SSL bug</link></title> + <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, @@ -612,12 +626,16 @@ System.out.println("Failed!");</programlisting> </figure> <figure xml:id="sd1_fig_useScannerClass"> - <title>User input by <link xlink:href="https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html">Scanner</link></title> + <title>User input recipe</title> + + <programlisting language="java">try (final <link xlink:href="https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html">Scanner</link> scan = new <link xlink:href="https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html">Scanner</link>(<link xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#in">System.in</link>)){ + System.out.print("Enter a value:"); + final int value = scan.<link xlink:href="https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextInt--">nextInt()</link>; + System.out.println("You entered " + value); +}</programlisting> - <programlisting language="java">final Scanner scan = new Scanner(System.in); -System.out.print("Enter a value:"); -final int value = scan.nextInt(); -System.out.println("You entered " + value);</programlisting> + <screen>Enter a value:1234567 +You entered 1234567</screen> <para>See <methodname xlink:href="https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextBoolean()">nextBoolean()</methodname>, <methodname><link xlink:href="https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextByte()">nextByte()</link></methodname> and friends.</para> </figure> @@ -945,6 +963,9 @@ switch(number) { default: System.out.println("Invalid number " + number); break; } ...</programlisting> + + <screen>Enter a weekday number (1=Monday, 2=Tuesday,...) :>6 +Saturday</screen> </figure> <qandaset defaultlabel="qanda" xml:id="sd1_qanda_whyBreak"> @@ -1133,7 +1154,7 @@ switch(day % 7) { <figure xml:id="sd1_fig_switchOnString"> <title>Switching on strings</title> - <para>Starting from <xref linkend="glo_Java"/> 7 <code>switch</code> statements using <classname xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html">String</classname> based <code>case</code> labels are allowed:</para> + <para><xref linkend="glo_Java"/> 7 introduced <classname xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html">String</classname> based <code>case</code> labels:</para> <programlisting language="java">String month, season; ... @@ -1404,7 +1425,7 @@ public class LeapYear { <glossterm>Objective</glossterm> <glossdef> - <para>Execute the same code multiple times.</para> + <para>Execute the same statement multiple times.</para> </glossdef> </glossentry> @@ -1412,7 +1433,7 @@ public class LeapYear { <glossterm>Solution</glossterm> <glossdef> - <para>Copy / paste the code in question:</para> + <para>Copy / paste the statement in question:</para> <programlisting language="java">System.out.println("Do not copy!"); System.out.println("Do not copy!"); @@ -1449,7 +1470,7 @@ switch(repetitions) { case 1: System.out.println("Do not copy!"); }</programlisting> - <para>Problem: Clumsy and limited (five lines of output max.)</para> + <para>Problem: Clumsy and limited (predefined maximum).</para> </figure> <section xml:id="sd1_sect_while"> @@ -1598,7 +1619,7 @@ Goodbye!</screen>We thus use a <code>do ... while</code> loop entering the loop' <title>for</title> <figure xml:id="sd1_fig_forConsider"> - <title>Common usage of <code>while</code></title> + <title>Frequent usage of <code>while</code></title> <programlisting language="java">int i = 1; <co linkends="sd1_callout_whileLoopJustCounting-1" xml:id="sd1_callout_whileLoopJustCounting-1-co"/> @@ -1627,9 +1648,20 @@ while (i < 5 <co linkends="sd1_callout_whileLoopJustCounting-2" xml:id="sd1_c <figure xml:id="sd1_fig_for"> <title>Replacing <code>while</code> by <code>for</code></title> - <programlisting language="java">for (int i = 0 <co linkends="sd1_callout_for-1" xml:id="sd1_callout_for-1-co"/>; i < 5 <co linkends="sd1_callout_for-2" xml:id="sd1_callout_for-2-co"/>; i++ <co linkends="sd1_callout_for-3" xml:id="sd1_callout_for-3-co"/>) { + <informaltable border="1"> + <tr> + <td><programlisting language="java">for (int i = 0 <co linkends="sd1_callout_for-1" xml:id="sd1_callout_for-1-co"/>; i < 5 <co linkends="sd1_callout_for-2" xml:id="sd1_callout_for-2-co"/>; i++ <co linkends="sd1_callout_for-3" xml:id="sd1_callout_for-3-co"/>) { ... -}</programlisting> +}</programlisting></td> + + <td><programlisting language="java">int i = 1; <coref linkend="sd1_callout_for-1-co"/> + +while (i < 5 <coref linkend="sd1_callout_for-2-co"/>) { + ... + i++; <coref linkend="sd1_callout_for-3-co"/> +}</programlisting></td> + </tr> + </informaltable> <calloutlist> <callout arearefs="sd1_callout_for-1-co" xml:id="sd1_callout_for-1"> @@ -2710,9 +2742,9 @@ for (int i = 1; i <= limit; i++) { sum += i; } -System.out.println("0 + ... + " + limit + " = " + sum); </programlisting></td> +System.out.println("1 + ... + " + limit + " = " + sum); </programlisting></td> - <td><screen>0 + ... + 5 = 15 </screen></td> + <td><screen>1 + ... + 5 = 15 </screen></td> </tr> </informaltable> </figure> @@ -2725,9 +2757,9 @@ System.out.println("0 + ... + " + limit + " = " + sum); </programlisting> <question> <para>In <xref linkend="sd1_fig_loopCalculations"/> the resulting output was:</para> - <screen>0 + ... + 5 = 15</screen> + <screen>1 + ... + 5 = 15</screen> - <para>Modify the code to show <computeroutput>1 + 2 + 3 + 4 + 5 = 15</computeroutput> instead.</para> + <para>Modify the code to show all contributing values <computeroutput>1 + 2 + 3 + 4 + 5 = 15</computeroutput> instead.</para> </question> <answer> @@ -2926,22 +2958,24 @@ System.out.println(limit + " = " + sum);</programlisting> </m:math> </informalequation> - <para>Write an application which allows for determining the probabilistic success rates by calculating this coefficient. For the German <quote xml:lang="de">Glücksspirale</quote> a possible output reads:</para> + <para>Write an application which allows for determining the probabilistic success rates using this coefficient. For the German <quote xml:lang="de">Glücksspirale</quote> a possible output reads:</para> <screen>Your chance to win when drawing 6 out of 49 is 1 : 13983816</screen> + <para>Store parameters like 6 or 49 in variables to keep your software flexible.</para> + <tip> <orderedlist> <listitem> - <para>Store parameters like 6 or 49 in variables to keep your software flexible.</para> - </listitem> - - <listitem> - <para>Use <code>long</code> variables when appropriate to avoid overflow errors.</para> + <para>Why is it a bad idea to <abbrev>e.g.</abbrev> compute <inlineequation> + <m:math display="inline"> + <m:mi>49!</m:mi> + </m:math> + </inlineequation> directly even when using long variables? Remember <xref linkend="sd1_fig_byteOverflow"/>.</para> </listitem> <listitem> - <para>Even if using variables of type <code>long</code> you will not be able to represent e.g. <quote>90!</quote> due to an arithmetic overflow. This problem can be avoided by simplifying <inlineequation> + <para>Arithmetic overflow problems can be avoided by simplifying <inlineequation> <m:math display="inline"> <m:mfrac> <m:mrow> @@ -2979,28 +3013,295 @@ System.out.println(limit + " = " + sum);</programlisting> </m:mrow> </m:mfrac> </m:math> - </inlineequation> beforehand.</para> - </listitem> + </inlineequation> beforehand. You may want to write down an explicit example like <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mrow> + <m:mrow> + <m:mo>(</m:mo> - <listitem> - <para>The following loop allows for computing a given number's factorial:</para> + <m:mrow> + <m:mtable> + <m:mtr> + <m:mtd> + <m:mi>8</m:mi> + </m:mtd> + </m:mtr> + + <m:mtr> + <m:mtd> + <m:mi>3</m:mi> + </m:mtd> + </m:mtr> + </m:mtable> + </m:mrow> - <programlisting language="java">long factorial = 1; -for (int i = 2; i <= number; i++) { - factorial *= i; -}</programlisting> + <m:mo>)</m:mo> + </m:mrow> + </m:mrow> + + <m:mo>=</m:mo> + + <m:mfrac> + <m:mrow> + <m:mi>8</m:mi> + + <m:mo>!</m:mo> + </m:mrow> - <para>Use similar loops for calculation.</para> + <m:mrow> + <m:mrow> + <m:mi>3</m:mi> + + <m:mo>!</m:mo> + </m:mrow> + + <m:mo>â¢</m:mo> + + <m:mrow> + <m:mrow> + <m:mo>(</m:mo> + + <m:mrow> + <m:mi>8</m:mi> + + <m:mo>-</m:mo> + + <m:mi>3</m:mi> + </m:mrow> + + <m:mo>)</m:mo> + </m:mrow> + + <m:mo>!</m:mo> + </m:mrow> + </m:mrow> + </m:mfrac> + </m:mrow> + </m:math> + </inlineequation> to learn about cancellation of contributing factors.</para> </listitem> </orderedlist> </tip> </question> <answer> - <para>By simple fraction cancellation we get:</para> + <para>Consider the following snippet:</para> + + <programlisting language="java">int product = 1; +for (int i = 1; i < 50; i++) { + product *= i; + System.out.println(i + "! == " + product); +}</programlisting> + + <para>This results in:</para> + + <screen>1! == 1 +2! == 2 +3! == 6 +4! == 24 +5! == 120 +6! == 720 +7! == 5040 +8! == 40320 +9! == 362880 +10! == 3628800 +11! == 39916800 +12! == 479001600 +13! == 1932053504 +... +49! == 0</screen> + + <para>Only results up to <inlineequation> + <m:math display="inline"> + <m:mi>12!</m:mi> + </m:math> + </inlineequation> are correct: The next term <inlineequation> + <m:math display="inline"> + <m:mi>13!</m:mi> + </m:math> + </inlineequation> equals 6227020800 which is ways beyond <code xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#MAX_VALUE">Integer.MAX_VALUE</code> == 2147483647 giving rise to a (silent) arithmetic overflow. But even declaring <code>long product = 1</code> does not help much:</para> + + <screen>1! == 1 +2! == 2 +3! == 6 +4! == 24 +5! == 120 +6! == 720 +7! == 5040 +8! == 40320 +9! == 362880 +10! == 3628800 +11! == 39916800 +12! == 479001600 +13! == 6227020800 +14! == 87178291200 +15! == 1307674368000 +16! == 20922789888000 +17! == 355687428096000 +18! == 6402373705728000 +19! == 121645100408832000 +20! == 2432902008176640000 +21! == -4249290049419214848 +... +49! == 8789267254022766592</screen> + + <para>This time <code>20! == 2432902008176640000</code> is the last correct value being smaller than <code xlink:href="https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html#MAX_VALUE">Long.MAX_VALUE</code> == 9223372036854775807.</para> + + <para>Fortunately we have another option. Consider an example:</para> + + <informalequation> + <m:math display="block"> + <m:mrow> + <m:mrow> + <m:mrow> + <m:mo>(</m:mo> + + <m:mrow> + <m:mtable> + <m:mtr> + <m:mtd> + <m:mi>8</m:mi> + </m:mtd> + </m:mtr> + + <m:mtr> + <m:mtd> + <m:mi>3</m:mi> + </m:mtd> + </m:mtr> + </m:mtable> + </m:mrow> + + <m:mo>)</m:mo> + </m:mrow> + </m:mrow> + + <m:mo>=</m:mo> + + <m:mfrac> + <m:mrow> + <m:mrow> + <m:mi>8</m:mi> + + <m:mo>×</m:mo> + + <m:mi>7</m:mi> + + <m:mo>×</m:mo> + + <m:mi>6</m:mi> + + <m:mo>×</m:mo> + + <m:mi>5</m:mi> + + <m:mo>×</m:mo> + + <m:mi>4</m:mi> + + <m:mo>×</m:mo> + + <m:mi>3</m:mi> + + <m:mo>×</m:mo> + + <m:mi>2</m:mi> + + <m:mo>×</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> + + <m:mrow> + <m:mrow> + <m:mo>(</m:mo> + + <m:mi>3</m:mi> + + <m:mo>×</m:mo> + + <m:mi>2</m:mi> + + <m:mo>×</m:mo> + + <m:mi>1</m:mi> + + <m:mo>)</m:mo> + </m:mrow> + + <m:mo>×</m:mo> + + <m:mrow> + <m:mo>(</m:mo> + + <m:mi>5</m:mi> + + <m:mo>×</m:mo> + + <m:mi>4</m:mi> + + <m:mo>×</m:mo> + + <m:mi>3</m:mi> + + <m:mo>×</m:mo> + + <m:mi>2</m:mi> + + <m:mo>×</m:mo> + + <m:mi>1</m:mi> + + <m:mo>)</m:mo> + </m:mrow> + </m:mrow> + </m:mfrac> + + <m:mo>=</m:mo> + + <m:mfrac> + <m:mrow> + <m:mrow> + <m:mi>8</m:mi> + + <m:mo>×</m:mo> + + <m:mi>7</m:mi> + + <m:mo>×</m:mo> + + <m:mi>6</m:mi> + </m:mrow> + </m:mrow> + + <m:mrow> + <m:mrow> + <m:mi>3</m:mi> + + <m:mo>×</m:mo> + + <m:mi>2</m:mi> + + <m:mo>×</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> + </m:mfrac> + + <m:mo>=</m:mo> + + <m:mi>56</m:mi> + </m:mrow> + </m:math> + </informalequation> + + <para>We generalize this fraction cancellation example:</para> <equation xml:id="sd1EqnBinomialCalculate"> - <title>Calculating binomials in a slightly more efficient manner rather than just computing three factorials.</title> + <title>Calculating binomials by cancelling common factors.</title> <m:math display="block"> <m:mrow> @@ -3229,7 +3530,7 @@ for (int i = 2; i <= number; i++) { </m:math> </equation> - <para>As an example we have:</para> + <para>And thus:</para> <informalequation> <m:math display="block"> @@ -3321,7 +3622,7 @@ for (int i = 2; i <= number; i++) { </m:math> </informalequation> - <para>Calculating numerator and denominator in two separate loops a possible implementation reads:</para> + <para>We thus calculate numerator and denominator in two separate loops:</para> <programlisting language="java">public class Lottery {