From 432ea1e2c6c2e8a41e46b943c835e7bbe6179a91 Mon Sep 17 00:00:00 2001
From: "Dr. Martin Goik" <goik@hdm-stuttgart.de>
Date: Thu, 18 Apr 2019 16:34:11 +0200
Subject: [PATCH] Np slides generated for figures in qandasets

---
 Doc/Sd1/languageFundamentals.xml           | 141 ++++++++++++++-------
 ws/Docbook/CustomLayer/slide/slideHdm.xsl  |   4 +-
 ws/Docbook/Preprocess/Xsl/docbook2html.xsl |   2 +-
 3 files changed, 98 insertions(+), 49 deletions(-)

diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml
index 1c78d3796..4520f894f 100644
--- a/Doc/Sd1/languageFundamentals.xml
+++ b/Doc/Sd1/languageFundamentals.xml
@@ -4459,18 +4459,24 @@ _____________________________________
     </figure>
 
     <qandaset defaultlabel="qanda" xml:id="sd1_qanda_expressionTypes">
-      <title>Expressions and type</title>
+      <title><code language="java">int</code> to <code
+      language="java">short</code> assignment</title>
 
       <qandadiv>
         <qandaentry>
           <question>
             <para>Consider the following code segment:</para>
 
-            <programlisting language="java">short a = 4; <co
-                linkends="sd1_co_expressionTypeIntToShort-1"
-                xml:id="sd1_co_expressionTypeIntToShort-1-co"/>
+            <figure xml:id="sd1_qanda_expressionTypesListing">
+              <title><code language="java">int</code> expression
+              assignment</title>
+
+              <programlisting language="java">short a = 4; <co
+                  linkends="sd1_co_expressionTypeIntToShort-1"
+                  xml:id="sd1_co_expressionTypeIntToShort-1-co"/>
 short sum = a + 7; <co linkends="sd1_co_expressionTypeIntToShort-2"
-                xml:id="sd1_co_expressionTypeIntToShort-2-co"/></programlisting>
+                  xml:id="sd1_co_expressionTypeIntToShort-2-co"/></programlisting>
+            </figure>
 
             <calloutlist>
               <callout arearefs="sd1_co_expressionTypeIntToShort-1-co"
@@ -4493,33 +4499,26 @@ short sum = a + 7; <co linkends="sd1_co_expressionTypeIntToShort-2"
             <para>On contrary the following line will compile
             flawlessly:</para>
 
-            <programlisting language="java">short sum = 4 + 7;</programlisting>
+            <figure xml:id="sd1_listing_staticShortAssignment">
+              <title>Constant expression assignment</title>
+
+              <programlisting language="java">short sum = 4 + 7;</programlisting>
+            </figure>
 
             <para>Explain this strange behaviour.</para>
           </question>
 
           <answer>
-            <para>The expression <code language="java">a + 7</code> contains a
-            variable <quote>a</quote> of type <code language="java"
-            xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code>
-            to the plus operator's left and an <code language="java"
-            xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code>
-            literal <quote>7</quote> to its right. Thus the plus operator
-            yields an <code language="java"
-            xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code>
-            result (the <quote>biggest</quote> type of both operands) which
-            cannot be assigned to the variable <code
-            language="java">sum</code> of type <code language="java"
-            xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code>.</para>
-
-            <para>The very same argument seems to apply for the expression
-            <code language="java">4 + 7</code> even more: This time the plus
-            operator acts on even two <code language="java"
+            <para>Considering <code language="java">short sum = 4 + 7</code>
+            the plus operator acts on two <code language="java"
             xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code>
-            operands. However in contrast to the former assignment this time
-            <emphasis>both</emphasis> operands are static. The 4 + 7
-            expression's value of 11 is being calculated at compile time.
-            Since 11 of type <code language="java">int</code> is within
+            operands. Thus the expression <code language="java">4 + 7</code>
+            is of type <code language="java">int</code>. Both operands are
+            static <abbrev>i.e.</abbrev> their values are known at
+            <emphasis>compile time</emphasis> (rather than at <emphasis>run
+            time</emphasis>). The <code language="java">4 + 7</code>
+            expression's value can thus be evaluated at compile time. The
+            resulting value of <code language="java">11</code> is within
             <inlineequation>
                 <m:math display="inline">
                   <m:mrow>
