diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index feccfc98fe40d02e34a3f480cb664e6f5979df76..fcc4019658b7781b2ed0881b9bd06ad26eb7e0be 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -457,8 +457,8 @@ if (count = 4) { // is count equal to 4? 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> + <para>Explain its cause in detail by examining the <code + language="java">count = 4</code> expression.</para> <tip> <para><xref linkend="glo_Java"/> provides two similar looking @@ -469,8 +469,9 @@ Found: <emphasis role="red">int</emphasis></screen> </question> <answer> - <para>Java provides two seemingly similar but - <emphasis>completely</emphasis> unrelated operators:</para> + <para>The two operators <code language="java">=</code> and <code + language="java">==</code> are <emphasis>completely</emphasis> + unrelated:</para> <glosslist> <glossentry> @@ -520,6 +521,13 @@ Found: <emphasis role="red">int</emphasis></screen> <para>In math <quote>=</quote> denotes the equality of objects <abbrev>e.g.</abbrev> values, sets, functions and so on.</para> + + <para>More formally the expression <code + language="java">count = 4</code> is of type <code + language="java">int</code> evaluating to 4 (surprise!). + However an <code language="java">if (...)</code> operates + on <code language="java">boolean</code> values only. We + thus must not supply expressions of any other type.</para> </glossdef> </glossentry> @@ -541,17 +549,22 @@ Found: <emphasis role="red">int</emphasis></screen> identity.</para> </listitem> </itemizedlist> + + <para>In particular an expression like <code + language="java">count == 4</code> is of type boolean: + Either <code language="java">true</code> or <code + language="java">false</code>.</para> </glossdef> </glossentry> </glosslist> - <para>Thus <code language="java">(count = 4)</code> is an + <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; -int countAssignment = (count = 4); // Assigning expression count = 4 to variable countAssignment +int countAssignment = (count = 4); // Assigning expression count = 4 to variable countAssignment. if (countAssignment) { <emphasis role="red">// Error: An int is not a boolean!</emphasis> System.out.println("count is o.K."); @@ -677,8 +690,8 @@ int main(int argc, char **args) { 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> + avoiding <code language="java"><emphasis role="red">else + if</emphasis>(...) {}</code> branch statements!</para> <tip> <para>As the title suggests you may want to nest an @@ -690,9 +703,9 @@ int main(int argc, char **args) { <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> + language="java">if(...){ ...} else {...}</code> statement by + moving the final <code language="java">else</code> block into + the nested one.</para> <programlisting language="java">if (a < 7) { System.out.println("o.K."); @@ -1026,19 +1039,86 @@ You entered 123</screen><para>See <methodname <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>A lecturer marks an exam having a maximum of 12 reachable + points:</para> + + <informaltable border="1" width="50%"> + <tr> + <th>Name</th> + + <th>Mark</th> + </tr> + + <tr> + <td>Aaron</td> + + <td>3</td> + </tr> - <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> + <tr> + <td>Maureen</td> + + <td>11</td> + </tr> - <para>After completing his marking our lecturer wants to add - three more points to all participants without breaching the 12 - point limit.</para> + <tr> + <td>Sally</td> + + <td>1</td> + </tr> + + <tr> + <td>...</td> + + <td>...</td> + </tr> + </informaltable> + + <para>The lecturer is dissatisfied with the overall result. He + wants to add 3 bonus points but still keeping the maximum of 12 + points to be reachable:</para> + + <informaltable border="1" width="50%"> + <tr> + <th>Name</th> + + <th>Mark</th> + + <th>3 bonus points augmented mark</th> + </tr> + + <tr> + <td>Aaron</td> + + <td>3</td> + + <td>6</td> + </tr> + + <tr> + <td>Maureen</td> + + <td>11</td> + + <td><emphasis role="red">12</emphasis></td> + </tr> + + <tr> + <td>Sally</td> + + <td>1</td> + + <td>4</td> + </tr> + + <tr> + <td>...</td> + + <td>...</td> + + <td>...</td> + </tr> + </informaltable> <para>Complete the following code by assigning this modified number of points to the variable <code @@ -1046,15 +1126,17 @@ You entered 123</screen><para>See <methodname <programlisting language="java">public static void main(String[] args) { - int pointsReached = 1; - int maximumPoints = 12; - int pointsToAdd = 3; + int mark = 1; // May range from 0 to 12 points + - final int newResult; + final int maximumPoints = 12; + final int pointsToAdd = 3; - // TODO: Assignment to variable newResult + final int augmentedMark; - System.out.println("New Result:" + newResult); + // TODO: Assignment to variable augmentedMark + + System.out.println("New marking:" + augmentedMark); }</programlisting> </question> @@ -1070,22 +1152,21 @@ You entered 123</screen><para>See <methodname final int maximumPoints = 12; final int pointsToAdd = 3; - final int newResult; + final int augmentedMark; if (maximumPoints <= pointsReached + pointsToAdd) { - newResult = maximumPoints; + augmentedMark = maximumPoints; } else { - newResult = pointsReached + pointsToAdd; + augmentedMark = pointsReached + pointsToAdd; } - System.out.println("New Result:" + newResult); + System.out.println("New marking:" + augmentedMark); }</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>The augmented marks equal the minimum of <code + language="java">(pointsReached + pointsToAdd</code>) and <code + language="java">maximumPoints</code>. We may thus code as + well:</para> <programlisting language="java">public static void main(String[] args) { @@ -1093,10 +1174,10 @@ You entered 123</screen><para>See <methodname final int maximumPoints = 12; final int pointsToAdd = 3; - final int newResult = <link + final int augmentedMark = <link 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); + System.out.println("New Result:" + augmentedMark); }</programlisting> <para>You will fully understand the above expression <classname @@ -1650,14 +1731,16 @@ Tuesday</screen> </question> <answer> - <para>With respect to <xref linkend="sd1_fig_switch"/> a small - modification is being required</para> + <para>With respect to <xref linkend="sd1_fig_switch"/> two + modification are being required:</para> - <programlisting language="java">... -switch(day % 7) { + <programlisting language="none">... +switch(<emphasis role="red">day % 7</emphasis>) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; -...</programlisting> + ... + <emphasis role="red">case 0</emphasis>: System.out.println("Sunday"); break; +}</programlisting> </answer> </qandaentry> </qandadiv> @@ -1709,8 +1792,10 @@ Unknown day name 'July'</screen> <tip> <orderedlist> <listitem> - <para>Starting from <xref linkend="glo_Java"/> 7 switch - statements using strings are allowed.</para> + <para>Starting from <xref linkend="glo_Java"/> 7 <classname + xlink:href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html">String</classname> + based <code language="java">switch</code> statements are + allowed.</para> </listitem> <listitem> @@ -1815,12 +1900,16 @@ Midweek</screen> final String day = scan.next(); switch(day) { case "Monday": System.out.println("Start of work week"); break; + case "Tuesday": case "Wednesday": case "Thursday": System.out.println("Midweek"); break; + case "Friday": System.out.println("End of work week"); break; + case "Saturday": case "Sunday": System.out.println("Weekend"); break; + default: System.out.println("Unknown day name " + day); break; } }</programlisting> @@ -1905,20 +1994,21 @@ public class LeapYear { public static void main(String[] args) { - final Scanner scan = new Scanner(System.in); // Read user input - System.out.print("Enter a year:>"); - final int year = scan.nextInt(); - scan.close(); - - if (0 == year % 400) { <emphasis role="bold"> // Every 400 years we do have a leap year.</emphasis> - System.out.println( - "Year " + year + " is a leap year"); - } else if (0 == year % 4 && <emphasis role="bold"> // Every 4 years we do have a leap year</emphasis> - 0 != year % 100) { <emphasis role="bold"> // unless year is a multiple of 100.</emphasis> - System.out.println("Year " + year + " is a leap year"); - } else { - System.out.println("Year " + year + " is no leap year"); - } + try (final Scanner scan = new Scanner(System.in)) { + + System.out.print("Enter a year:>"); + final int year = scan.nextInt(); + + if (0 == year % 400) { <emphasis role="bold"> // Every 400 years we do have a leap year.</emphasis> + System.out.println( + "Year " + year + " is a leap year"); + } else if (0 == year % 4 && <emphasis role="bold"> // Every 4 years we do have a leap year</emphasis> + 0 != year % 100) { <emphasis role="bold"> // unless year is a multiple of 100.</emphasis> + System.out.println("Year " + year + " is a leap year"); + } else { + System.out.println("Year " + year + " is no leap year"); + } + } } }</programlisting> @@ -1929,12 +2019,7 @@ public class LeapYear { branch into one resolves the issue:</para> <programlisting language="java">public static void main(String[] args) { - - final Scanner scan = new Scanner(System.in); // Read user input - System.out.print("Enter a year:>"); - final int year = scan.nextInt(); - scan.close(); - + ... if (0 == year % 400 || <emphasis role="bold">// Every 400 years we do have a leap year.</emphasis> (0 == year % 4 && <emphasis role="bold">// Every 4 years we do have a leap year</emphasis> 0 != year % 100)) { <emphasis role="bold">// unless year is a multiple of 100.</emphasis> @@ -1942,20 +2027,20 @@ public class LeapYear { } else { System.out.println("Year " + year + " is no leap year"); } -}</programlisting> +...</programlisting> <para>Some enthusiasts prefer compact expressions at the expense - of readability («Geek syndrome»). The following code is fully - equivalent albeit syntactically different:</para> + of readability (<quote>Geek syndrome</quote>) sometimes referred + to as <quote>syntactic sugar</quote>. The following code based on + the <code language="java" + xlink:href="https://www.geeksforgeeks.org/java-ternary-operator-with-examples">...? + ...: ...</code> operator is fully equivalent:</para> - <programlisting language="java">final Scanner scan = new Scanner(System.in); // Read user input -System.out.print("Enter a year:>"); -final int year = scan.nextInt(); + <programlisting language="java">... System.out.println("Year " + year + (year % 400 == 0 || year % 4 == 0 && 0 != year % 100 ? " is a leap year" : " is no leap year")); - -scan.close();</programlisting> +}</programlisting> </answer> </qandaentry> </qandadiv> @@ -2102,12 +2187,104 @@ Do not copy!</screen></td> <programlisting language="java">int threeSeries = 1; -while ((threeSeries *=3) < 100); +while ((threeSeries *=3 ) < 100); System.out.println(threeSeries);</programlisting> <para>Exercise: Guess resulting output.</para> </figure> + + <qandaset defaultlabel="qanda" xml:id="sd1_statements_qanda_factorial"> + <title>Calculating factorial</title> + + <qandadiv> + <qandaentry> + <question> + <para>The factorial <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mi>n</m:mi> + + <m:mo>!</m:mo> + </m:mrow> + </m:math> + </inlineequation> of a given integer n is being defined as the + following product:</para> + + <informalequation> + <m:math display="block"> + <m:mrow> + <m:mrow> + <m:mi>n</m:mi> + + <m:mo>!</m:mo> + </m:mrow> + + <m:mo>=</m:mo> + + <m:mrow> + <m:mi>n</m:mi> + + <m:mo>×</m:mo> + + <m:mrow> + <m:mo>(</m:mo> + + <m:mrow> + <m:mi>n</m:mi> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + + <m:mo>)</m:mo> + </m:mrow> + + <m:mo>×</m:mo> + + <m:mo>⋯</m:mo> + + <m:mo>×</m:mo> + + <m:mi>2</m:mi> + + <m:mo>×</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> + </m:math> + </informalequation> + + <para>Write an application asking a user for an integer value + and calculate the corresponding factorial e.g.:</para> + + <screen>Enter an integer value: 5 +5! == 120</screen> + </question> + + <answer> + <programlisting language="java">public static void main(String[] args) { + try (final Scanner scan = new Scanner(System.in)) { + + System.out.print("Enter an integer value: "); + final int value = scan.nextInt(); + + long factorial = value; + int i = value; + + while (1 < --i) { + factorial *= i; + } + + System.out.println(value + "! == " + factorial); + } +}</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> </section> <section xml:id="sd1_sect_doWhile">