From 7a2e1412295cfdac177e2ab4e0af0e76cfb3a2c9 Mon Sep 17 00:00:00 2001
From: "Dr. Martin Goik" <goik@hdm-stuttgart.de>
Date: Sat, 7 Dec 2019 19:28:46 +0100
Subject: [PATCH] Cosmetics +  two new equals() / enum exercises

---
 .../de/hdm_stuttgart/mi/sd1/use_enum/Day.java |   2 +-
 Doc/Sd1/deployment.xml                        |   2 +-
 Doc/Sd1/gettingStarted.xml                    |  14 +-
 Doc/Sd1/inheritance.xml                       | 150 ++++++++++++++++++
 Doc/Sd1/objectsClasses.xml                    |  29 +++-
 5 files changed, 186 insertions(+), 11 deletions(-)

diff --git a/Doc/Sd1/Ref/ObjectsAndClasses/Enum/Intro/src/main/java/de/hdm_stuttgart/mi/sd1/use_enum/Day.java b/Doc/Sd1/Ref/ObjectsAndClasses/Enum/Intro/src/main/java/de/hdm_stuttgart/mi/sd1/use_enum/Day.java
index 6b649cd3f..03313efa1 100644
--- a/Doc/Sd1/Ref/ObjectsAndClasses/Enum/Intro/src/main/java/de/hdm_stuttgart/mi/sd1/use_enum/Day.java
+++ b/Doc/Sd1/Ref/ObjectsAndClasses/Enum/Intro/src/main/java/de/hdm_stuttgart/mi/sd1/use_enum/Day.java
@@ -26,7 +26,7 @@ public enum Day {
         return name;
     }
 