@@ -4558,11 +4557,61 @@ short sum = a + 7; <co linkends="sd1_co_expressionTypeIntToShort-2"
                     <m:mo>]</m:mo>
                   </m:mrow>
                 </m:math>
-              </inlineequation> the compiler will safely cast this value to
-            the <code language="java"
+              </inlineequation> so the compiler will safely cast and assign it
+            to the variable <code language="java">sum of type</code> <code
+            language="java"
+            xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code>
+            without any information loss.</para>
+
+            <para>The expression <code language="java">a + 7</code> contains a
+            variable <quote>a</quote> of type <code language="java"
             xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code>
-            variable <code language="java">sum</code> without information
-            loss.</para>
+            to the plus operator's left and an <code language="java"
+            xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code>
+            literal <quote>7</quote> to its right. Thus the expression is of
+            type <code language="java"
+            xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-C">int</code>
+            as well. However a compiler will consider the variable <code
+            language="java">a</code>'s value to be unknown within the given
+            expression at compile time (albeit humans will safely assume a
+            value of 4 by virtue of the preceding statement). Therefore
+            variable <code language="java">a</code>'s value could potentially
+            exceed a <code language="java"
+            xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-B">short</code>'s
+            range of values and thus cannot be assigned to the variable <code
+            language="java">sum</code> in a safe manner.</para>
+          </answer>
+        </qandaentry>
+      </qandadiv>
+    </qandaset>
+
+    <qandaset defaultlabel="qanda" xml:id="sd1_qanda_expressionTypesFinal">
+      <title><code language="java">int</code> to <code
+      language="java">short</code> assignment using <code
+      language="java">final</code></title>
+
+      <qandadiv>
+        <qandaentry>
+          <question>
+            <para>We reconsider a variant of <xref
+            linkend="sd1_qanda_expressionTypesListing"/>:</para>
+
+            <programlisting language="none"><emphasis role="red">final</emphasis> short a = 4;
+short sum = a + 7;</programlisting>
+
+            <para>This time the code compiles flawlessly. Explain the
+            underlying reason being related to the <code
+            language="java">final</code> modifier's role.</para>
+          </question>
+
+          <answer>
+            <para>A final variable's value cannot change. Thus the expression
+            a + 7 is fully equivalent to 4 + 7 and can thus be evaluated at
+            <emphasis>compile time</emphasis>. Like in the preceding <xref
+            linkend="sd1_listing_staticShortAssignment"/> code sample the
+            resulting value of <code language="java">11</code> neatly fits
+            into the variable <code language="java">sum</code> of type <code
+            language="java">short</code>.</para>
           </answer>
         </qandaentry>
       </qandadiv>
@@ -5193,7 +5242,7 @@ System.out.println("Difference: " + difference);</programlisting><screen>Differe
     </qandaset>
 
     <qandaset defaultlabel="qanda" xml:id="sw1QandaCircleAreaFinal">
-      <title>Calculating the area of a circle avoiding accidental
+      <title>Calculating a circle's area avoiding accidental
       redefinition</title>
 
       <qandadiv>
@@ -5213,8 +5262,8 @@ System.out.println("Difference: " + difference);</programlisting><screen>Differe
 }</programlisting>
 
             <para>Though there is nothing wrong with this approach it is error
-            prone: In a similar program a careless programmer accidentally
-            redefine the value of <code language="java">pi</code>:</para>
+            prone: A careless programmer might accidentally redefine the value
+            of <code language="java">pi</code>:</para>
 
             <programlisting language="java">double pi = 3.141592653589793;
 