-    public static String getItalianDayname(final Day day) {
+    public static String getItalianDayName(final Day day) {
 
         switch (day) {
             case MONDAY:    return "Lunedì";
diff --git a/Doc/Sd1/deployment.xml b/Doc/Sd1/deployment.xml
index ce63f6421..ce392181b 100644
--- a/Doc/Sd1/deployment.xml
+++ b/Doc/Sd1/deployment.xml
@@ -8,7 +8,7 @@
          xmlns:m="http://www.w3.org/1998/Math/MathML"
          xmlns:html="http://www.w3.org/1999/xhtml"
          xmlns:db="http://docbook.org/ns/docbook">
-  <title>Application deployment I (8.12.)</title>
+  <title>Application deployment I</title>
 
   <section xml:id="sd1DeployPrepare">
     <title>Preparations</title>
diff --git a/Doc/Sd1/gettingStarted.xml b/Doc/Sd1/gettingStarted.xml
index 25783b8de..1c9d31095 100644
--- a/Doc/Sd1/gettingStarted.xml
+++ b/Doc/Sd1/gettingStarted.xml
@@ -2361,7 +2361,7 @@ nano ~/.m2/settings.xml <co linkends="sd1_fig_mavenMiRepo-2"
             refers to a <orgname
             xlink:href="https://www.sonatype.com">Sonatype</orgname>
             <productname>Nexus repository manager</productname> instance
-            hosting both supplementary <link
+            hosting supplementary <link
             xlink:href="https://maven.apache.org/guides/introduction/introduction-to-archetypes.html">Maven
             archetypes</link> and <link
             xlink:href="https://maven.apache.org/glossary.html">artifacts</link>.</para>
@@ -2711,7 +2711,7 @@ Generating /ma/goik/First/target/site/apidocs/help-doc.html...
             <calloutlist role="slideExclude">
               <callout arearefs="sd1_fig_mavenIdeaMiArchetypeRepo_co_10"
                        xml:id="sd1_fig_mavenIdeaMiArchetypeRepo-1">
-                <para>Archetype URI:</para>
+                <para>You may want to copy the archetype URI below:</para>
 
                 <para><uri
                 xlink:href="https://maven.mi.hdm-stuttgart.de/nexus/repository/mi-maven/archetype-catalog.xml">https://maven.mi.hdm-stuttgart.de/nexus/repository/mi-maven/archetype-catalog.xml</uri></para>
@@ -2767,7 +2767,7 @@ Generating /ma/goik/First/target/site/apidocs/help-doc.html...
 
               <callout arearefs="sd1_fig_mavenIdeaDefineProjectMetadata-2-co"
                        xml:id="sd1_fig_mavenIdeaDefineProjectMetadata-2.2">
-                <para>Tick <guibutton>Create from archetype</guibutton>
+                <para>Tick the <option>Create from archetype</option>
                 option.</para>
               </callout>
 
@@ -2777,9 +2777,11 @@ Generating /ma/goik/First/target/site/apidocs/help-doc.html...
                 <option>de.hdm_stuttgart.mi</option>.</para>
 
                 <caution>
-                  <para>Mind the underscore <quote>_</quote>. Do not use a
-                  dash as in the corresponding <uri>mi.hdm-stuttgart.de</uri>
-                  URL.</para>
+                  <para>Mind the underscore <quote>_</quote>! Do not use a
+                  dash (-) as in the corresponding <uri>mi.hdm<emphasis
+                  role="red">-</emphasis>stuttgart.de</uri> URL. Doing so
+                  would mess with <xref linkend="glo_Java"/>'s minus operator
+                  when it comes to package names.</para>
                 </caution>
               </callout>
 
diff --git a/Doc/Sd1/inheritance.xml b/Doc/Sd1/inheritance.xml
index 010679cf6..d2121e97c 100644
--- a/Doc/Sd1/inheritance.xml
+++ b/Doc/Sd1/inheritance.xml
@@ -429,6 +429,156 @@ public <link
         </callout>
       </calloutlist>
     </figure>
+
+    <qandaset defaultlabel="qanda" xml:id="sd1_qanda_stringEqualProblem">
+      <title>Let me in, please!</title>
+
+      <qandadiv>
+        <qandaentry>
+          <question>
+            <para>Consider the following snippet:</para>
+
+            <programlisting language="java">try (final Scanner scan = new Scanner(System.in)) {
+  do {
+    System.out.print("Please enter password: ");
+    final String password = scan.nextLine();
+    if (password == "secret") {
+      break;  // Leave enclosing do ... while loop
+    } else {
+      System.out.println("Sorry, please try again");
+    }
+  } while (true);
+  System.out.println("You made it!");
+  // ...
+}</programlisting>
+
+            <para>Describe the above code's intended behaviour. Will it
+            succeed? Execute the above code and provide a clue for correcting
+            the underlying flaw.</para>
+          </question>
+
+          <answer>
+            <para>The user is being asked for a password. Only when entering
+            <code>"secret"</code> access shall be granted.</para>
+
+            <para>Unfortunately comparing the user's input and the
+            corresponding password is seriously flawed:</para>
+
+            <programlisting language="java">...
+  if (password == "secret")
+...</programlisting>
+
+            <para>On execution the string literal "secret" will be represented
+            as a <classname
+            xlink:href="https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/String.html">String</classname>
+            object in memory. Each new user input string will be represented
+            as a <emphasis>different</emphasis> <classname
+            xlink:href="https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/String.html">String</classname>
+            object in memory as well.</para>
+
+            <para>Unfortunately the usual <quote>==</quote> operator only
+            works as expected for the eight built in primitive <xref
+            linkend="glo_Java"/> types. With respect to class instances a
+            variable holds a reference to an object rather than the object
+            itself. Applying the <quote>==</quote> operator compares for
+            object identity rather than object equality.</para>
+
+            <para>Two different <classname
+            xlink:href="https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/String.html">String</classname>
+            instances albeit being different object instances may off course
+            be equal with respect to their <quote>payload</quote> namely the
+            strings they both represent. Comparing for object equality rather
+            than for object identity in <xref linkend="glo_Java"/> requires
+            overriding the <methodname
+            xlink:href="https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/Object.html#equals(java.lang.Object)">Object.equals(Object
+            o)</methodname> method. This override does exist in class
+            <classname
+            xlink:href="https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/String.html">String</classname>.
+            Within the given context we may thus simply use <methodname
+            xlink:href="https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/String.html#equals(java.lang.Object)">String.equals(Object
+            o)</methodname>:</para>
+
+            <programlisting language="none">try (final Scanner scan = new Scanner(System.in)) {
+  do {
+    System.out.print("Please enter password: ");
+    final String password = scan.nextLine();
+    <emphasis role="red">if (password.equals("secret"))</emphasis> {
+      break;  // Leave enclosing do ... while loop
+    } else {
+      System.out.println("Sorry, please try again");
+    }
+  } while (true);
+  System.out.println("You made it!");
+  // ...
+}</programlisting>
+          </answer>
+        </qandaentry>
+      </qandadiv>
+    </qandaset>
+
+    <qandaset defaultlabel="qanda" xml:id="sd1_qanda_enomNoEqualsRequired">
+      <title>Why does == work for <code language="java">enum</code>
+      instances?</title>
+
+      <qandadiv>
+        <qandaentry>
+          <question>
+            <para>The preceding exercise <xref
+            linkend="sd1_qanda_stringEqualProblem"/> told us to override
+            <methodname
+            xlink:href="https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/Object.html#equals(java.lang.Object)">Object.equals(Object
+            o)</methodname> when comparing for the equality of different
+            objects.</para>
+
+            <para>With respect to <code language="java">enum</code> instances
+            there seems to be an exception to the rule
+            <abbrev>e.g.</abbrev>:</para>
+
+            <programlisting language="java">Day day = Day.Saturday;
+
+// Sufficient using operator == ?
+if (day == Day.FRIDAY) ... 
+
+// Or do we require equals?
+if (day.equals(Day.FRIDAY)) ...</programlisting>
+
+            <para>Do we thus require an <methodname>equals(Object
+            o)</methodname> method for comparisons? Give a precise
+            explanation.</para>
+          </question>
+
+          <answer>
+            <para>In case of <code language="java">enum</code> instances using
+            the <quote>==</quote> operator is sufficient. Due to the way <code
+            language="java">enum</code>'s are being constructed instance
+            creation is being limited to the underlying <code
+            language="java">enum</code>'s scope by its implicitly private
+            constructor. It is thus impossible to create new instances outside
+            the respective <code language="java">enum</code>'s scope.
+            Therefore object identity and equality of values are being
+            guaranteed to match exactly for two arbitrary instances belonging
+            to the same <code language="java">enum</code> class:</para>
+
+            <programlisting language="none">MyEnum e1 = ..., e2 = ...;          // MyEnum representing some enum type
+
+final boolean
+  <emphasis role="red">objectIdentity</emphasis> = (e1 == e2),      // Both boolean values will
+  <emphasis role="red">objectEquality</emphasis> = e1.equals(e2);   // always be equal.</programlisting>
+
+            <para>In case of <code language="java">null</code> values using
+            the <quote>==</quote> operator avoids
+            <classname>java.lang.NullPointerException</classname>
+            problems:</para>
+
+            <programlisting language="java">Day day = null;
+
+if (day == Day.FRIDAY) ...      // Just different, no problems
+
+if (day.equals(Day.FRIDAY)) ... // Oops: NPE approaching ...</programlisting>
+          </answer>
+        </qandaentry>
+      </qandadiv>
+    </qandaset>
   </section>
 
   <section xml:id="sd1_inherit_sect_overrideToString">
diff --git a/Doc/Sd1/objectsClasses.xml b/Doc/Sd1/objectsClasses.xml
index 7bb579a38..cf1391366 100644
--- a/Doc/Sd1/objectsClasses.xml
+++ b/Doc/Sd1/objectsClasses.xml
@@ -12020,7 +12020,7 @@ class_wrapper.Day@63961c42</emphasis></screen></td>
         <abstract>
           <para>Clearing the mess.</para>
 
-          <para><code>switch</code> still does not work.</para>
+          <para><code>Sad: switch</code> still not working.</para>
         </abstract>
       </info>
 
@@ -12126,7 +12126,7 @@ class_wrapper_private.Day'</emphasis></screen></td>
         <programlisting language="java">public enum <link
             xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Intro/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/use_enum/Day.java">Day</link> {
 ...
-  public static String getItalianDayname(final Day day) {
+  public static String getItalianDayName(final Day day) {
     switch (day) {
       case MONDAY:    return "Lunedì";
       case TUESDAY:   return "Martedì";
@@ -12134,7 +12134,7 @@ class_wrapper_private.Day'</emphasis></screen></td>
       case SUNDAY:    return "Domenica";
     }
     return null; // Actually unreachable, but static
-                 // code analysis is limited
+                 // compiler code analysis is limited
   }
 }</programlisting>
       </figure>
@@ -12368,6 +12368,29 @@ System.out.println(" Opposite: " + southWest.opposite());</programlisting></td>
   <section xml:id="sd1_sect_usingGit">
     <title>Using git</title>
 
+    <figure xml:id="sd1_fig_gitUrban">
+      <title>From <uri
+      xlink:href="https://www.urbandictionary.com">https://www.urbandictionary.com</uri></title>
+
+      <para>Git:</para>
+
+      <orderedlist>
+        <listitem>
+          <para>A completely ignorant, childish person with no manners.</para>
+        </listitem>
+
+        <listitem>
+          <para>A person who feels justified in their callow behaviour.</para>
+        </listitem>
+
+        <listitem>
+          <para>A pubescent kid who thinks it's totally cool to act like a
+          moron on the internet, only because no one can actually reach
+          through the screen and punch their lights out.</para>
+        </listitem>
+      </orderedlist>
+    </figure>
+
     <figure xml:id="sd1_fig_gitBiblio">
       <title>Useful links</title>
 
-- 
GitLab