@@ -5280,7 +5329,7 @@ System.out.println("A circle of radius " + radius + " will cover an area of " +
                 time error:</para>
 
                 <para><computeroutput>Cannot assign a value to final variable
-                'PI'</computeroutput></para>
+                'pi'</computeroutput></para>
               </callout>
             </calloutlist>
 
@@ -5289,8 +5338,8 @@ System.out.println("A circle of radius " + radius + " will cover an area of " +
             xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4">final</code>
             declaring it to remain constant.</para>
 
-            <para>In addition we may refactor our variable <code
-            language="java">pi</code> to use capital letters reflecting the
+            <para>In addition refactoring our variable <code
+            language="java">pi</code> using capital letters reflecting the
             naming convention for constants. This is does not change program
             execution in any way but enhances code readability:</para>
 
@@ -5300,7 +5349,7 @@ double radius = 2.3; // Computing a circle's area
 System.out.println("A circle of radius " + radius + " will cover an area of " +
   <emphasis role="red">PI</emphasis> * radius * radius);
 
-<emphasis role="red">PI</emphasis> = -4;
+<emphasis role="red">PI</emphasis> = -4; <emphasis role="red">// Compile time error</emphasis>
 
 radius = 1.8;
 
@@ -5359,22 +5408,22 @@ final int secondsElapsed =
 
 System.out.println(secondsElapsed);</programlisting>
 
-            <para>This requires four additions and 10 multiplications. We may
-            use:</para>
+            <para>This requires four additions and 10 multiplications. <link
+            xlink:href="https://eli.thegreenplace.net/2010/03/30/horners-rule-efficient-evaluation-of-polynomials#horner-s-rule">Horner's
+            rule</link> allows for:</para>
 
             <programlisting language="java">...
 final int secondsElapsed =
   seconds + 60 * (minutes + 60 * (hours + 24 * (days + 7 * weeks)));
 ...</programlisting>
 
-            <para>This leaves us with four additions but just 4 instead of 10
-            multiplications.</para>
+            <para>This still leaves us with four additions but just 4 instead
+            of 10 multiplications.</para>
 
             <para>Within the given context the difference in execution time
-            can be neglected. But in a different application the calculation
-            in question might have to be repeated millions of times giving
-            rise to a substantial gain with respect to total execution
-            time.</para>
+            can be neglected. But in a larger application the calculation in
+            question might have to be repeated millions of times giving rise
+            to a substantial gain with respect to execution time.</para>
           </answer>
         </qandaentry>
       </qandadiv>
@@ -5540,7 +5589,7 @@ System.out.println("A circle of radius " + radius + " will cover an area of " +
       <qandadiv>
         <qandaentry>
           <question>
-            <para>This exercise consists of two parts:</para>
+            <para>This is a two part exercise:</para>
 
             <orderedlist>
               <listitem>
diff --git a/ws/Docbook/CustomLayer/slide/slideHdm.xsl b/ws/Docbook/CustomLayer/slide/slideHdm.xsl
index b81a3ec62..a5390eb29 100644
--- a/ws/Docbook/CustomLayer/slide/slideHdm.xsl
+++ b/ws/Docbook/CustomLayer/slide/slideHdm.xsl
@@ -58,7 +58,7 @@
           <div class="reveal">
             <!-- Wrap all slides in a single "slides" class -->
             <div class="slides">
-              <xsl:apply-templates select=".//d:figure" mode="slideDescendants"/>
+              <xsl:apply-templates select=".//d:figure[not (ancestor::d:qandaset)]" mode="slideDescendants"/>
             </div>
           </div>
           
@@ -222,7 +222,7 @@
      
     <xsl:variable name="currentFigureId" select="generate-id()"/>
     <xsl:variable name="followupQandasets" 
-      select="following::d:qandaset[generate-id(preceding::d:figure[1]) = $currentFigureId]"/>
+      select="following::d:qandaset[generate-id(preceding::d:figure[not(ancestor::d:qandaset)][1]) = $currentFigureId]"/>
     <xsl:if test="$followupQandasets">
       
       <xsl:if test="10 &lt; count($followupQandasets)">
diff --git a/ws/Docbook/Preprocess/Xsl/docbook2html.xsl b/ws/Docbook/Preprocess/Xsl/docbook2html.xsl
index 02888ad7b..636e2ad68 100644
--- a/ws/Docbook/Preprocess/Xsl/docbook2html.xsl
+++ b/ws/Docbook/Preprocess/Xsl/docbook2html.xsl
@@ -27,7 +27,7 @@
     
   </xsl:template>
   
-  <xsl:template match="db:figure/db:title">
+  <xsl:template match="db:figure[not(ancestor::db:qandaset)]/db:title">
     <xsl:variable name="slideContainerId" select="ancestor::*[contains(@annotations, 'slide')]/@xml:id"/>
     
     <title>
-- 
GitLab