diff --git a/Doc/Common/snippets.xml b/Doc/Common/snippets.xml index e926617847a42c631dcf8a4db2c2b59c1487e567..e7b0269f014d94a63eaf3da16582c2257467103d 100644 --- a/Doc/Common/snippets.xml +++ b/Doc/Common/snippets.xml @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> -<chapter version="5.1" xml:id="snippets" xmlns="http://docbook.org/ns/docbook" +<chapter version="5.1" xml:id="snippets" xml:lang="de" + xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes" xmlns:xi="http://www.w3.org/2001/XInclude" @@ -10,27 +11,75 @@ xmlns:db="http://docbook.org/ns/docbook"> <title>Useful snippets</title> - <informaltable border="1"> - <tr> - <td valign="top"/> + <glosslist> + <glossentry> + <glossterm>Tabellenzeile mit Rahmen</glossterm> - <td valign="top"/> - </tr> - </informaltable> + <glossdef> + <informaltable border="1"> + <tr> + <td valign="top"/> - <informaltable border="0"> - <tr> - <th/> + <td valign="top"/> + </tr> + </informaltable> + </glossdef> + </glossentry> - <th/> - </tr> + <glossentry> + <glossterm>Kopf- + Tabellenzeile mit Rahmen</glossterm> - <tr> - <td valign="top"/> + <glossdef> + <informaltable border="1"> + <tr> + <th/> - <td valign="top"/> - </tr> - </informaltable> + <th/> + </tr> + + <tr> + <td valign="top"/> + + <td valign="top"/> + </tr> + </informaltable> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Tabellenzeile ohne Rahmen</glossterm> + + <glossdef> + <informaltable border="0"> + <tr> + <td valign="top"/> + + <td valign="top"/> + </tr> + </informaltable> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Kopf + Tabellenzeile ohne Rahmen</glossterm> + + <glossdef> + <informaltable border="0"> + <tr> + <th/> + + <th/> + </tr> + + <tr> + <td valign="top"/> + + <td valign="top"/> + </tr> + </informaltable> + </glossdef> + </glossentry> + </glosslist> <qandaset defaultlabel="qanda" xml:id="qanda_"> <title>X</title> diff --git a/Doc/Sd1/appendix.xml b/Doc/Sd1/appendix.xml index 0f602870ba83b696477d2d529060f4f4182ab934..8644bf0faf18da0931d6c522e58f8bd69000eac7 100644 --- a/Doc/Sd1/appendix.xml +++ b/Doc/Sd1/appendix.xml @@ -1393,7 +1393,7 @@ The red cross acts worldwide</screen> <para>We want to set up a <productname>Junit</productname> test which captures the output to compare it with the expected string - value <code>"Hello World!"</code>. Following <uri + value <code language="java">"Hello World!"</code>. Following <uri xlink:href="http://stackoverflow.com/questions/1119385/junit-test-for-system-out-println">http://stackoverflow.com/questions/1119385/junit-test-for-system-out-println</uri> we redefine the standard output stream by a private instance of <classname @@ -1454,12 +1454,12 @@ public class AppTest { <itemizedlist> <listitem> - <para>Generating all prime numbers of type <code>int</code>.</para> + <para>Generating all prime numbers of type <code language="java">int</code>.</para> </listitem> <listitem> <para>Using this set of prime numbers for decomposing arbitrary - <code>int</code> values into prime factors e.g.:</para> + <code language="java">int</code> values into prime factors e.g.:</para> <para>1050 = 2 * 3 * 5 * 5 * 7</para> </listitem> @@ -1472,7 +1472,7 @@ public class AppTest { <programlisting language="java">boolean[] nonPrimes = new boolean[100];</programlisting> - <para>This array will initially be filled with 100 <code>false</code> + <para>This array will initially be filled with 100 <code language="java">false</code> values. The idea is using the <link xlink:href="https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Euler.27s_Sieve">Euler sieve algorithm</link> to turn all non-prime value to <code>true @@ -1532,7 +1532,7 @@ value | t| f| f| f| t| f| f| t| ...</screen> <para>Decompose the <link xlink:href="https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Euler.27s_Sieve">Euler sieve algorithm</link> into smaller tasks and write appropriate tests. - Start with small <code>limit</code> values (like 20) and extend to + Start with small <code language="java">limit</code> values (like 20) and extend to <code xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html#MAX_VALUE">Integer.MAX_VALUE</code> step by step.</para> @@ -1597,7 +1597,7 @@ public class PrimeFrequency { }</programlisting> <para>Now 2 * 3 * 5 * 5 * 7 may be represented by three (not four!) - instances of <code>PrimeFrequency</code>. In order to represent the + instances of <code language="java">PrimeFrequency</code>. In order to represent the whole product implement a second container:</para> <programlisting language="java">/** @@ -1671,7 +1671,7 @@ public class PrimeFrequencySet { } }</programlisting> - <para>This allows for decomposing arbitrary <code>int</code> values into + <para>This allows for decomposing arbitrary <code language="java">int</code> values into their prime factors. We show a unit test example:</para> <programlisting language="java">/** diff --git a/Doc/Sd1/arrays.xml b/Doc/Sd1/arrays.xml index 91175e3dc0532b67a07b4e89b36af53cbc0921e5..f5e4e42ad3040502160eecc5393ee425969726db 100644 --- a/Doc/Sd1/arrays.xml +++ b/Doc/Sd1/arrays.xml @@ -657,7 +657,7 @@ x..</screen></td> </annotation> <para>This implementation already uses a so called - <code>enum</code> to be discussed later during an upcoming + <code language="java">enum</code> to be discussed later during an upcoming lecture:</para> <programlisting language="java">public enum Player { @@ -689,9 +689,9 @@ x..</screen></td> } }</programlisting> - <para>A <xref linkend="glo_Java"/> <code>enum</code> essentially + <para>A <xref linkend="glo_Java"/> <code language="java">enum</code> essentially is a specialized class. If you don't like this yet you may - safely replace the given <code>enum</code> by the following + safely replace the given <code language="java">enum</code> by the following class:</para> <programlisting language="java">public class Player { @@ -1074,16 +1074,16 @@ Enter value #1 of 4: 1 <itemizedlist> <listitem> - <para>{0,1,0,4} → <code>1+4=5</code>.</para> + <para>{0,1,0,4} → <code language="java">1+4=5</code>.</para> </listitem> <listitem> - <para>{0,0,0} → <code>0</code>. (By virtue of rule + <para>{0,0,0} → <code language="java">0</code>. (By virtue of rule 1)</para> </listitem> <listitem> - <para>{0,1,0} → <code>1</code>. (By virtue of rule + <para>{0,1,0} → <code language="java">1</code>. (By virtue of rule 2)</para> </listitem> </itemizedlist> @@ -1092,10 +1092,10 @@ Enter value #1 of 4: 1 <listitem> <para>If possible a sum shall not start with a negative value. The list {-1, -2, 5} for example will generate - <code>-1-2+5=2</code>. Instead the first negative value + <code language="java">-1-2+5=2</code>. Instead the first negative value shall be swapped with the first positive value (if existent). The current example will thus result in - <code>5-2-1=2</code>.</para> + <code language="java">5-2-1=2</code>.</para> </listitem> </orderedlist> @@ -1110,8 +1110,8 @@ Enter value #1 of 4: 1 <listitem> <para>The following Maven project skeleton contains both a yet not implemented class method - <code>de.hdm_stuttgart.mi.sda1.console_sum.App.prettifyOutput(int[] - values)</code> and a corresponding <xref + <methodname>de.hdm_stuttgart.mi.sda1.console_sum.App.prettifyOutput(int[] + values)</methodname> and a corresponding <xref linkend="glo_Junit"/> test suite being defined in <package>de.hdm_stuttgart.mi.sda1.console_sum</package>.<classname>AppTest</classname>:</para> @@ -1139,8 +1139,8 @@ public class AppTest { <para>Import this project into your <xref linkend="glo_IDE"/> and complete its implementation of - <code>de.hdm_stuttgart.mi.sda1.console_sum.App.prettifyOutput(int[] - values)</code> until all <xref linkend="glo_Junit"/> tests + <methodname>de.hdm_stuttgart.mi.sda1.console_sum.App.prettifyOutput(int[] + values)</methodname> until all <xref linkend="glo_Junit"/> tests do succeed. Removing the <interfacename xlink:href="http://junit.sourceforge.net/javadoc/org/junit/Ignore.html">@Ignore</interfacename> line will allow for testing your implementation.</para> @@ -1198,7 +1198,7 @@ public class AppTest { <classname>IntStoreTest</classname> which allows you to test your ongoing implementation of <classname>BoundedIntegerStore</classname>. This class - contains several <code>// TODO</code> comments indicating + contains several <code language="java">// TODO</code> comments indicating positions to be completed.</para> </listitem> @@ -1257,7 +1257,7 @@ public class AppTest { </listitem> <listitem> - <para>You have to modify the <code>void addValue(int + <para>You have to modify the <code language="java">void addValue(int value)</code> method's implementation: If the array's size gets exceeded (e.g. adding the fifth value while still having array size of 4) provide the following actions:</para> @@ -1275,7 +1275,7 @@ public class AppTest { </listitem> <listitem> - <para>Assign the new array to your <code>int[] + <para>Assign the new array to your <code language="java">int[] values</code> variable. You thereby implicitly discard the old array which become subject to be garbage collected.</para> @@ -1350,7 +1350,7 @@ public class AppTest { <para role="eclipse">Sd1/Array/integerStoreStat</para> </annotation> - <para>Testing for equality of two <code>double</code> variables is + <para>Testing for equality of two <code language="java">double</code> variables is generally a bad idea. Consider:</para> <programlisting language="java">double b= 1./3.; @@ -1362,9 +1362,9 @@ System.out.println(3 * b);</programlisting> <screen>0.9999999999999998</screen> - <para>Thus <code>assertEquals(1., b)</code> will fail due to + <para>Thus <code language="java">assertEquals(1., b)</code> will fail due to limited arithmetic precision. <xref linkend="glo_Junit"/> provides - a method <code + a method <code language="java" xlink:href="http://junit.org/javadoc/latest/org/junit/Assert.html#assertEquals(double,%20double,%20double)">assertEquals(double expected, double actual, double delta)</code> addressing this problem:</para> @@ -1445,7 +1445,7 @@ public void testApp() { <qandaentry> <question> <para>Consider the following method aimed to test whether a - given <code>long</code> number is prime or not:</para> + given <code language="java">long</code> number is prime or not:</para> <programlisting language="java"> /** * Test, whether a given number is prime. @@ -1546,25 +1546,25 @@ public void testApp() { <listitem> <para>Executing this test yields an error at index 49. This - is due to the chosen limit <code>i * i < candidate</code> + is due to the chosen limit <code language="java">i * i < candidate</code> in:</para> <programlisting language="java"> public static boolean isPrime(final long candidate) { for (long i = 2; i * i < candidate; i++) { ...</programlisting> - <para>This is wrong: Having <code>candidate == 49</code> the + <para>This is wrong: Having <code language="java">candidate == 49</code> the last value of i to be considered will be 6. So the required - test <code>49 % 7</code> will never be executed thus + test <code language="java">49 % 7</code> will never be executed thus returning true. We have to modify the loop's limit slightly - by using <code>i * i <= candidate</code>:</para> + by using <code language="java">i * i <= candidate</code>:</para> <programlisting language="java"> public static boolean isPrime(final long candidate) { for (long i = 2; i * i <emphasis role="bold"><=</emphasis> candidate; i++) { ...</programlisting> - <para>This way <code>49 % 7</code> will be evaluated to zero - thus returning <code>false</code> and thereby categorizing + <para>This way <code language="java">49 % 7</code> will be evaluated to zero + thus returning <code language="java">false</code> and thereby categorizing 49 as a non-prime number.</para> </listitem> @@ -1710,7 +1710,7 @@ public void testApp() { }</programlisting> <caution> - <para>Do not just return your internal array <code>int[] + <para>Do not just return your internal array <code language="java">int[] values</code>! Due to the amortized doubling implementation this will in most cases contain unused positions on top of added values.</para> @@ -1816,7 +1816,7 @@ values newArray | 1| 2| F| 7| 9| | ...</screen> </listitem> <listitem> - <para>Provide a constructor <code>public IntegerStore(final + <para>Provide a constructor <code language="java">public IntegerStore(final int[] values)</code> in a meaningful way with respect to median calculations.</para> </listitem> @@ -1835,7 +1835,7 @@ values newArray | 1| 2| F| 7| 9| | ...</screen> </listitem> <listitem> - <para>Finally complete the desired <code>double + <para>Finally complete the desired <code language="java">double getMedian()</code> method's implementation and actually test it. There must be at least one element in order to be able returning a meaningful result:</para> @@ -1885,7 +1885,7 @@ Your sample's median is: 1.0</screen> xlink:href="https://docs.oracle.com/javase/9/docs/api/java/util/Arrays.html#copyOfRange-int:A-int-int-">copyOfRange(...)</link> method in <methodname xlink:href="P/Sd1/Array/integerStoreMedianAnswer/target/site/apidocs/de/hdm_stuttgart/mi/sd1/store/IntegerStore.html#getValues--">getValues()</methodname> - returns that portion of our <code>int[] values</code> array + returns that portion of our <code language="java">int[] values</code> array actually been filled with data.</para> </listitem> @@ -2061,7 +2061,7 @@ values newArray | 1| 2| F| 7| 9| | ...</screen> <qandadiv> <qandaentry> <question> - <para>Implement a class to plot e.g. <code>sin(x)</code> or other + <para>Implement a class to plot e.g. <code language="java">sin(x)</code> or other functions in a terminal like e.g.:</para> <screen> ****** diff --git a/Doc/Sd1/collections.xml b/Doc/Sd1/collections.xml index 904e7e6cb2550fd30c58df2d974b74a5fa41324c..2680f175d7b9126b2682e10704573777b513d85b 100644 --- a/Doc/Sd1/collections.xml +++ b/Doc/Sd1/collections.xml @@ -99,7 +99,7 @@ <para>Create a <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/util/Set.html">Set</classname> of String instances including duplicates. Iterate over this set - and write each value to <code>System.out</code>:</para> + and write each value to <code language="java">System.out</code>:</para> <programlisting language="java"> public static void main(String[] args) { @@ -132,7 +132,7 @@ } }</programlisting> - <para>Notice the duplicate name <code>"Jim"</code>: Since our + <para>Notice the duplicate name <code language="java">"Jim"</code>: Since our collection does have set semantics it only contains three elements {"Eve", "Jim", "Tom"}:</para> @@ -161,7 +161,7 @@ Eve</screen> <para>Create a <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/util/List.html">List</classname> of String instances including duplicates. Iterate over this list - and write each value to <code>System.out</code>:</para> + and write each value to <code language="java">System.out</code>:</para> <programlisting language="java"> public static void main(String[] args) { @@ -321,7 +321,7 @@ c12.equals("dummy"):false of <link linkend="sd1CollectionDefCoordinate"><classname>Coordinate</classname></link> instances including duplicates. Iterate over this set and write - each value to <code>System.out</code> in a similar fashion to + each value to <code language="java">System.out</code> in a similar fashion to <xref linkend="sd1QandaSetString"/>. Compare your output to the corresponding example <xref linkend="sd1QandaSetString"/>.</para> @@ -501,7 +501,7 @@ c12.equals("dummy"):false white space characters may be achieved by means of <methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#split-java.lang.String-">split(...)</methodname> - and the regular expression <code>String regex = "[ + and the regular expression <code language="java">String regex = "[ \t\"!?.,'´`:;]+"</code>;. This <quote>+</quote> sign indicates the appearance of a succession of one ore more character element from the set <emphasis role="bold"><code> @@ -509,7 +509,7 @@ c12.equals("dummy"):false <para>Thus a text <emphasis role="bold"><code>That's it. Next try</code></emphasis> will be split into a string array - <code>{"That", "s", "it", "Next", "try"}</code>.</para> + <code language="java">{"That", "s", "it", "Next", "try"}</code>.</para> </listitem> <listitem> diff --git a/Doc/Sd1/coreClasses.xml b/Doc/Sd1/coreClasses.xml index 28e2871e14dade0ad150af9b4c8cc3de8eaa2db7..a6192f534cba05a3563d73acbf08cdee145458e6 100644 --- a/Doc/Sd1/coreClasses.xml +++ b/Doc/Sd1/coreClasses.xml @@ -192,7 +192,7 @@ s1.equals(s2): true</screen> </classname><methodname>equals(...)</methodname> and <methodname>hashCode()</methodname></title> - <para>If <methodname>a.equals(b)</methodname> ==> <code>a.hashCode() == + <para>If <methodname>a.equals(b)</methodname> ==> <code language="java">a.hashCode() == b.hashCode()</code>. <emphasis role="red">The reverse does not apply!</emphasis></para> @@ -209,7 +209,7 @@ s1.equals(s2): true</screen> <qandaentry> <question> <para>This exercise aims at a better understanding of - <code>System.out.<link + <code language="java">System.out.<link xlink:href="https://docs.oracle.com/javase/9/docs/api/java/io/PrintStream.html#format-java.lang.String-java.lang.Object...-">format()</link></code> already being used in <xref linkend="sd1QandaSquareNumberTableFormatted"/> and other @@ -249,9 +249,9 @@ s1.equals(s2): true</screen> method</link> providing an additional locale argument does indeed exist.</para> - <para>According to <code>System.out.<methodname + <para>According to <code language="java">System.out.<methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/io/PrintStream.html#format-java.lang.String-java.lang.Object...-">format(...)</methodname></code> - the first argument must be of type <code + the first argument must be of type <code language="java" xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html">String</code>. Additional arguments of <emphasis>arbitrary</emphasis> type may be added in accordance with the <quote>%...</quote> <link @@ -264,7 +264,7 @@ s1.equals(s2): true</screen> <listitem> <para>There is a one to one correspondence between each - <code>%...</code> format string and its corresponding + <code language="java">%...</code> format string and its corresponding argument: If n further arguments exist the format string must contain n <quote>%</quote> format specifiers.</para> </listitem> @@ -325,7 +325,7 @@ java.util.IllegalFormatConversionException: <emphasis role="bold">d != java.lang <question> <para>In Exercise <xref linkend="sw1QandaCircleAreaFinal"/> you calculated a given circle's area protecting variables against - accidental redefinition using the <code>final</code> + accidental redefinition using the <code language="java">final</code> modifier:</para> <programlisting language="java">public static void main(String[] args) { @@ -347,7 +347,7 @@ java.util.IllegalFormatConversionException: <emphasis role="bold">d != java.lang predefines constants in <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html">java.lang.Math</classname> class. Read its documentation to rewrite your code thereby - replacing your own variable <code>pi</code>'s definition .</para> + replacing your own variable <code language="java">pi</code>'s definition .</para> <tip> <para>You may want to read the <quote>Static Members</quote> and @@ -414,10 +414,10 @@ public final class Math { <emphasis role="bold">public static final double PI = 3.14159265358979323846;</emphasis> ...</programlisting> - <para>This accounts for using the expression <code><varname - xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html#field.summary">Math.PI</varname></code>.</para> + <para>This accounts for using the expression <varname + xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html#field.summary">Math.PI</varname>.</para> - <para>The careful reader may have expected an <code>import</code> + <para>The careful reader may have expected an <code language="java">import</code> statement in order to use the <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html">Math</classname> class:</para> @@ -593,7 +593,7 @@ public class CircleAreaCalculator { <listitem> <para>The <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html">String</classname> - class does contain a method returning the <code + class does contain a method returning the <code language="java" xlink:href="https://docs.oracle.com/javase/tutorial/java/data/characters.html">char</code> value at a given index. Read the documentation to find it.</para> @@ -698,22 +698,22 @@ public class CircleAreaCalculator { }</programlisting> <para>Unfortunately this method sometimes returns weird results: - The argument <code>"5397536978179"</code> for example returns + The argument <code language="java">"5397536978179"</code> for example returns -1594459696. This negative value is due to an arithmetic overflow: The product 5 × 3 × 9 × 7 × 5 × 3 × 6 × 9 × 7 × 8 × 1 × 7 × 9 is - actually 2,700,507,600. This exceeds <code + actually 2,700,507,600. This exceeds <code language="java" xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html#MAX_VALUE">MAX_VALUE</code> of 2,147,483,647 <link xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">being - representable by a 4 byte <code>int</code> variable</link>.</para> + representable by a 4 byte <code language="java">int</code> variable</link>.</para> - <para>Luckily a <code>long</code> variable will be able to hold + <para>Luckily a <code language="java">long</code> variable will be able to hold positive values <link xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">up to 9,223,372,036,854,775,807</link>. This is much larger than our <quote>worst case</quote> 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 = 2,541,865,828,329 value. We thus change our data type - from <code>int</code> to <code>long</code>:</para> + from <code language="java">int</code> to <code language="java">long</code>:</para> <programlisting language="java"> private static <emphasis role="bold">long</emphasis> getDigitProduct(final String digitWord) { @@ -725,7 +725,7 @@ public class CircleAreaCalculator { }</programlisting> <para>Assembling these pieces leaves us with the following - <code>main(...)</code> method:</para> + <code language="java">main(...)</code> method:</para> <programlisting language="java"> public static void main(String[] args) { @@ -771,7 +771,7 @@ public class CircleAreaCalculator { <para>The largest product of 13 successive digits stems from the substring <code>"5576689664895</code>" <coref linkend="sd1CoMaxProductSequence"/> starting with the last three - digits in the fourth definition line of <code>String input = + digits in the fourth definition line of <code language="java">String input = ...</code>.</para> <screen>The substring '5576689664895' yields the largest product value 23514624000.</screen> @@ -835,14 +835,14 @@ b1.equals(b2): true </programlisting> <calloutlist> <callout arearefs="answerCoStringOperatorEquality-1-co" xml:id="answerCoStringOperatorEquality-1"> - <para>The string literal <code>"TestA"</code> is being + <para>The string literal <code language="java">"TestA"</code> is being instantiated only once: The <xref linkend="glo_Java"/> compiler implementation allocates only one instance of class String per string literal.</para> <para>The string literal "TestA" (even if being defined within different classes) always represents the same object. Thus the - two distinct variables <code>a1</code> and <code>a2</code> are + two distinct variables <code language="java">a1</code> and <code language="java">a2</code> are being assigned an identical reference pointing to this unique instance. As per definition the operator == compares two object references for equality and thus returns true. You @@ -868,7 +868,7 @@ b1.equals(b2): true </programlisting> <screen>Hashcode a1 == 366712642, Hashcode a2 == 366712642</screen> - <para>So <code>a1</code> and <code>a2</code> indeed point to + <para>So <code language="java">a1</code> and <code language="java">a2</code> indeed point to the same object resulting in true when using the <methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#equals-java.lang.Object-">equals()</methodname> method for comparison.</para> @@ -879,7 +879,7 @@ b1.equals(b2): true </programlisting> <para>Every call to the <link xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#String-java.lang.String-">String constructor</link> will create a new string instance. Thus - <code>b1</code> and <code>b2</code> will hold two distinct + <code language="java">b1</code> and <code language="java">b2</code> will hold two distinct references pointing to different String instances albeit these two instances contain identical values. Following the above reasoning we may execute:</para> @@ -894,7 +894,7 @@ b1.equals(b2): true </programlisting> <para>Comparing these two reference values for equality the <quote>==</quote> operator thus returns - <code>false</code>.</para> + <code language="java">false</code>.</para> <para>The <methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#equals-java.lang.Object-">equals()</methodname> @@ -973,16 +973,16 @@ b1.equals(b2): true </programlisting> </question> <answer> - <para>Having <code>name = "An" + "ton"</code> in place we + <para>Having <code language="java">name = "An" + "ton"</code> in place we get:</para> <screen>Content:Anton <emphasis role="bold">Instances are equal</emphasis></screen> <para>The Java compiler performs a clever optimization: The - expression <code>"An" + "ton"</code> is being evaluated at compile + expression <code language="java">"An" + "ton"</code> is being evaluated at compile time. Its value is identical to the string literal - <code>"Anton"</code> and thus both variables + <code language="java">"Anton"</code> and thus both variables <varname>name</varname> and <varname>reference</varname> will be initialized to a common <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html">String</classname> @@ -996,14 +996,14 @@ b1.equals(b2): true </programlisting> </mediaobject> </informalfigure> - <para>When starting from <code>name = "An".concat("ton")</code> + <para>When starting from <code language="java">name = "An".concat("ton")</code> the result is different:</para> <screen>Content:Anton <emphasis role="bold">Instances are not equal</emphasis></screen> <para>This time the Java compiler is unable to dynamically resolve - the expression <code>"An".concat("ton")</code> at compile time. + the expression <code language="java">"An".concat("ton")</code> at compile time. Instead the method <methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#concat-java.lang.String-">concat(...)</methodname>will be called at runtime resulting in a second <classname @@ -1203,7 +1203,7 @@ File extension: html</screen> <para>You will have to deal with files having no extensions like e.g. <filename>/usr/bin/firefox</filename>. In this case the <code>extension</code> attribute is expected to be - <code>null</code>.</para> + <code language="java">null</code>.</para> </listitem> <listitem> @@ -1233,7 +1233,7 @@ File extension: html</screen> separated by space characters using the <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/util/StringTokenizer.html">java.util.StringTokenizer</classname> class. Read its documentation and learn how to specify - string delimiters other than default space <code>' '</code>. + string delimiters other than default space <code language="java">' '</code>. It'll allow you to dissect paths using separators '/' (Unix) or '\' (Windows) to break paths into components.</para> diff --git a/Doc/Sd1/deployment.xml b/Doc/Sd1/deployment.xml index 4bf2bf6a57a21babd639f8d5dd45fcbcf48a15b8..9e8f30224d8fd611db36bfa588ff39a90b4c2c1b 100644 --- a/Doc/Sd1/deployment.xml +++ b/Doc/Sd1/deployment.xml @@ -107,27 +107,27 @@ public class ArrayMethodTest { xlink:href="P/Sd1/Array/arraycalcExercise/target/site/apidocs/de/hdm_stuttgart/mi/sd1/store/Arraymethods.html#containsSameElements-int:A-int:A-">containsSameElements</link></glossterm> <glossdef> - <para>You may copy <code>int[] b</code> array to a + <para>You may copy <code language="java">int[] b</code> array to a <quote>shadow</quote> array and then subsequently erase all - elements of <code>int[] a</code> from this copy. The method + elements of <code language="java">int[] a</code> from this copy. The method <link xlink:href="P/Sd1/Array/arraycalcExercise/target/site/apidocs/de/hdm_stuttgart/mi/sd1/store/Arraymethods.html#findIndex-int:A-int-">findIndex</link> is quite helpful.</para> - <para>Consider for example <code>int[] bCopy = {1, 3, 4, 3, + <para>Consider for example <code language="java">int[] bCopy = {1, 3, 4, 3, 7}</code> containing 5 elements. Suppose our array - <code>a</code> contains the value 3 which exists at index - position 1 in <code>bCopy</code>. We may override the value + <code language="java">a</code> contains the value 3 which exists at index + position 1 in <code language="java">bCopy</code>. We may override the value index position 1 by the last array value 7 and thereby keeping track of reducing the number of array elements to 4 like {1, 7, 4, 3}.</para> - <para>Off course the array <code>bCopy</code> cannot shrink. + <para>Off course the array <code language="java">bCopy</code> cannot shrink. But we may introduce an integer variable to account for the effective number of array elements still to be considered. - If and only if all elements from <code>a</code> are - subsequently found within <code>bCopy</code> the two arrays - <code>a</code> and <code>b</code> are equal.</para> + If and only if all elements from <code language="java">a</code> are + subsequently found within <code language="java">bCopy</code> the two arrays + <code language="java">a</code> and <code language="java">b</code> are equal.</para> </glossdef> </glossentry> </glosslist> @@ -172,16 +172,16 @@ Your sample's median is: 6.0</screen> <listitem xml:id="sd1OlMedianCmdLineStep1"> <para>Using command line values means entering strings rather then e.g. integer values: In the current example the Java - runtime will pass an array of strings <code>{"2", "6", - "7"}</code> on behalf of the user's input <quote><code>2 6 - 7</code></quote> to your <code>main(String [] args)</code> + runtime will pass an array of strings <code language="java">{"2", "6", + "7"}</code> on behalf of the user's input <quote><code language="java">2 6 + 7</code></quote> to your <code language="java">main(String [] args)</code> method. These strings must be converted to integer values. This may be achieved by means of <methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html#parseInt-java.lang.String-">parseInt(String)</methodname>.</para> <para>Depending on inconsistent user input like - <quote><code>three</code></quote> instead of - <quote><code>3</code></quote> you may decide to terminate your + <quote><code language="java">three</code></quote> instead of + <quote><code language="java">3</code></quote> you may decide to terminate your application thereby providing a meaningful error message:</para> @@ -263,7 +263,7 @@ public class InputValidator { }</programlisting> <para>You may then create an instance by supplying your - <code>main(String[] args)</code> command line values:</para> + <code language="java">main(String[] args)</code> command line values:</para> <programlisting language="java"> public static void main(String[] args) { diff --git a/Doc/Sd1/gettingStarted.xml b/Doc/Sd1/gettingStarted.xml index bd5d855e9ba7765fca36a542886f60e8612b23b0..d6a707bc210c9717b597aceba0d65e6a466b152a 100644 --- a/Doc/Sd1/gettingStarted.xml +++ b/Doc/Sd1/gettingStarted.xml @@ -625,7 +625,7 @@ <listitem> <para>The <methodname>System.out.println(...)</methodname> method adds a final line break to your output. The newline - character <quote><code>\n</code></quote> allows for just a + character <quote><code language="java">\n</code></quote> allows for just a single <methodname>System.out.println(...)</methodname> statement:</para> @@ -718,8 +718,8 @@ System.out.println(...);</programlisting> } }</programlisting> - <para>Test different values for <code>a</code> and - <code>b</code>.</para> + <para>Test different values for <code language="java">a</code> and + <code language="java">b</code>.</para> </question> </qandaentry> </qandadiv> @@ -770,8 +770,8 @@ loop # 12</screen> <orderedlist> <listitem> <para>The number of output lines is being determined by the - loop's termination condition <code>i < 5</code>. - Replacing this limit by <code>while ( i < 10)</code> + loop's termination condition <code language="java">i < 5</code>. + Replacing this limit by <code language="java">while ( i < 10)</code> achieves the desired result:</para> </listitem> diff --git a/Doc/Sd1/inheritance.xml b/Doc/Sd1/inheritance.xml index 1fe7995d2828dc17ddb169a8772d217615b2553f..e552abe89bd9c48621fc5cbe141790a3d2c9d91e 100644 --- a/Doc/Sd1/inheritance.xml +++ b/Doc/Sd1/inheritance.xml @@ -37,7 +37,7 @@ public class AlphaSub }</programlisting> <para>If we try to mimic the related classes access to - <code>attribProtected</code> becomes prohibited as well:</para> + <code language="java">attribProtected</code> becomes prohibited as well:</para> <programlisting language="java">package package_two; @@ -74,8 +74,8 @@ public class AlphaSub <para>Our <classname>Rectangle</classname> and <classname>Circle</classname> instances are being described by - <code>with</code> and <code>height</code> or <code>radius</code> - respectively. Implementing a drawing application requires + <code language="java">width</code>, <code language="java">height</code> and <code>radius</code>. + Implementing a drawing application requires <emphasis>arbitrary</emphasis> objects to be moved from one position to another e.g.:</para> @@ -258,7 +258,7 @@ Rectangle (3.0,1.0), width=1.5, height=4.4</screen> <tip> <para>You may access <methodname>Figure.toString()</methodname> from derived - classes by using <code>super()</code>.</para> + classes by using <code language="java">super()</code>.</para> </tip> </question> diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml index d1f02fabbd564e1df6da8ef06d60394b7e76406a..0a5981f872e9fc0b15a2dde55af041c423c49aa2 100644 --- a/Doc/Sd1/languageFundamentals.xml +++ b/Doc/Sd1/languageFundamentals.xml @@ -159,7 +159,7 @@ </mediaobject> </figure> - <para>Signed <code + <para>Signed <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-A">byte</code> values are being represented accordingly:</para> @@ -315,7 +315,7 @@ X X X X X</programlisting> </mediaobject> </figure> - <para>Declaring a variable requires a type name like <code + <para>Declaring a variable requires a type name like <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">double</code> and an identifier:</para> @@ -325,7 +325,7 @@ X X X X X</programlisting> <programlisting language="java">double pi;</programlisting> </figure> - <para>We may assign values to variables or build expressions like <code>pi + <para>We may assign values to variables or build expressions like <code language="java">pi * 2.0 * 2.0</code> :</para> <figure xml:id="sd1_fig_varDeclareAndUse"> @@ -394,13 +394,13 @@ int c;</programlisting> <itemizedlist> <listitem> - <para>Start with a small letter like <code>africa</code> rather than - <code>Africa</code>.</para> + <para>Start with a small letter like <code language="java">africa</code> rather than + <code language="java">Africa</code>.</para> </listitem> <listitem> <para>Use <quote>camel case</quote> e.g. - <code>myFirstCode</code>.</para> + <code language="java">myFirstCode</code>.</para> <mediaobject> <imageobject> @@ -410,7 +410,7 @@ int c;</programlisting> </listitem> <listitem> - <para>Do not start with <code>_</code> or <code>$</code>.</para> + <para>Do not start with <code language="java">_</code> or <code language="java">$</code>.</para> </listitem> </itemizedlist> </figure> @@ -444,14 +444,14 @@ pi = 1.0; // Compile time error: Constant cannot be modified</programlisting> <tr> <td><itemizedlist> <listitem> - <para>Start with a letter, <quote><code>_</code></quote> or - <quote><code>$</code></quote></para> + <para>Start with a letter, <quote><code language="java">_</code></quote> or + <quote><code language="java">$</code></quote></para> </listitem> <listitem> <para><emphasis role="bold">May</emphasis> be followed by - letters, digits, <quote><code>_</code></quote> or - <quote><code>$</code></quote></para> + letters, digits, <quote><code language="java">_</code></quote> or + <quote><code language="java">$</code></quote></para> </listitem> <listitem> @@ -464,8 +464,8 @@ pi = 1.0; // Compile time error: Constant cannot be modified</programlisting> </listitem> <listitem> - <para><quote><code>boolean</code></quote> or - <quote><code>null</code></quote> literal</para> + <para><quote><code language="java">boolean</code></quote> or + <quote><code language="java">null</code></quote> literal</para> </listitem> </itemizedlist> </listitem> @@ -473,34 +473,34 @@ pi = 1.0; // Compile time error: Constant cannot be modified</programlisting> <td valign="top"><itemizedlist> <listitem> - <para><code>$test</code></para> + <para><code language="java">$test</code></para> </listitem> <listitem> - <para><code>_</code></para> + <para><code language="java">_</code></para> </listitem> <listitem> - <para><code>count</code></para> + <para><code language="java">count</code></para> </listitem> <listitem> - <para><code>blue</code></para> + <para><code language="java">blue</code></para> </listitem> </itemizedlist></td> <td valign="top"><itemizedlist> <listitem> - <para><code><emphasis role="red">2sad</emphasis></code></para> + <para><code language="java"><emphasis role="red">2sad</emphasis></code></para> </listitem> <listitem> - <para><code><emphasis + <para><code language="java"><emphasis role="red">switch</emphasis></code></para> </listitem> <listitem> - <para><code><emphasis role="red">true</emphasis></code></para> + <para><code language="java"><emphasis role="red">true</emphasis></code></para> </listitem> </itemizedlist></td> </tr> @@ -564,7 +564,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>for</code></td> + <td><code language="java">for</code></td> <td/> @@ -572,7 +572,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>sum_of_data</code></td> + <td><code language="java">sum_of_data</code></td> <td/> @@ -580,7 +580,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>sumOfData</code></td> + <td><code language="java">sumOfData</code></td> <td/> @@ -588,7 +588,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>first-name</code></td> + <td><code language="java">first-name</code></td> <td/> @@ -596,7 +596,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>ABC</code></td> + <td><code language="java">ABC</code></td> <td/> @@ -604,7 +604,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>42isThesolution</code></td> + <td><code language="java">42isThesolution</code></td> <td/> @@ -612,7 +612,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>println</code></td> + <td><code language="java">println</code></td> <td/> @@ -620,7 +620,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>B4</code></td> + <td><code language="java">B4</code></td> <td/> @@ -628,7 +628,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>AnnualSalary</code></td> + <td><code language="java">AnnualSalary</code></td> <td/> @@ -636,7 +636,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>"hello"</code></td> + <td><code language="java">"hello"</code></td> <td/> @@ -644,7 +644,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>_average </code></td> + <td><code language="java">_average </code></td> <td/> @@ -652,7 +652,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>ανδÏος</code></td> + <td><code language="java">ανδÏος</code></td> <td/> @@ -660,7 +660,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>$sum </code></td> + <td><code language="java">$sum </code></td> <td/> @@ -704,7 +704,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>for</code></td> + <td><code language="java">for</code></td> <td>no</td> @@ -712,7 +712,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>sum_of_data</code></td> + <td><code language="java">sum_of_data</code></td> <td>yes</td> @@ -720,7 +720,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>sumOfData</code></td> + <td><code language="java">sumOfData</code></td> <td>yes</td> @@ -728,7 +728,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>first-name</code></td> + <td><code language="java">first-name</code></td> <td>no</td> @@ -737,7 +737,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>ABC</code></td> + <td><code language="java">ABC</code></td> <td>yes</td> @@ -746,7 +746,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>42isThesolution</code></td> + <td><code language="java">42isThesolution</code></td> <td>no</td> @@ -754,17 +754,17 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>println</code></td> + <td><code language="java">println</code></td> <td>yes</td> - <td><code>println</code> is a method of - <code>System.out.println(...)</code> but is no <xref + <td><code language="java">println</code> is a method of + <code language="java">System.out.println(...)</code> but is no <xref linkend="glo_Java"/> keyword.</td> </tr> <tr> - <td><code>B4</code></td> + <td><code language="java">B4</code></td> <td>yes</td> @@ -773,7 +773,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>AnnualSalary</code></td> + <td><code language="java">AnnualSalary</code></td> <td>yes</td> @@ -783,7 +783,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>"hello"</code></td> + <td><code language="java">"hello"</code></td> <td>no</td> @@ -791,7 +791,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>_average </code></td> + <td><code language="java">_average </code></td> <td>yes</td> @@ -799,7 +799,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>ανδÏος</code></td> + <td><code language="java">ανδÏος</code></td> <td>yes</td> @@ -808,7 +808,7 @@ System.out.println(count + ":" + Count);</programlisting> </tr> <tr> - <td><code>$sum </code></td> + <td><code language="java">$sum </code></td> <td>yes</td> @@ -983,9 +983,9 @@ double d = i;</programlisting> <para>This snippet shows perfectly correct <xref linkend="glo_Java"/> code. However <xref linkend="sd1_fig_typeSafety"/> suggests we should not be allowed - to assign an <code + to assign 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> - to a <code + to a <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">double</code> value due to type safety.</para> @@ -1000,18 +1000,18 @@ int i = d; // Error: Incompatible types</programlisting> <answer> <para>The first snippet compiles by using the <emphasis - role="bold">widening conversion</emphasis>: When assigning <code>d + role="bold">widening conversion</emphasis>: When assigning <code language="java">d = i</code> the <xref linkend="glo_Java"/> compiler implicitly - converts the <code + converts the <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> - into a <code + into a <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">double</code> value.</para> <para>Turning a double into an int is more cumbersome: The - expression <code>i = 3.5</code> could be evaluated by agreeing on + expression <code language="java">i = 3.5</code> could be evaluated by agreeing on a specific rounding prescription. But what about i = - 3457357385783573478955345.45? A <xref linkend="glo_Java"/> <code + 3457357385783573478955345.45? A <xref linkend="glo_Java"/> <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>'s maximum value is <inlineequation> <m:math display="inline"> @@ -1027,7 +1027,7 @@ int i = d; // Error: Incompatible types</programlisting> <m:mi>1</m:mi> </m:mrow> </m:math> - </inlineequation>exceeding the former <code + </inlineequation>exceeding the former <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">double</code> value.</para> @@ -1331,7 +1331,7 @@ int i = (int) d; // Explicit cast double to int</programlisting> <para>In 3-bit two complement representation the leftmost bit will be discarded. Thus the actual result - is <code>101</code> as being expected.</para> + is <code language="java">101</code> as being expected.</para> </glossdef> </glossentry> @@ -1393,18 +1393,18 @@ int i = (int) d; // Explicit cast double to int</programlisting> </qandaset> <qandaset defaultlabel="qanda" xml:id="sw1QandaMaxMinInt"> - <title>An <code + <title>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>'s minimum and maximum value</title> <qandadiv> <qandaentry> <question> - <para>In this exercise we look at an <code + <para>In this exercise we look at 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>'s the largest and smallest (negative) possible value.</para> - <para>A <xref linkend="glo_Java"/> <code + <para>A <xref linkend="glo_Java"/> <code language="java" xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">int</code> is internally being represented by 4 <link linkend="glo_byte">bytes</link>. <inlineequation> @@ -1426,7 +1426,7 @@ int i = (int) d; // Explicit cast double to int</programlisting> <table border="1" xml:id="sw1Table4ByteIntegerRepresentation"> <caption>4 Byte <link xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's - complement</link> representation of <code + complement</link> representation of <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> values.</caption> @@ -1437,13 +1437,13 @@ int i = (int) d; // Explicit cast double to int</programlisting> </tr> <tr> - <td><code>00000000_00000000_00000000_00000000</code></td> + <td><code language="java">00000000_00000000_00000000_00000000</code></td> <td>0</td> </tr> <tr> - <td><code>01111111_11111111_11111111_11111111</code></td> + <td><code language="java">01111111_11111111_11111111_11111111</code></td> <td><inlineequation> <m:math display="inline"> @@ -1469,7 +1469,7 @@ int i = (int) d; // Explicit cast double to int</programlisting> </tr> <tr> - <td><code>10000000_00000000_00000000_00000000</code></td> + <td><code language="java">10000000_00000000_00000000_00000000</code></td> <td><inlineequation> <m:math display="inline"> @@ -1493,21 +1493,21 @@ int i = (int) d; // Explicit cast double to int</programlisting> </tr> <tr> - <td><code>11111111_11111111_11111111_11111111</code></td> + <td><code language="java">11111111_11111111_11111111_11111111</code></td> <td>-1</td> </tr> </table> <para>Use <link - xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1"><code + xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1"><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> literals</link> in <link xlink:href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-BinaryNumeral">binary - representation</link> like e.g. <code>0B1100</code> in the + representation</link> like e.g. <code language="java">0B1100</code> in the <quote>Language Fundamentals</quote> / <quote>Literals</quote> section of <xref linkend="bib_Kurniawan2015"/> in order to write - an <code + 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>'s minimum and maximum possible value to standard output.</para> @@ -1538,13 +1538,13 @@ int i = (int) d; // Explicit cast double to int</programlisting> }</programlisting> <para>BTW: The <xref linkend="glo_JDK"/> does provide maximum - value, minimum value and related information for <code + value, minimum value and related information for <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code>, - <code + <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-A">byte</code>, - <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> - and <code + and <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> primitive data types within their corresponding <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Character.html">Character</classname>, @@ -1671,9 +1671,9 @@ LocalDate birtday = LocalDate.of(1990, Month.JULY, 5);</programlisting> <title>Literals</title> <figure xml:id="sd1_fig_literal2memory"> - <title><code + <title><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">float</code> - and <code + and <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">double</code></title> <mediaobject> @@ -1744,10 +1744,10 @@ int a = 20, }</programlisting> <para>The above code does not compile due to a compiler error when - defining variable <code>c</code>.</para> + defining variable <code language="java">c</code>.</para> - <para>Explain the underlying cause. In particular: Why is <code>b - = 03</code> just fine in contrast to <code>c = 09</code> ?</para> + <para>Explain the underlying cause. In particular: Why is <code language="java">b + = 03</code> just fine in contrast to <code language="java">c = 09</code> ?</para> <tip> <para>Re-read the section on integer literal @@ -1775,7 +1775,7 @@ int a = 20, <programlisting language="java">int a = 041; System.out.println("Value = " + a);</programlisting> - <para>On execution we receive the output <code>Value = 33</code>. + <para>On execution we receive the output <code language="java">Value = 33</code>. Explain this result</para> </question> @@ -1820,7 +1820,7 @@ System.out.println("Value = " + a);</programlisting> <tip> <para>Try to assign the corresponding int values like e.g. - <code>0x27BD</code> rather than using the symbol + <code language="java">0x27BD</code> rather than using the symbol itself.</para> </tip> </listitem> @@ -1836,19 +1836,19 @@ System.out.println("Value = " + a);</programlisting> <orderedlist> <listitem> <para>Our emoticon's decimal <xref linkend="glo_unicode"/> - value is 128,526 or hexadecimal <code>1f60E</code> . So its + value is 128,526 or hexadecimal <code language="java">1f60E</code> . So its value is larger than 65535 and thus requires more than two bytes. <xref linkend="glo_Java"/> however represents characters using just two bytes. Thus the emoticon does not - fit into a single <xref linkend="glo_Java"/> <code>char</code> + fit into a single <xref linkend="glo_Java"/> <code language="java">char</code> value:</para> <programlisting language="java">char c = 0x1f60e; // Error: Incompatible types // Required: char Found: int</programlisting> <para>On contrary the arrow's decimal value of 10173 or - hexadecimal <code>0x27BD</code> fits perfectly well into a - <xref linkend="glo_Java"/> <code>char</code> variable:</para> + hexadecimal <code language="java">0x27BD</code> fits perfectly well into a + <xref linkend="glo_Java"/> <code language="java">char</code> variable:</para> <programlisting language="java">char c = 0x27BD; System.out.println("Arrow: " + c); @@ -1907,7 +1907,7 @@ System.out.println(34.0223); // An double (floating point) literal</programlisti </figure> <figure xml:id="sd1_fig_integerLiteral"> - <title><code>int</code> literals</title> + <title><code language="java">int</code> literals</title> <programlisting language="java">System.out.println("Value 1: " + 29); System.out.println("Value 2: " + 0b11101); @@ -1921,7 +1921,7 @@ Value 4: 29</screen> </figure> <figure xml:id="sd1_fig_integerLiteralExplain"> - <title><code>int</code> literals explained</title> + <title><code language="java">int</code> literals explained</title> <informaltable border="1"> <colgroup width="11%"/> @@ -2163,80 +2163,80 @@ Value 4: 29</screen> <colgroup width="76%"/> <tr> - <td><code + <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-A">byte</code>, - <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></td> <td>-</td> </tr> <tr> - <td><code + <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code></td> - <td><code>'A'</code>, <code>'\u0041'</code></td> + <td><code language="java">'A'</code>, <code language="java">'\u0041'</code></td> </tr> <tr> - <td><code + <td><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></td> - <td><code>29</code>, <code>0b1_1101</code>, <code>0x1D</code>, - <code>035</code>, <code>-29</code>,</td> + <td><code language="java">29</code>, <code language="java">0b1_1101</code>, <code language="java">0x1D</code>, + <code language="java">035</code>, <code language="java">-29</code>,</td> </tr> <tr> - <td><code + <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code></td> - <td><code>35<emphasis role="red">L</emphasis></code>, - <code>0b10_0011<emphasis role="red">L</emphasis></code>, - <code>0x23<emphasis role="red">L</emphasis></code>, - <code>043<emphasis role="red">L</emphasis></code>, - <code>-35<emphasis role="red">L</emphasis></code>,...</td> + <td><code language="java">35<emphasis role="red">L</emphasis></code>, + <code language="java">0b10_0011<emphasis role="red">L</emphasis></code>, + <code language="java">0x23<emphasis role="red">L</emphasis></code>, + <code language="java">043<emphasis role="red">L</emphasis></code>, + <code language="java">-35<emphasis role="red">L</emphasis></code>,...</td> </tr> <tr> - <td><code + <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">float</code></td> - <td><code>55.43<emphasis role="red">F</emphasis></code>, - <code>1.7E-23<emphasis role="red">F</emphasis></code>, - <code>-17.<emphasis role="red">F</emphasis></code>, - <code>100_342.334_113<emphasis role="red">F</emphasis></code></td> + <td><code language="java">55.43<emphasis role="red">F</emphasis></code>, + <code language="java">1.7E-23<emphasis role="red">F</emphasis></code>, + <code language="java">-17.<emphasis role="red">F</emphasis></code>, + <code language="java">100_342.334_113<emphasis role="red">F</emphasis></code></td> </tr> <tr> - <td><code + <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">double</code></td> - <td><code>55.43</code>, <code>1.7 E -23</code>, - <code>-17.</code></td> + <td><code language="java">55.43</code>, <code language="java">1.7 E -23</code>, + <code language="java">-17.</code></td> </tr> <tr> - <td><code + <td><code language="java" xlink:href="https://docs.oracle.com/javase/tutorial/java/data/strings.html">String</code></td> - <td><code>"Hello"</code>, <code>"Greek Δ"</code>, <code>"Greek <link + <td><code language="java">"Hello"</code>, <code language="java">"Greek Δ"</code>, <code language="java">"Greek <link xlink:href="https://www.fileformat.info/info/unicode/char/0394/index.htm">\u0394</link>"</code></td> </tr> <tr> - <td><code + <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.5-100">boolean</code></td> - <td><code + <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.5-100">true</code>, - <code + <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.5-100">false</code></td> </tr> <tr> <td/> - <td><code + <td><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.1-200">null</code></td> </tr> </informaltable> @@ -2252,8 +2252,8 @@ Value 4: 29</screen> xlink:href="http://www.rapidtables.com/code/text/ascii-table.htm#print">printable ASCII characters</link>. Write a <xref linkend="glo_Java"/> application by starting just from from the character literals - <code>' '</code>, <code>'!'</code>, <code>'"'</code>, - <code>'#'</code>, '$', '%', '&' to show their corresponding + <code language="java">' '</code>, <code language="java">'!'</code>, <code language="java">'"'</code>, + <code language="java">'#'</code>, '$', '%', '&' to show their corresponding <xref linkend="glo_ASCII"/> decimal values. The intended output is:</para> @@ -2269,18 +2269,18 @@ $: 36 32.</para> <tip> - <para>A <code + <para>A <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code> - value being represented by two bytes may be assigned to an <code + value being represented by two bytes may be assigned to 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> variable.</para> </tip> </question> <answer> - <para>Since <code + <para>Since <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code> - values may be assigned to <code + values may be assigned to <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> variables we may code:</para> @@ -2463,7 +2463,7 @@ System.out.println(intensity);</programlisting> negative value of your choice.</para> <tip> - <para>An <code + <para>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> is being represented by four bytes in <xref linkend="glo_Java"/>.</para> @@ -2496,9 +2496,9 @@ Decimal:932</screen> xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's complement</link> representation starts with a <quote>1</quote> at its highest bit. Binary literals in <xref - linkend="glo_Java"/> represent <code + linkend="glo_Java"/> represent <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> - values. An <code + values. 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> in <xref linkend="glo_Java"/> uses 4 bytes and thus occupies 4 x 8=32 bits. Therefore choosing a negative value is a simple @@ -2551,8 +2551,7 @@ System.out.println("Maximum short value:" + maximum</programlisting> </listitem> <listitem> - <para>On contrary the second assignment <code - language="java">maximum = 0B01111111_11111111</code> gets + <para>On contrary the second assignment <code language="java">maximum = 0B01111111_11111111</code> gets smoothly accepted.</para> </listitem> </itemizedlist> @@ -2565,7 +2564,7 @@ System.out.println("Maximum short value:" + maximum</programlisting> </question> <answer> - <para>Since variables of type <code + <para>Since variables 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> have a two bytes representation their corresponding range is <inlineequation> @@ -2606,23 +2605,23 @@ System.out.println("Maximum short value:" + maximum</programlisting> will be easily accommodated.</para> <para>The second question is more difficult to explain: The <xref - linkend="glo_Java"/> standard only defines <code + linkend="glo_Java"/> standard only defines <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> - literals. It does however not define <code + literals. It does however not define <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> - (or <code + (or <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-A">byte</code>) literals.</para> <para>Thus the 0B01111111_11111111 literal will be treated as an - <code + <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> - value being compatible with a <code + value being compatible with 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> variable and is thus assignable.</para> <para>On the contrary the binary value - <code>10000000_00000000</code> evaluates to <inlineequation> + <code language="java">10000000_00000000</code> evaluates to <inlineequation> <m:math display="inline"> <m:msup> <m:mn>2</m:mn> @@ -2633,9 +2632,9 @@ System.out.println("Maximum short value:" + maximum</programlisting> </inlineequation> (equal to 32768) exceeding a two-byte (signed) short variable's upper limit.</para> - <para>Even worse: A <code + <para>Even worse: 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> - variable containing binary <code>10000000_00000000</code> due to + variable containing binary <code language="java">10000000_00000000</code> due to its two byte <link xlink:href="https://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's complement</link> representation has got a value <inlineequation> @@ -2657,7 +2656,7 @@ System.out.println("Maximum short value:" + maximum</programlisting> <programlisting language="java">short minumum = (short) 0B10000000_00000000;</programlisting> - <para>Since a <code + <para>Since 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> variable's range is <inlineequation> <m:math display="inline"> @@ -2715,7 +2714,7 @@ System.out.println("Maximum short value:" + maximum</programlisting> </m:msup> </m:mrow> </m:math> - </inlineequation> to a <code + </inlineequation> to 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> variable at all.</para> </answer> @@ -2724,7 +2723,7 @@ System.out.println("Maximum short value:" + maximum</programlisting> </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1QandaBracesInPrintln"> - <title>Why using braces inside <code>System.out.println(...)</code> + <title>Why using braces inside <code language="java">System.out.println(...)</code> ?</title> <qandadiv> @@ -2752,7 +2751,7 @@ System.out.println("Decimal:" + (512 + <listitem> <para>Read about the <quote>+</quote> operator's role e.g. - in <code>System.out.println("Some" + " + in <code language="java">System.out.println("Some" + " string").</code></para> </listitem> </itemizedlist> @@ -2813,7 +2812,7 @@ System.out.println("Decimal:" +<co linkends="sd1ListingPlusOpDuplicate-1" <callout arearefs="sd1ListingPlusOpDuplicate-1-co" xml:id="sd1ListingPlusOpDuplicate-1"> <para><quote>+</quote> operator concatenating the two strings - <code>"Decimal:"</code> and <code>"932"</code>.</para> + <code language="java">"Decimal:"</code> and <code language="java">"932"</code>.</para> </callout> <callout arearefs="sd1ListingPlusOpDuplicate-2-co sd1ListingPlusOpDuplicate-3-co sd1ListingPlusOpDuplicate-4-co sd1ListingPlusOpDuplicate-5-co" @@ -2821,8 +2820,8 @@ System.out.println("Decimal:" +<co linkends="sd1ListingPlusOpDuplicate-1" <para><quote>+</quote> operators computing the integer sum of 512, 256, 128, 32 and 4 yielding a value of 932. This value subsequently gets transformed into the String - <code>"932"</code> in order to be compatible with the - preceding <code>"Decimal:"</code> string.</para> + <code language="java">"932"</code> in order to be compatible with the + preceding <code language="java">"Decimal:"</code> string.</para> </callout> </calloutlist> </answer> @@ -2849,7 +2848,7 @@ System.out.println("Decimal:" +<co linkends="sd1ListingPlusOpDuplicate-1" <screen>3 Games having 22 players each results in 66 players altogether.</screen> - <para>Write your code in a way that changing i.e. <code>final int + <para>Write your code in a way that changing i.e. <code language="java">final int games = 4</code> will result in a corresponding change of output.</para> </question> @@ -2983,7 +2982,7 @@ System.out.println("Character: " + arabicChar);</programlisting> <tip> <para>Consider the number of bytes being allocated for - representing <code + representing <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code> values in <xref linkend="glo_Java"/>.</para> </tip> @@ -2992,11 +2991,11 @@ System.out.println("Character: " + arabicChar);</programlisting> <answer> <para>According to the <link xlink:href="https://en.wikipedia.org/wiki/Arabic_(Unicode_block)#Block">Arabic - block</link> table the code point of <code>'Úœ'</code> is U+069C + block</link> table the code point of <code language="java">'Úœ'</code> is U+069C having a decimal equivalent of 1692.</para> <para><xref linkend="glo_Java"/> uses two bytes equivalent to 16 - bits for representing positive <code + bits for representing positive <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code> values. Thus the largest representable code point value is <inlineequation> @@ -3019,7 +3018,7 @@ System.out.println("Character: " + arabicChar);</programlisting> <m:mi>65535</m:mi> </m:mrow> </m:math> - </inlineequation>. So <code>'Úœ'</code> fits well into this + </inlineequation>. So <code language="java">'Úœ'</code> fits well into this range.</para> <para>According to the <link @@ -3089,16 +3088,16 @@ _____________________________________ <para>This exceeds the four byte limit of int variables. Thus the leading <quote>1</quote> will be discarded leaving us with a result of - <code>11111111_11111111_11111111_11111110</code> equalling -2 with respect + <code language="java">11111111_11111111_11111111_11111110</code> equalling -2 with respect to four byte integer two complement representation.</para> - <para>Why not using float / double in favour of bounded <code + <para>Why not using float / double in favour of bounded <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-A">byte</code>, - <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>, - <code + <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>, - <code + <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> for arithmetics?</para> @@ -3134,9 +3133,9 @@ Expected value: 9223372036854775807</screen> <title>Conversions</title> <figure xml:id="fig_WideningByte2Short"> - <title>Widening from <code + <title>Widening from <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-A">byte</code> - to <code + to <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></title> <mediaobject> @@ -3147,9 +3146,9 @@ Expected value: 9223372036854775807</screen> </figure> <figure xml:id="fig_NarrowingInt2Char"> - <title>Narrowing from <code + <title>Narrowing from <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> - to <code + to <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code></title> <mediaobject> @@ -3160,9 +3159,9 @@ Expected value: 9223372036854775807</screen> </figure> <qandaset defaultlabel="qanda" xml:id="qandaNarrowingInt2Char"> - <title><code + <title><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> - to <code + to <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code> narrowing problems</title> @@ -3206,9 +3205,9 @@ char c = i;</programlisting> <answer> <itemizedlist> <listitem> - <para>Assigning an <code + <para>Assigning 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> - to a <code + to a <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code> variable effectively narrows from four to two bytes and is thus prohibited. A fix requires an explicit type cast:</para> @@ -3242,7 +3241,7 @@ char c = (char) i;</programlisting> <programlisting language="java">System.out.println(Integer.toBinaryString(66200)); // Yields 1_00000010_10011000 System.out.println(Integer.toBinaryString(64200)); // Yields 11111010_11001000</programlisting> - <para>Thus 64200 fits into a two byte <code + <para>Thus 64200 fits into a two byte <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-E">char</code> whereas 66200 being larger than <inlineequation> <m:math display="inline"> @@ -3267,7 +3266,7 @@ System.out.println(Integer.toBinaryString(64200)); // Yields 11111010_11001000 </qandaset> <qandaset defaultlabel="qanda" xml:id="qandaNarrowing137toByte"> - <title>Get a <code>byte</code> from 137</title> + <title>Get a <code language="java">byte</code> from 137</title> <qandadiv> <qandaentry> @@ -3283,12 +3282,12 @@ System.out.println(b);</programlisting> </question> <answer> - <para>A four byte <code + <para>A four byte <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> representation of 139 reads - <code>00000000_00000000_00000000_10001011</code>. The cast <code>b + <code language="java">00000000_00000000_00000000_10001011</code>. The cast <code language="java">b = (byte) i</code> will strip the leading three bytes leaving - <code>b</code> containing <code>10001011</code>.</para> + <code language="java">b</code> containing <code language="java">10001011</code>.</para> <para>Since byte values in <xref linkend="glo_Java"/> are being represented as signed values using two-complement notation this @@ -3311,8 +3310,8 @@ System.out.println(b);</programlisting> mimic a code portion in <xref linkend="glo_Java"/> showing the catastrophic error.</para> - <para>Start with a <code>double</code> variable using a value - being suitable to be assigned to a <code>short</code> variable + <para>Start with a <code language="java">double</code> variable using a value + being suitable to be assigned to a <code language="java">short</code> variable using a cast (narrowing).</para> <para>Then in a second step raise this value breaking your short @@ -3330,7 +3329,7 @@ System.out.println(value);</programlisting> <para>Execution yields an expected integer output of <computeroutput>2331</computeroutput>. However increasing our - <code>level</code> variable's value from 2331.12 to 42331.12 + <code language="java">level</code> variable's value from 2331.12 to 42331.12 yields an output of <computeroutput>-23205</computeroutput> due to an overflow.</para> </answer> @@ -3339,9 +3338,9 @@ System.out.println(value);</programlisting> </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1QandaLong2Float"> - <title>Reducing <code + <title>Reducing <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> - to <code + to <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> (difficult)</title> @@ -3349,11 +3348,11 @@ System.out.println(value);</programlisting> <qandaentry> <question> <para>For changing a map's scale from fine to coarse Joe - programmer intends to map positive <code + programmer intends to map positive <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> - values to <code + values to <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> - values. This requires scaling down <quote>half</quote> the <code + values. This requires scaling down <quote>half</quote> the <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> data type's range <inlineequation> <m:math display="inline"> @@ -3381,7 +3380,7 @@ System.out.println(value);</programlisting> <m:mo>]</m:mo> </m:mrow> </m:math> - </inlineequation> to the <code + </inlineequation> to the <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>'s range of <inlineequation> <m:math display="inline"> @@ -3427,12 +3426,12 @@ System.out.println(value);</programlisting> </tr> <tr> - <th><code + <th><code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code></th> - <th><code>remark</code></th> + <th>remark</th> - <th><code + <th><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></th> <th>remark</th> @@ -3553,9 +3552,8 @@ System.out.println(value);</programlisting> <m:mi>2</m:mi> </m:mrow> </m:math> - </inlineequation> or <code - xlink:href="https://docs.oracle.com/javase/9/docs/api/constant-values.html#java.lang.Long.MAX_VALUE"><varname - xlink:href="https://docs.oracle.com/javase/9/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname></code> + </inlineequation> or <varname + xlink:href="https://docs.oracle.com/javase/9/docs/api/constant-values.html#java.lang.Long.MAX_VALUE">Long.MAX_VALUE</varname> - 1</td> <td align="right">2147483647</td> @@ -3619,7 +3617,7 @@ System.out.println(value);</programlisting> </tr> </informaltable> - <para>Joe's idea is dividing <code + <para>Joe's idea is dividing <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> values by <inlineequation> <m:math display="inline"> @@ -3662,7 +3660,7 @@ System.out.println(value);</programlisting> <m:mi>32</m:mi> </m:msup> </m:math> - </inlineequation> seems to be equal to <code>2 * (<link + </inlineequation> seems to be equal to <code language="java">2 * (<link xlink:href="https://docs.oracle.com/javase/9/docs/api/constant-values.html#java.lang.Integer.MAX_VALUE">Integer.MAX_VALUE</link> + 1))</code> (why?) Joe's first attempt reads:</para> @@ -3787,7 +3785,7 @@ Process finished with exit code 1</screen> </m:math> </inlineequation> (truncating). This indeed yields the desired result for non-negative values. So why does Joe encounter a - division by zero runtime exception when executing <code>longValue + division by zero runtime exception when executing <code language="java">longValue / (2 * (Integer.MAX_VALUE + 1))</code>?</para> <para>Unfortunately Joe's implementation is seriously flawed for @@ -3795,16 +3793,16 @@ Process finished with exit code 1</screen> <orderedlist> <listitem> - <para>The constant <code - xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html">Integer</code>.<code + <para>The constant <code language="java" + xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html">Integer</code>.<code language="java" xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#MAX_VALUE">MAX_VALUE</code> already suggests we will not be able to increase its value - while staying as an <code + while staying as 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>. - The expression <code>Integer.MAX_VALUE + 1</code> will be - evaluated using <code + The expression <code language="java">Integer.MAX_VALUE + 1</code> will be + evaluated using <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> - rather than <code + rather than <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> arithmetic returning:</para> @@ -3814,10 +3812,10 @@ _____________________________________ 10000000_00000000_00000000_00000000</programlisting> <para>This is the binary representation of the unintended - result <code - xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html">Integer</code>.<code + result <code language="java" + xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html">Integer</code>.<code language="java" xlink:href="https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#MIN_VALUE">MIN_VALUE</code> - due to an arithmetic overflow. The expression <code>2 * + due to an arithmetic overflow. The expression <code language="java">2 * (Integer.MAX_VALUE + 1)</code> then gives rise to a second overflow error:</para> @@ -3829,21 +3827,21 @@ _____________________________________ </orderedlist> <para>Both errors combined surprisingly result in a value of - <code>0</code> explaining the <quote>division by zero</quote> + <code language="java">0</code> explaining the <quote>division by zero</quote> error message. There are two possible solutions:</para> <glosslist> <glossentry> - <glossterm><code>(int) (longValue / (2L * (Integer.MAX_VALUE + + <glossterm><code language="java">(int) (longValue / (2L * (Integer.MAX_VALUE + 1L)))</code></glossterm> <glossdef> - <para>Introducing <code>2L</code> or <code>1L</code> (one is + <para>Introducing <code language="java">2L</code> or <code language="java">1L</code> (one is sufficient) in favour of simply using 2 and 1 turns both addition and multiplication into operations involving at - least one <code>long</code> argument. Thus for both + least one <code language="java">long</code> argument. Thus for both operations the <xref linkend="glo_Java"/> runtime will use - <code + <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> arithmetic returning the desired <quote>reducing</quote> factor of <inlineequation> @@ -3854,29 +3852,29 @@ _____________________________________ <m:mi>32</m:mi> </m:msup> </m:math> - </inlineequation> of type <code + </inlineequation> of type <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code>.</para> </glossdef> </glossentry> <glossentry> - <glossterm><code>(int) (longValue / 2 / (Integer.MAX_VALUE + + <glossterm><code language="java">(int) (longValue / 2 / (Integer.MAX_VALUE + 1L))</code></glossterm> <glossdef> <para>Same result as before.</para> <note> - <para>This time the expression starts with <code>longValue - / 2 ...</code> Since the variable <code>longValue</code> - is of type <code + <para>This time the expression starts with <code language="java">longValue + / 2 ...</code> Since the variable <code language="java">longValue</code> + 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-D">long</code> - the expression <code>longValue / 2</code> will be + the expression <code language="java">longValue / 2</code> will be evaluated by the <xref linkend="glo_Java"/> runtime using - <code + <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> arithmetics. The result will subsequently be divided by - <code>Integer.MAX_VALUE + 1L</code> again using <code + <code language="java">Integer.MAX_VALUE + 1L</code> again using <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> arithmetic.</para> </note> @@ -3929,14 +3927,14 @@ short sum = a + 7; <co linkends="sd1_co_expressionTypeIntToShort-2" <calloutlist> <callout arearefs="sd1_co_expressionTypeIntToShort-1-co" xml:id="sd1_co_expressionTypeIntToShort-1"> - <para>Declaring a <code + <para>Declaring 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> - variable <code>a</code> assigning 4.</para> + variable <code language="java">a</code> assigning 4.</para> </callout> <callout arearefs="sd1_co_expressionTypeIntToShort-2-co" xml:id="sd1_co_expressionTypeIntToShort-2"> - <para>Declaring a short variable <code>sum</code> assigning 4 + <para>Declaring a short variable <code language="java">sum</code> assigning 4 + 7.This yields a compile time error:</para> <screen>Type mismatch: cannot convert from int to short</screen> @@ -3952,28 +3950,28 @@ short sum = a + 7; <co linkends="sd1_co_expressionTypeIntToShort-2" </question> <answer> - <para>The expression <code>a + 7</code> contains a variable - <quote>a</quote> of type <code + <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 + 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 + 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>sum</code> of type <code + 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>4 + 7</code> even better: This time the plus operator acts - on two <code + <code language="java">4 + 7</code> even better: This time 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 the <xref linkend="glo_Java"/> compiler is - <quote>clever</quote> enough casting the <code + <quote>clever</quote> enough casting the <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 11 to the <code + result 11 to the <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>sum</code> without any loss of information.</para> + variable <code language="java">sum</code> without any loss of information.</para> </answer> </qandaentry> </qandadiv> @@ -3990,20 +3988,20 @@ short sum = a + 7; <co linkends="sd1_co_expressionTypeIntToShort-2" <programlisting language="java">System.out.println(8 / 9);</programlisting> <para>The output is <computeroutput>0</computeroutput> rather than - <code>0.888888...</code> . Explain this result.</para> + <code language="java">0.888888...</code> . Explain this result.</para> </question> <answer> - <para>The divide operator acts on two <code + <para>The divide 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> literal values 8 an 9. Thus irrespective of the possibly intended floating point result the resulting type is int .</para> <para>According to <uri xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3-300-A-2">https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3-300-A-2</uri> - the floating point value <code>0.88888...</code> will be + the floating point value <code language="java">0.88888...</code> will be <quote>rounded toward zero</quote> yielding an int value of - <code>0</code> rather than <code>1</code>.</para> + <code language="java">0</code> rather than <code language="java">1</code>.</para> </answer> </qandaentry> </qandadiv> @@ -4057,8 +4055,8 @@ a = a + 1; // Error: Type mismatch: cannot convert from int to byte System.out.println("New value=" + a);</programlisting> <para>What do you learn about the two operators - <quote><code>+</code></quote> an - <quote><code>a++</code></quote>? Explain this error's + <quote><code language="java">+</code></quote> an + <quote><code language="java">a++</code></quote>? Explain this error's cause.</para> </listitem> </orderedlist> @@ -4082,21 +4080,21 @@ System.out.println("New value=" + a);</programlisting> <informaltable border="1"> <tr> - <td><code> 01111111</code></td> + <td><code language="java"> 01111111</code></td> - <td><code> 127</code></td> + <td><code language="java"> 127</code></td> </tr> <tr> - <td><code>+00000001</code></td> + <td><code language="java">+00000001</code></td> - <td><code> +1</code></td> + <td><code language="java"> +1</code></td> </tr> <tr> - <td><code>=10000000</code></td> + <td><code language="java">=10000000</code></td> - <td><code>=-128</code></td> + <td><code language="java">=-128</code></td> </tr> </informaltable> @@ -4109,7 +4107,7 @@ System.out.println("New value=" + a);</programlisting> <listitem> <para>The compile time error is due to the definition of the - <quote>+</quote> operator in Java always returning an <code + <quote>+</quote> operator in Java always returning 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> rather than a byte. Consider:</para> @@ -4117,12 +4115,12 @@ System.out.println("New value=" + a);</programlisting> System.out.println(a + b);</programlisting> <para>This yields the expected output of 130 and corresponds - to an <code + to 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> value.</para> - <para>If the expression <code>a + b</code> was of data type - <code + <para>If the expression <code language="java">a + b</code> was of data type + <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-A">byte</code> in <xref linkend="glo_Java"/> an arithmetic overflow as in the subsequent code example would occur:</para> @@ -4135,13 +4133,13 @@ System.out.println(sum);</programlisting> <para>The explicit type conversion (a so called <quote>type cast</quote> or cast for short) forces the 4-byte - <code>int</code> into a one-byte variable <code>sum</code> + <code language="java">int</code> into a one-byte variable <code language="java">sum</code> thereby loosing its original value and returning -126 instead.</para> - <para>Conclusion: <code>a = a + 1</code> and <code>a++</code> + <para>Conclusion: <code language="java">a = a + 1</code> and <code language="java">a++</code> (surprisingly) differ in behaviour when being applied to - non-<code>int</code> variables.</para> + non-<code language="java">int</code> variables.</para> </listitem> </orderedlist> </answer> @@ -4170,21 +4168,21 @@ System.out.println(2147483647 + 1L);</programlisting> </question> <answer> - <para>The value 2147483647 is actually the largest possible <code + <para>The value 2147483647 is actually the largest possible <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> value <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html">Integer</classname>.<property xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html#MAX_VALUE">MAX_VALUE</property>. - The plus operator in the first expression <code>2147483647 + - 1</code> acts on two operands of type <code + The plus operator in the first expression <code language="java">2147483647 + + 1</code> acts on two operands 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>. - Thus the resulting sum will be of type <code + Thus the resulting sum will be 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 irrespective of its value.</para> - <para>On binary level adding an <code + <para>On binary level adding 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> - value of <code>1</code> results in:</para> + value of <code language="java">1</code> results in:</para> <screen> 01111111_11111111_11111111_11111111 2147483647 + 00000000_00000000_00000000_00000001 + 1 @@ -4192,21 +4190,21 @@ System.out.println(2147483647 + 1L);</programlisting> 10000000_00000000_00000000_00000000 -2147483648</screen> <para>With respect to two-complement representation of signed - <code + <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> values this is actually an overflow error: There simply is no - positive <code + positive <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> value of 2147483648 in <xref linkend="glo_Java"/>. See <uri xlink:href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3-220">http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3-220</uri> for details.</para> <para>On contrary the plus operator in the expression - <code>2147483647 + 1L</code> acts on an <code + <code language="java">2147483647 + 1L</code> acts on 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> - to its left and a <code + to its left and a <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code> - to its right. The result will thus be of type <code + to its right. The result will thus be of type <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code>:</para> <screen> 01111111_11111111_11111111_11111111 2147483647 @@ -4214,7 +4212,7 @@ System.out.println(2147483647 + 1L);</programlisting> ------------------------------------------------------------------------- ------------ 00000000_00000000_00000000_00000000_10000000_00000000_00000000_00000000 2147483648</screen> - <para>Due to a <code + <para>Due to a <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1-100-D">long</code>'s larger allocation of eight rather than four bytes execution reveals no overflow problem thus yielding the expected @@ -4251,22 +4249,22 @@ System.out.println(2147483647 + 1L);</programlisting> </question> <answer> - <para>The expression <code>x == y</code> evaluates to - <code>false</code>. This surprising result is due to limited - precision regarding both <code + <para>The expression <code language="java">x == y</code> evaluates to + <code language="java">false</code>. This surprising result is due to limited + precision regarding both <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">float</code> - and <code + and <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">double</code> IEEE representations: A given value will be approximated as close as possible.</para> - <para>Adding <code>System.out.println(x - y)</code> yields a value + <para>Adding <code language="java">System.out.println(x - y)</code> yields a value of -1.1102230246251565E-16 denoting the representational deviation - of <code>x</code> and <code>y</code>.</para> + of <code language="java">x</code> and <code language="java">y</code>.</para> - <para>Comparing <code + <para>Comparing <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">float</code> - and <code + and <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3-100">double</code> values thus requires providing a representational distance limit below which two values will be regarded as equal:</para> @@ -4425,55 +4423,55 @@ System.out.println("Difference: " + difference);</programlisting><screen>Differe </tr> <tr> - <td><code>boolean</code></td> + <td><code language="java">boolean</code></td> - <td><code>boolean</code></td> + <td><code language="java">boolean</code></td> - <td><code>boolean</code></td> + <td><code language="java">boolean</code></td> - <td><code>|, &, &&, ||, ^</code></td> + <td><code language="java">|, &, &&, ||, ^</code></td> </tr> <tr> - <td><code>int</code></td> + <td><code language="java">int</code></td> - <td><code>int</code></td> + <td><code language="java">int</code></td> - <td><code>int</code></td> + <td><code language="java">int</code></td> - <td rowspan="5"><code>+, -, *, /, %</code></td> + <td rowspan="5"><code language="java">+, -, *, /, %</code></td> </tr> <tr> - <td><code>byte</code></td> + <td><code language="java">byte</code></td> - <td><code>short</code></td> + <td><code language="java">short</code></td> - <td><code>short</code></td> + <td><code language="java">short</code></td> </tr> <tr> - <td><code>double</code></td> + <td><code language="java">double</code></td> - <td><code>float</code></td> + <td><code language="java">float</code></td> - <td><code>double</code></td> + <td><code language="java">double</code></td> </tr> <tr> - <td><code>int</code></td> + <td><code language="java">int</code></td> - <td><code>float</code></td> + <td><code language="java">float</code></td> - <td><code>float</code></td> + <td><code language="java">float</code></td> </tr> <tr> - <td><code>char</code></td> + <td><code language="java">char</code></td> - <td><code>byte</code></td> + <td><code language="java">byte</code></td> - <td><code>char</code></td> + <td><code language="java">char</code></td> </tr> </informaltable> </figure> @@ -4515,7 +4513,7 @@ System.out.println("Difference: " + difference);</programlisting><screen>Differe </m:math> </inlineequation>. Complete the following code to calculate the result and write it to standard output using - <code>System.out.println(...)</code>:</para> + <code language="java">System.out.println(...)</code>:</para> <programlisting language="java">public static void main(String[] args) { @@ -4554,7 +4552,7 @@ System.out.println("Difference: " + difference);</programlisting><screen>Differe <glossdef> <para>Instead of immediately using the expression as an - argument to <code>System.out.println(...)</code> we may + argument to <code language="java">System.out.println(...)</code> we may assign its value to a variable prior to output creation:</para> @@ -4597,7 +4595,7 @@ System.out.println("Difference: " + difference);</programlisting><screen>Differe <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>pi</code>:</para> + redefine the value of <code language="java">pi</code>:</para> <programlisting language="java">double pi = 3.141592653589793; @@ -4631,10 +4629,10 @@ System.out.println("A circle of radius " + radius + " will cover an area of " + </question> <answer> - <para>The solution is straightforward. We add the <code + <para>The solution is straightforward. We add the <code language="java" xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4">final</code> - modifier to the definition of our variable <code>pi</code>. In - addition we use capital letters <code>PI</code> reflecting the + modifier to the definition of our variable <code language="java">pi</code>. In + addition we use capital letters <code language="java">PI</code> reflecting the naming convention for constants:</para> <programlisting language="java" linenumbering="numbered">final double PI = 3.141592653589793; @@ -4656,7 +4654,7 @@ System.out.println("A circle of radius " + radius + " will cover an area of " + 'PI'</computeroutput></para> <para>As a rule of thumb: Whenever you intend a variable not to - change after an initial assignment use <code + change after an initial assignment use <code language="java" 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> </answer> @@ -4955,7 +4953,7 @@ char c = 'A'; // ASCII 65 System.out.println(s + c);</programlisting> <para>Execution results in <computeroutput>66</computeroutput>. - What is the <code>s+c</code> expression's type?</para> + What is the <code language="java">s+c</code> expression's type?</para> <tip> <para>Try to assign the expression to another variable and @@ -4965,7 +4963,7 @@ System.out.println(s + c);</programlisting> </question> <answer> - <para>Both <code>short</code> and <code>char</code> require two + <para>Both <code language="java">short</code> and <code language="java">char</code> require two bytes in <xref linkend="glo_Java"/>. It is thus surprising that a mixed expression has got neither of both input types:</para> @@ -4975,14 +4973,14 @@ short resultShort = s + c; // Incompatible types Required: short found: int char resultChar= s + c; // Incompatible types Required: char found: int</programlisting> <para>According to the compiler's error message the expression - <code>s + c</code> is of type <code>int</code>. Why choosing a + <code language="java">s + c</code> is of type <code language="java">int</code>. Why choosing a result type being twice the size of both input types?</para> <para>The two input types have different ranges:</para> <glosslist> <glossentry> - <glossterm><code>short</code></glossterm> + <glossterm><code language="java">short</code></glossterm> <glossdef> <para>Signed integer value ranging from <inlineequation> @@ -5016,7 +5014,7 @@ char resultChar= s + c; // Incompatible types Required: char found: int</prog </glossentry> <glossentry> - <glossterm><code>char</code></glossterm> + <glossterm><code language="java">char</code></glossterm> <glossdef> <para>Unsigned integer value ranging from 0 to @@ -5039,7 +5037,7 @@ char resultChar= s + c; // Incompatible types Required: char found: int</prog </glossentry> </glosslist> - <para>A promotion to <code>int</code> guarantees correct results + <para>A promotion to <code language="java">int</code> guarantees correct results in all circumstances.</para> </answer> </qandaentry> @@ -5107,10 +5105,10 @@ System.out.println("e=" + e);</programlisting> (possibly) start wailing about your success rate.</para> <tip> - <para>Both <code>x++</code> (postfix notation) and - <code>++x</code> (infix notation) are expressions themselves - which might even be rewritten as <code>(x++)</code> and - <code>(++x)</code> for the sake of clarity. The difference is + <para>Both <code language="java">x++</code> (postfix notation) and + <code language="java">++x</code> (infix notation) are expressions themselves + which might even be rewritten as <code language="java">(x++)</code> and + <code language="java">(++x)</code> for the sake of clarity. The difference is not about operator precedence rules but simply about the values of these expressions when being assigned to other variables.</para> @@ -5120,18 +5118,18 @@ System.out.println("e=" + e);</programlisting> <answer> <para>As inferred by the hint the biggest problem is about understanding postfix and infix notation of the operators - <code>++</code> and <code>--</code>. A corresponding expression + <code language="java">++</code> and <code language="java">--</code>. A corresponding expression evaluates to:</para> <itemizedlist> <listitem> - <para><code>a = x++</code> yields a ==x: The value of x + <para><code language="java">a = x++</code> yields a ==x: The value of x <emphasis role="bold">before</emphasis> incrementing it.</para> </listitem> <listitem> - <para><code>a = ++x</code> yields a ==(x + 1): The value of x + <para><code language="java">a = ++x</code> yields a ==(x + 1): The value of x <emphasis role="bold">after</emphasis> incrementing it.</para> </listitem> </itemizedlist> @@ -5184,7 +5182,7 @@ System.out.println("a = " + a + ", b = " + b + ", c = " + c);</programlisting> <screen>a = 7, b = 7, c = 1</screen> <para>Decompose this cryptic assignment into a series of multiple - elementary ones like e.g. <code>a++</code> by possibly introducing + elementary ones like e.g. <code language="java">a++</code> by possibly introducing one or more helper variables. Thus your code shall become longer but better to understand.</para> @@ -5206,29 +5204,29 @@ System.out.println("a = " + a + ", b = " + b + ", c = " + c);</programlisting> <orderedlist> <listitem> - <para><code>a</code></para> + <para><code language="java">a</code></para> </listitem> <listitem> - <para><code>a=2</code></para> + <para><code language="java">a=2</code></para> </listitem> <listitem> - <para><code>++a</code></para> + <para><code language="java">++a</code></para> </listitem> <listitem> - <para><code>++b % c--</code></para> + <para><code language="java">++b % c--</code></para> - <para>This one deserves further attention: The <code>%</code> - operator will act on <code>b</code>'s value <emphasis + <para>This one deserves further attention: The <code language="java">%</code> + operator will act on <code language="java">b</code>'s value <emphasis role="bold">after</emphasis> incrementing (infix notation) and - <code>c</code>'s value <emphasis role="bold">before</emphasis> + <code language="java">c</code>'s value <emphasis role="bold">before</emphasis> decrementing (postfix notation).</para> </listitem> </orderedlist> - <para>We introduce a helper variable <code>sum</code> for + <para>We introduce a helper variable <code language="java">sum</code> for decomposing our code into the above described four subexpressions:</para> diff --git a/Doc/Sd1/objectsClasses.xml b/Doc/Sd1/objectsClasses.xml index a79af47761fddb384985393bdc3468988c34456d..7759c6a63c1b7a09151223bda193a29891d2aa99 100644 --- a/Doc/Sd1/objectsClasses.xml +++ b/Doc/Sd1/objectsClasses.xml @@ -191,7 +191,7 @@ dashedRectangle.hasSolidBorder = false;</programlisting> </figure> <figure xml:id="sd1_fig_null"> - <title>References and <code>null</code></title> + <title>References and <code language="java">null</code></title> <programlisting language="java">Rectangle r = new Rectangle();// Creating an object @@ -203,7 +203,7 @@ r.width = 28; // Runtime error: NullPointerException (NPE) </programlisting> </figure> <figure xml:id="sd1_fig_checkObjectPresent"> - <title>References and <code>null</code></title> + <title>References and <code language="java">null</code></title> <programlisting language="java">Rectangle r; @@ -236,8 +236,8 @@ if (null == r) { <listitem> <para>Provide access restrictions.</para> - <para><code>public</code>, <code>private</code> and - <code>protected</code> modifier</para> + <para><code language="java">public</code>, <code language="java">private</code> and + <code language="java">protected</code> modifier</para> </listitem> <listitem> @@ -288,7 +288,7 @@ if (null == r) { </figure> <figure xml:id="sd1_fig_fullyQualifiedClassName"> - <title>Qualified name vs. <code + <title>Qualified name vs. <code language="java" xlink:href="https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html">import</code></title> <glosslist> @@ -305,7 +305,7 @@ if (null == r) { </glossentry> <glossentry> - <glossterm>Using <code>import</code></glossterm> + <glossterm>Using <code language="java">import</code></glossterm> <glossdef> <para><programlisting language="java">import java.util.Scanner; <co @@ -330,7 +330,7 @@ public class Q {<lineannotation>Class def</lineannotation> <tr> <th>Fully qualified class name</th> - <th>Using <code>import</code></th> + <th>Using <code language="java">import</code></th> </tr> <tr> @@ -357,7 +357,7 @@ public class Q {<lineannotation>Class def</lineannotation> <callout arearefs="sd1_callout_importVsQualifying-4.2-co" xml:id="sd1_callout_importVsQualifying-4.2"> - <para>Unqualified class use due to <code>import</code>.</para> + <para>Unqualified class use due to <code language="java">import</code>.</para> </callout> </calloutlist></td> </tr> @@ -584,7 +584,7 @@ public class Main { <callout arearefs="sd1_callout_createStringAnswer1-2-co" xml:id="sd1_callout_createStringAnswer1-2"> - <para>Due to the <code>import</code> statement this is + <para>Due to the <code language="java">import</code> statement this is actually <package>de.hdm_stuttgart.mi.sd1.tooling</package>.<classname>String</classname>.</para> </callout> @@ -764,8 +764,8 @@ height=44 </screen></td> xml:id="sd1_callout_methodSyntax-1"> <para>Optional <link xlink:href="https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html#accesscontrol-levels">access - control</link> modifier either of <code>public</code>, - <code>protected</code> or <code>private</code>.</para> + control</link> modifier either of <code language="java">public</code>, + <code language="java">protected</code> or <code language="java">private</code>.</para> </callout> <callout arearefs="sd1_callout_methodSyntax-2-co" @@ -774,7 +774,7 @@ height=44 </screen></td> <glosslist> <glossentry> - <glossterm><code>void</code></glossterm> + <glossterm><code language="java">void</code></glossterm> <glossdef> <para>The method will not return a value on completion.</para> @@ -782,8 +782,8 @@ height=44 </screen></td> </glossentry> <glossentry> - <glossterm>A data type <abbrev>e.g.</abbrev> <code>int</code>, - <code>double</code>, ...</glossterm> + <glossterm>A data type <abbrev>e.g.</abbrev> <code language="java">int</code>, + <code language="java">double</code>, ...</glossterm> <glossdef> <para>The method will return a value of the given type to its @@ -978,10 +978,10 @@ public class Rectangle { </question> <answer> - <para>First we define two instance variables <code>width</code> and - <code>height</code> representing a + <para>First we define two instance variables <code language="java">width</code> and + <code language="java">height</code> representing a <classname>Rectangle</classname>'s corresponding two parameters - <code>width</code> and <code>height</code>:</para> + <code language="java">width</code> and <code language="java">height</code>:</para> <programlisting language="java">public class Rectangle { @@ -1028,7 +1028,7 @@ public class Rectangle { name does not conflict with the instance variable name <quote>width</quote> being defined at class level. We can simply assign this value to our corresponding instance - variable using <code>width = w;</code>.</para> + variable using <code language="java">width = w;</code>.</para> </glossdef> </glossentry> @@ -1040,7 +1040,7 @@ public class Rectangle { <para>The method's formal parameter <quote>height</quote> shadows the instance variable's name being defined at class level. We need the <quote>this</quote> keyword in - <code>this.height = height</code> to resolve the + <code language="java">this.height = height</code> to resolve the ambiguity.</para> </glossdef> </glossentry> @@ -1198,7 +1198,7 @@ public class Circle { </question> <answer> - <para>We define an instance variable <code>radius</code> inside + <para>We define an instance variable <code language="java">radius</code> inside our class <classname>Circle</classname>:</para> <programlisting language="java">public class Circle { @@ -1224,7 +1224,7 @@ public class Circle { <emphasis role="bold">this.</emphasis>radius = radius; }</programlisting> - <para>This requires the usage of the <code>this</code> keyword to + <para>This requires the usage of the <code language="java">this</code> keyword to distinguish the formal parameter in <methodname>setRadius(double radius)</methodname> from the instance variable previously being defined within our class <classname>Circle</classname>. In other @@ -1373,7 +1373,7 @@ public class Circle { linkend="glo_SVG"/> code being written to standard output. Use <methodname>System.out.println(...)</methodname> calls to create the desired <xref linkend="glo_SVG"/> output. You - may need <code>\"</code> to escape double quotes as in the + may need <code language="java">\"</code> to escape double quotes as in the subsequent example or use single attribute quotes instead:</para> @@ -1619,7 +1619,7 @@ public void writeSvg() { </tr> <tr> - <td><code>public</code></td> + <td><code language="java">public</code></td> <td>yes</td> @@ -1631,7 +1631,7 @@ public void writeSvg() { </tr> <tr> - <td><code>protected</code></td> + <td><code language="java">protected</code></td> <td><emphasis role="red">no</emphasis></td> @@ -1655,7 +1655,7 @@ public void writeSvg() { </tr> <tr> - <td><code>private</code></td> + <td><code language="java">private</code></td> <td><emphasis role="red">no</emphasis></td> @@ -1820,11 +1820,11 @@ public class Gamma { <orderedlist> <listitem> <para>Explain the underlying idea of the term - <code>(secondsSince_2000_01_01 / 60) % 60</code>.</para> + <code language="java">(secondsSince_2000_01_01 / 60) % 60</code>.</para> </listitem> <listitem> - <para>Explain the <code>(int)</code> cast's necessity. Why + <para>Explain the <code language="java">(int)</code> cast's necessity. Why is there no overflow error about to happen?</para> </listitem> </orderedlist> @@ -1847,12 +1847,12 @@ public class Gamma { </listitem> <listitem> - <para>The expression <code>(secondsSince_2000_01_01 / 60) % + <para>The expression <code language="java">(secondsSince_2000_01_01 / 60) % 60</code> is of type long due to the presence of a - <code>long</code> variable. However the <quote>%</quote> + <code language="java">long</code> variable. However the <quote>%</quote> operator guarantees the result to be in the integer range 0 ... 59 which may thus be safely narrowed to an - <code>int</code> value.</para> + <code language="java">int</code> value.</para> </listitem> </orderedlist> </answer> @@ -2119,7 +2119,7 @@ public class SetterAccess { <itemizedlist> <listitem> - <para>Return type, e.g. <code>void</code></para> + <para>Return type, e.g. <code language="java">void</code></para> </listitem> <listitem> @@ -2323,7 +2323,7 @@ void main(void) { <para>In <xref linkend="glo_Java"/> method signatures allow for uniquely addressing a method within a given class e.g.:</para> - <para>The method named <code>print</code> having an int argument + <para>The method named <code language="java">print</code> having an int argument followed by a double:</para> <programlisting language="java">print(int, double)</programlisting> @@ -2470,7 +2470,7 @@ a = 33;</programlisting></td> <glossterm>Empty argument list</glossterm> <glossdef> - <para>Default constructor e.g. <code>obj = new + <para>Default constructor e.g. <code language="java">obj = new MyClass()</code>.</para> </glossdef> </glossentry> @@ -2479,7 +2479,7 @@ a = 33;</programlisting></td> <glossterm>Non-empty argument list</glossterm> <glossdef> - <para>Non-default constructor, e.g. <code>obj = new + <para>Non-default constructor, e.g. <code language="java">obj = new MyClass("xyz")</code>.</para> </glossdef> </glossentry> @@ -2558,7 +2558,7 @@ Rectangle individual = new Rectangle(2, 7); // 2 x 7 </programlisting></td> <informaltable border="1"> <tr> - <th colspan="2">Equivalent: <code>Rectangle r = new + <th colspan="2">Equivalent: <code language="java">Rectangle r = new Rectangle();</code></th> </tr> @@ -2650,7 +2650,7 @@ Salary:30000.00</pre> </itemizedlist> <para>Run your implementation by a separate class - <code>Driver</code>:</para> + <classname>Driver</classname>:</para> <programlisting language="java">package company; @@ -2730,8 +2730,8 @@ Salary:30000.00</pre> <para>Currently both classes (hopefully!) reside in the same package <package>company</package>. We will assume the two - attributes age and salary yet have no <code>public</code>, - <code>private</code> or <code>protected</code> access modifiers + attributes age and salary yet have no <code language="java">public</code>, + <code language="java">private</code> or <code language="java">protected</code> access modifiers :</para> <programlisting language="java">package company; @@ -2782,8 +2782,8 @@ public class Driver { this problem?</para> <tip> - <para>Read the section on <code>public</code>, - <code>private</code>, <code>protected</code> and default + <para>Read the section on <code language="java">public</code>, + <code language="java">private</code>, <code language="java">protected</code> and default access level.</para> </tip> </question> @@ -2793,8 +2793,8 @@ public class Driver { <package>company</package> package to <package>model</package> changes just one line of code in <filename>Employee.java</filename> namely its - <code>package</code> declaration. It also adds an - <code>import</code> statement</para> + <code language="java">package</code> declaration. It also adds an + <code language="java">import</code> statement</para> <informaltable border="1"> <colgroup width="45%"/> @@ -2899,7 +2899,7 @@ public class Driver { <glossdef> <para>Raising access level from default to - <code>public</code> thereby violating the <link + <code language="java">public</code> thereby violating the <link xlink:href="https://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)">principle of encapsulation</link>:</para> @@ -2913,7 +2913,7 @@ public class Driver { <glossentry> <glossterm>Providing a so called setter method in our <classname>Employee</classname> class of access type - <code>public</code></glossterm> + <code language="java">public</code></glossterm> <glossdef> <informaltable border="1"> @@ -3008,10 +3008,10 @@ public class Driver { }</programlisting> <para>This compiles correctly but yields an unexpected result: - The constructor variables <code>int age</code> and <code>int + The constructor variables <code language="java">int age</code> and <code language="java">int salary</code> will be assigned to themselves as being indicated by a corresponding compiler warning. More important their values - don't make it to the <code>age</code> and <code>salary</code> + don't make it to the <code language="java">age</code> and <code language="java">salary</code> attributes being defined on class level.</para> <para>Apparently we are missing something. Explain these two @@ -3029,7 +3029,7 @@ public class Driver { <answer> <para>When choosing <methodname>public Employee(int age, double salary)</methodname> we have two sets of variables - (<code>age</code>, <code>salary</code>) in two different + (<code language="java">age</code>, <code language="java">salary</code>) in two different conflicting scopes:</para> <glosslist> @@ -3058,15 +3058,15 @@ public class Driver { <para>Within the constructor's method body the parameter list scope will take precedence over class scope. Thus the assignment - <code>age = age</code> will assign the constructor's argument - <code>age</code> to itself rather than assigning it to the - instance variable <code>age</code> being defined within class + <code language="java">age = age</code> will assign the constructor's argument + <code language="java">age</code> to itself rather than assigning it to the + instance variable <code language="java">age</code> being defined within class scope.</para> <para>We may explicitly resolve this scope conflict in our - favour by qualifying the instance variables <code>age</code> and - <code>salary</code> using their respective scope being - represented by the <code>this</code> keyword:</para> + favour by qualifying the instance variables <code language="java">age</code> and + <code language="java">salary</code> using their respective scope being + represented by the <code language="java">this</code> keyword:</para> <programlisting language="java"> public Employee(int age, double salary) { // The "this" keyword refers to class scope @@ -3166,7 +3166,7 @@ public class Driver { <calloutlist> <callout arearefs="sd1_callout_clubStaticMembercount-1-co" xml:id="sd1_callout_clubStaticMembercount-1"> - <para>The keyword <code>static</code> defines <code>memberCount</code> + <para>The keyword <code language="java">static</code> defines <code language="java">memberCount</code> as a <emphasis xlink:href="https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html">class variable</emphasis> keeping track of the club's overall member @@ -3175,7 +3175,7 @@ public class Driver { <callout arearefs="sd1_callout_clubStaticMembercount-2-co" xml:id="sd1_callout_clubStaticMembercount-2"> - <para><code>memberNumber</code> and <code>name</code> being defined as + <para><code language="java">memberNumber</code> and <code language="java">name</code> being defined as instance variables.</para> </callout> @@ -3244,7 +3244,7 @@ public class Driver { xml:id="sd1_callout_clubMembershipAccessHeadcount-1"> <para><methodname>getMemberCount()</methodname> being defined as <emphasis role="bold">class method</emphasis> by virtue of the - <code>static</code> keyword.</para> + <code language="java">static</code> keyword.</para> </callout> <callout arearefs="sd1_callout_clubMembershipAccessHeadcount-2-co" @@ -3354,7 +3354,7 @@ Club's member count:3</screen> <para>Is it possible to implement <methodname>getDetails()</methodname> from <xref linkend="sd1_fig_staticMemberConstructorImplement"/> as a - class method by adding <code>static</code>?</para> + class method by adding <code language="java">static</code>?</para> </listitem> <listitem> @@ -3368,7 +3368,7 @@ Club's member count:3</screen> <answer> <orderedlist> <listitem> - <para>Adding the <code>static</code> modifier yields:</para> + <para>Adding the <code language="java">static</code> modifier yields:</para> <programlisting language="none">public class ClubMember { ... @@ -3426,7 +3426,7 @@ model/ClubMember.java:20: error: non-static variable memberNumber System.out.println("Club's headcount:" + <emphasis role="red">john.</emphasis>getMemberCount());</programlisting> <para>This is actually a regression with respect to the - <code>static</code> class method variant since the number of + <code language="java">static</code> class method variant since the number of club members does not depend on individual member instances.</para> </listitem> @@ -3544,8 +3544,8 @@ public class <link </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1QandaRewriteLoop"> - <title>A method for printing square numbers using <code>for</code>, - <code>while</code> and <code>do</code> ... <code>while</code></title> + <title>A method for printing square numbers using <code language="java">for</code>, + <code language="java">while</code> and <code language="java">do</code> ... <code language="java">while</code></title> <qandadiv> <qandaentry> @@ -3583,7 +3583,7 @@ public class <link <orderedlist> <listitem> - <para><code>while (...) {...}</code> loop:</para> + <para><code language="java">while (...) {...}</code> loop:</para> <programlisting language="java"> ... public static void while_squareNumbers(final int limit) { @@ -3597,7 +3597,7 @@ public class <link </listitem> <listitem> - <para><code>do {...} while(...)</code> loop:</para> + <para><code language="java">do {...} while(...)</code> loop:</para> <programlisting language="java"> ... public static void while_squareNumbers(final int limit) { @@ -3612,7 +3612,7 @@ public class <link </listitem> </orderedlist> - <para>Caveat: The <code>do {...} while(...)</code> part is a + <para>Caveat: The <code language="java">do {...} while(...)</code> part is a little bit tricky. Read the method's documentation <emphasis><emphasis role="bold">precisely</emphasis></emphasis> to avoid a common pitfall.</para> @@ -3639,7 +3639,7 @@ public class <link System.out.println("Finished computing square numbers"); } ...</programlisting> - <para>Its tempting to implement a <code>do ... while</code> in a + <para>Its tempting to implement a <code language="java">do ... while</code> in a similar fashion:</para> <programlisting language="java">public class LoopExample { @@ -3674,12 +3674,12 @@ public class <link System.out.println("Finished computing square numbers"); }</programlisting> - <para>This required if-clause reminds us that a <code>do {...} + <para>This required if-clause reminds us that a <code language="java">do {...} while (...)</code> is an ill-suited choice here in comparison to a - <code>while(...){...}</code> or a <code>for(...){...}</code> + <code language="java">while(...){...}</code> or a <code language="java">for(...){...}</code> loop.</para> - <para>Actually a <code>for(...){...} loop is the best choice here + <para>Actually a <code language="java">for(...){...} loop is the best choice here since the number of iterations is known in advance, the increment is constant and it allows for initialization</code>.</para> </answer> @@ -3829,7 +3829,7 @@ public class <link declaration</glossterm> <glossdef> - <para><code>private double balance</code>, <code>public + <para><code language="java">private double balance</code>, <code language="java">public void setBalance(double balance)</code></para> </glossdef> </glossentry> @@ -3839,8 +3839,8 @@ public class <link declaration</glossterm> <glossdef> - <para><code>private static double </code>interestRate, - <code>public static void setInterestRate(double + <para><code language="java">private static double </code>interestRate, + <code language="java">public static void setInterestRate(double z)</code></para> </glossdef> </glossentry> @@ -3867,17 +3867,17 @@ public class <link interestRate = z; // in contrast interestRate has } // class scope.</programlisting> - <para>The formal variable's name <quote><code>z</code></quote> + <para>The formal variable's name <quote><code language="java">z</code></quote> may be <emphasis>consistently</emphasis> renamed to any other legal, non-conflicting value like - <quote><code>myFunnyVariableName</code></quote>:</para> + <quote><code language="java">myFunnyVariableName</code></quote>:</para> <programlisting language="java"> public static void setInterestRate(double myFunnyVariableName) { interestRate = myFunnyVariableName; }</programlisting> <para>Alternatively name shadowing conflicts may be resolved by - using the keyword <emphasis><code>this</code></emphasis> <coref + using the keyword <emphasis><code language="java">this</code></emphasis> <coref linkend="sd1ListingThis"/>:</para> <programlisting language="java">public class Konto { @@ -3972,7 +3972,7 @@ public class <link <qandadiv> <qandaentry> <question> - <para>Our current <code>Account</code> class does not handle + <para>Our current <classname>Account</classname> class does not handle negative balances accordingly. Typically banks will charge a different interest rate whenever an account is in debt i.e. having a negative balance. In this case a second so called @@ -3982,7 +3982,7 @@ public class <link <para>Extend the current project by adding a new instance variable <varname>defaultInterestRate</varname> along with getter and setter methods. Then change the implementation of - <code>applyInterest()</code> and <code>applyInterest(int + <code language="java">applyInterest()</code> and <code language="java">applyInterest(int years)</code> by using the correct interest value according to the account's balance being positive or negative.</para> @@ -3998,7 +3998,7 @@ public class <link </annotation> <para>We introduce a new variable - <code>defaultInterestRate</code> to cover negative balance + <code language="java">defaultInterestRate</code> to cover negative balance values:</para> <programlisting language="java"> private static double @@ -4065,9 +4065,9 @@ int sum = a + b;</programlisting> <answer> <para>The sum of a and b may either exceed - <code>java.lang.Integer.MAX_VALUE</code> or in turn may be - less than <code>java.lang.Integer.MIN_VALUE</code>. To avoid - this type of overflow error our variable <code>sum</code> may + <code language="java">java.lang.Integer.MAX_VALUE</code> or in turn may be + less than <code language="java">java.lang.Integer.MIN_VALUE</code>. To avoid + this type of overflow error our variable <code language="java">sum</code> may be declared of type long:</para> <programlisting language="java">int a = ..., b = ...; @@ -4075,12 +4075,12 @@ int sum = a + b;</programlisting> long sum = a + b;</programlisting> <para>Unfortunately this does not (yet) help at all: Since - both operands <code>a</code> and <code>b</code> are of type - <code>int</code> the expression <code>a + b</code> is also of + both operands <code language="java">a</code> and <code language="java">b</code> are of type + <code language="java">int</code> the expression <code language="java">a + b</code> is also of type int and will be evaluated as such. To circumvent this problem we have to cast at least one operand to type - <code>long</code> prior to computing the sum. This works since - the cast operator <code>(long)</code> does have higher + <code language="java">long</code> prior to computing the sum. This works since + the cast operator <code language="java">(long)</code> does have higher priority than the <quote>+</quote> operator</para> <programlisting language="java">int a = ..., b = ...; @@ -4101,7 +4101,7 @@ long sum = (long)a + b;</programlisting> <question> <para>Implement a class representing fractions. You may find a dummy implementation containing some (not yet working) sample - usage code being contained in a <code>main()</code> + usage code being contained in a <code language="java">main()</code> method.</para> <annotation role="make"> @@ -4168,11 +4168,11 @@ long sum = (long)a + b;</programlisting> </question> <answer> - <para>Incrementing <code>++a</code> and decrementing - <code>--c</code> happens prior to adding / subtracting their - values to the variable <code>result</code> (prefix notation). - The increment operation <code>b--</code> in contrast happens - after being being subtracted from variable <code>result</code> + <para>Incrementing <code language="java">++a</code> and decrementing + <code language="java">--c</code> happens prior to adding / subtracting their + values to the variable <code language="java">result</code> (prefix notation). + The increment operation <code language="java">b--</code> in contrast happens + after being being subtracted from variable <code language="java">result</code> (postfix notation). The following code snippet is thus equivalent:</para> @@ -4393,7 +4393,7 @@ long sum = (long)a + b;</programlisting> }</programlisting> <para>In this case our numerator variable of type - <code>long</code> will be set to a value 22! (factorial). + <code language="java">long</code> will be set to a value 22! (factorial). Consider a simple demo code snippet among with its result:</para> @@ -4677,7 +4677,7 @@ Largest long value:9223372036854775807</screen></td> <qandaentry> <question> <para>We recall <xref linkend="sd1SectFraction"/>. So far no one - demanded cancelling fractions. Yet calling <code>new + demanded cancelling fractions. Yet calling <code language="java">new Fraction(4,8)</code> will create an instance internally being represented by <inlineequation> <m:math display="inline"> @@ -4713,7 +4713,7 @@ Largest long value:9223372036854775807</screen></td> }</programlisting> <para>With respect to fractions one or both parameters - <code>a</code> and <code>b</code> <coref + <code language="java">a</code> and <code language="java">b</code> <coref linkend="sd1ListEuclidNeg"/> may be zero or negative. So we do have several special cases to handle:</para> @@ -4974,7 +4974,7 @@ Largest long value:9223372036854775807</screen></td> </m:mrow> </m:math> </inlineequation>. Our simple implementation proposal would - call <code>new Fraction(12, 14)</code> only to discover a + call <code language="java">new Fraction(12, 14)</code> only to discover a <acronym>GCD</acronym> value of 4. Having larger argument values this might cause an unnecessary overflow. Moreover the <acronym>GCD</acronym> calculation will take longer than @@ -5001,7 +5001,7 @@ Largest long value:9223372036854775807</screen></td> </m:math> </inlineequation> to enable cancelling <emphasis role="bold">prior</emphasis> to multiplying. Now the call - <code>new Fraction(4,2)</code> will construct the representation + <code language="java">new Fraction(4,2)</code> will construct the representation <inlineequation> <m:math display="inline"> <m:mfrac> @@ -5207,8 +5207,8 @@ public class Math { <orderedlist> <listitem> <para>The method's signature looks slightly weird: It does - expect an argument of type <code>int</code> but returns a - <code>long</code> value. Explain the underlying + expect an argument of type <code language="java">int</code> but returns a + <code language="java">long</code> value. Explain the underlying ratio.</para> </listitem> @@ -5237,7 +5237,7 @@ public class Math { <listitem> <para>Returning a long is sensible since even small argument values in return yield large factorials. - <code>long</code> is the best (largest) choice among the + <code language="java">long</code> is the best (largest) choice among the <xref linkend="glo_Java"/> built-in integer types. Consider the following example code:</para> @@ -5278,13 +5278,13 @@ public class Math { <m:mo>!</m:mo> </m:mrow> </m:math> - </inlineequation> we already see <code>long</code> + </inlineequation> we already see <code language="java">long</code> overflow related errors. Thus allowing for even larger - <code>long</code> arguments instead of <code>int</code> + <code language="java">long</code> arguments instead of <code language="java">int</code> does not make sense at all.</para> <para>Since <quote>21</quote> is pretty small we might - favour <code>short</code> (or even <code>char</code>) as + favour <code language="java">short</code> (or even <code language="java">char</code>) as argument type:</para> <programlisting language="java">static public long factorial(short n) { ... }</programlisting> @@ -5293,7 +5293,7 @@ public class Math { would be flagged as compile time errors since both integer literals and arithmetic expressions in <xref linkend="glo_Java"/> evaluate to the data type - <code>int</code>:</para> + <code language="java">int</code>:</para> <programlisting language="java">// Compile time error: // The method factorial(short) in the type @@ -6294,7 +6294,7 @@ Ratio Recursive / Loop: 65554</screen></td> }</programlisting> </figure> - <para>Compare your results using <code>seriesLimit=8</code> + <para>Compare your results using <code language="java">seriesLimit=8</code> terms and the corresponding values from the professional implementation <link xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html#exp-double">java.lang.Math.exp</link> @@ -8136,8 +8136,8 @@ sin(4 * PI)=4518.2187229323445, difference=4518.2187229323445</screen> <para>Hint: The standard function <methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html#floor-double-">Math.rint(double)</methodname> - could be helpful: It may turn e.g. 4.47 (<code>double</code>) - to 4 (<code>long</code>).</para> + could be helpful: It may turn e.g. 4.47 (<code language="java">double</code>) + to 4 (<code language="java">long</code>).</para> </question> <answer> @@ -8202,9 +8202,9 @@ sin(4 * PI)=4518.2187229323445, difference=4518.2187229323445</screen> <para>Holding data within own memory location.</para> <para>Eight value types in <xref linkend="glo_Java"/>: - <code>byte</code>, <code>short</code>, <code>int</code>, - <code>long</code>, <code>float</code>, <code>double</code>, - <code>char</code> and <code>boolean</code>.</para> + <code language="java">byte</code>, <code language="java">short</code>, <code language="java">int</code>, + <code language="java">long</code>, <code language="java">float</code>, <code language="java">double</code>, + <code language="java">char</code> and <code language="java">boolean</code>.</para> </glossdef> </glossentry> @@ -8561,7 +8561,7 @@ After printDuplicateValue: <emphasis role="red">6</emphasis></screen></td> <glossterm>Instance scope</glossterm> <glossdef> - <para>Object lifetime: <code>new ...()</code> until being garbage + <para>Object lifetime: <code language="java">new ...()</code> until being garbage collected.</para> </glossdef> </glossentry> @@ -8571,7 +8571,7 @@ After printDuplicateValue: <emphasis role="red">6</emphasis></screen></td> <glossdef> <para>During method call until (possibly implicit) - <code>return</code>.</para> + <code language="java">return</code>.</para> </glossdef> </glossentry> </glosslist> @@ -8649,7 +8649,7 @@ After printDuplicateValue: <emphasis role="red">6</emphasis></screen></td> <tr> <td valign="top"><emphasis role="bold">empty</emphasis></td> - <td valign="top"><para><code>true</code> if and only if the stack is + <td valign="top"><para><code language="java">true</code> if and only if the stack is empty. Read only.</para></td> </tr> </informaltable> @@ -8713,7 +8713,7 @@ After printDuplicateValue: <emphasis role="red">6</emphasis></screen></td> <para>Three call stack frames corresponding to <methodname>main()</methodname> calling <methodname>circleArea(2)</methodname> calling - <code>square(2)</code>.</para> + <methodname>square(2)</methodname>.</para> </callout> <callout arearefs="sd1_fig_callStackCircleAreaIdea-2-co" @@ -8822,7 +8822,7 @@ After printDuplicateValue: <emphasis role="red">6</emphasis></screen></td> }</programlisting> </figure> - <para>The <code>static</code> method + <para>The <code language="java">static</code> method <methodname>getFirstNegative(...)</methodname> may be executed from an arbitrary context:</para> @@ -8920,7 +8920,7 @@ public class HelperTest { <itemizedlist> <listitem> - <para>All tests fail (Obvious cause: <code>return + <para>All tests fail (Obvious cause: <code language="java">return 1234</code>).</para> </listitem> @@ -9068,8 +9068,8 @@ Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 </figure> <figure xml:id="sd1_fig_equalsDoubleCaveat"> - <title>Caution comparing <code>float</code> / - <code>double</code></title> + <title>Caution comparing <code language="java">float</code> / + <code language="java">double</code></title> <mediaobject> <imageobject> @@ -9347,8 +9347,8 @@ Elapsed time: 1169805 nanoseconds</screen> Expected :0 Actual :10</screen> - <para>We forgot to deal with negative <code>limit</code> values. - Our sum is supposed to start with 0 so negative <code>limit</code> + <para>We forgot to deal with negative <code language="java">limit</code> values. + Our sum is supposed to start with 0 so negative <code language="java">limit</code> values should yield 0 like in our loop based solution:</para> <programlisting language="java">public static long getSum(int limit) { @@ -9367,10 +9367,10 @@ Actual :10</screen> Expected :2147450880 Actual :-32768</screen> - <para>This actually is a showstopper for large <code>limit</code> - values: The algebraic value of <code>limit * (limit + 1) / - 2</code> might still fit into an <code>int</code>. But <code>limit - * (limit + 1)</code> itself not yet divided by 2 may exceed <code + <para>This actually is a showstopper for large <code language="java">limit</code> + values: The algebraic value of <code language="java">limit * (limit + 1) / + 2</code> might still fit into an <code language="java">int</code>. But <code language="java">limit + * (limit + 1)</code> itself not yet divided by 2 may exceed <code language="java" xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html#MAX_VALUE">Integer.MAX_VALUE</code>. Since the multiplication happens prior to dividing by 2 we see this overflow error happen.</para> @@ -9387,11 +9387,11 @@ Actual :-32768</screen> } }</programlisting> - <para>This is only correct if <code>limit</code> is even. + <para>This is only correct if <code language="java">limit</code> is even. Otherwise division by 2 leaves us with a remainder of 1.</para> - <para>However if <code>limit</code> is uneven the second factor - <code>limit + 1</code> will be even. This observation leads us to + <para>However if <code language="java">limit</code> is uneven the second factor + <code language="java">limit + 1</code> will be even. This observation leads us to the final solution:</para> <programlisting language="java">public static long getSum(int limit) { diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index 94f0f7a42ed8ec9579eec3341a3a7d8db924e193..e087f36b0b11ba427bc306104a89edb34f3a87ac 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -152,11 +152,11 @@ <td><itemizedlist> <listitem> - <para><code>if</code>: Conditional block execution.</para> + <para><code language="java">if</code>: Conditional block execution.</para> </listitem> <listitem> - <para><code>for</code> / <code>while</code>: Repeated block + <para><code language="java">for</code> / <code language="java">while</code>: Repeated block execution.</para> </listitem> </itemizedlist></td> @@ -168,7 +168,7 @@ <title>The if conditional statement</title> <figure xml:id="sd1_fig_if"> - <title><code>if</code> block execution selection</title> + <title><code language="java">if</code> block execution selection</title> <informaltable border="1"> <tr> @@ -192,14 +192,14 @@ Done!</screen></td> </figure> <figure xml:id="sd1_fig_ifSyntax"> - <title><code>if</code> execution syntax</title> + <title><code language="java">if</code> execution syntax</title> <programlisting language="java">if (booleanExpression) (block | statement)</programlisting> </figure> <figure xml:id="sd1_fig_ifElse"> - <title><code>if</code> ... <code>else</code> block execution + <title><code language="java">if</code> ... <code language="java">else</code> block execution selection</title> <informaltable border="1"> @@ -252,7 +252,7 @@ Done!</screen></td> </figure> <figure xml:id="sd1_fig_ifElseSyntax"> - <title><code>if ... else</code> execution syntax</title> + <title><code language="java">if ... else</code> execution syntax</title> <programlisting language="java">if (booleanExpression) (block | statement) @@ -261,7 +261,7 @@ else </figure> <figure xml:id="sd1_fig_bestPracticeCompareEquals"> - <title><code>Best practices comparing for equality</code></title> + <title>Best practices comparing for equality</title> <para>Always use</para> @@ -297,12 +297,12 @@ if (count = 4) { // is count equal to 4? <answer> <para>The compiler complains about incompatible types: An - <code>int</code> is being found but a <code>boolean</code> to be + <code language="java">int</code> is being found but a <code language="java">boolean</code> to be expected.</para> - <para>The expression <code>count = 4</code> is an assignment and + <para>The expression <code language="java">count = 4</code> is an assignment and <emphasis role="bold">not</emphasis> a comparison. Its type is - <code>int</code> rather than <code>boolean</code>:</para> + <code language="java">int</code> rather than <code language="java">boolean</code>:</para> <programlisting language="java">int count = 1; final int test = (count = 4); @@ -314,7 +314,7 @@ System.out.println("test=" + test);</programlisting> 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>boolean</code>:</para> + of type <code language="java">boolean</code>:</para> <programlisting language="java">int count = 4 + 3; final boolean test = (count == 4); // Now using "==" (comparison) in favour of "=" (assignment) @@ -337,7 +337,7 @@ if (count == 4) { // is count equal to 4? <note xml:id="sd1_note_useLiteralEqualsVariable"> <para>In contrast to <xref linkend="glo_Java"/> some programming languages like C and C++ allow using expressions like e.g. - <code>if(count = 4)</code>:</para> + <code language="java">if(count = 4)</code>:</para> <programlisting language="c">#include <stdio.h> @@ -349,8 +349,8 @@ int main(int argc, char **args) { } }</programlisting> - <para>The integer expression <code>count = 4</code> has got a value of - 4. Integer values inside an <code>if (...)</code> statement will be + <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> @@ -371,25 +371,25 @@ 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 + <para>Thus <code language="java">if(count = 4)</code> will always be true 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.</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 = + <para>For this reason it is good practice using <code language="java">if (4 == + count)</code> instead: Even in C 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> <qandaset defaultlabel="qanda" xml:id="sd1_qanda_replaceElseIf"> - <title>Replacing <code>if else (...)</code> by introducing a nested - <code>if ... else</code></title> + <title>Replacing <code language="java">if else (...)</code> by introducing a nested + <code language="java">if ... else</code></title> <qandadiv> <qandaentry> <question> - <para>A computer newbie did not yet read about the <code>else + <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> @@ -401,22 +401,22 @@ int main(int argc, char **args) { System.out.println("Wrong!"); }</programlisting> - <para><code>a</code> and <code>b</code> are supposed to be - <code>int</code> variables. Please help our newbie just using - <code>if(...) else </code> (having no <code>else if(...)</code> + <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 just using + <code language="java">if(...) else </code> (having no <code language="java">else if(...)</code> branch) statements!</para> <tip> <para>As the title suggests you may want to nested an - <quote>inner</quote> <code>if(...)</code> Inside 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>else - if(...)</code> branch by a nested <code>if(...){ ...} else</code> - statement and moving the final <code>else</code> block into the + <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) { @@ -589,7 +589,7 @@ System.out.println("Failed!");</programlisting> 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>return</code> instead for terminating the + 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"/> @@ -602,7 +602,7 @@ System.out.println("Failed!");</programlisting> </listitem> <listitem> - <para>Explain the <code>count++</code> statement's + <para>Explain the <code language="java">count++</code> statement's purpose.</para> <tip> @@ -610,7 +610,7 @@ System.out.println("Failed!");</programlisting> 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>count++</code> statement?</para> + the <code language="java">count++</code> statement?</para> </tip> </listitem> @@ -639,15 +639,15 @@ return; System.out.println("Failed!");</programlisting> - <para>The second <code>return</code> statement will leave the - context unconditionally regardless of the preceding <code>if - (count < 5)</code> and the <code>count</code> variable's + <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>System.out.println("Failed!")</code> <emphasis + <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>return</code> resolves the + <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; @@ -665,7 +665,7 @@ System.out.println("Failed!");</programlisting> </listitem> <listitem> - <para>Removing <code>count++</code> leaves us with:</para> + <para>Removing <code language="java">count++</code> leaves us with:</para> <programlisting language="java">int count = 3; @@ -686,20 +686,20 @@ System.out.println("Failed!");</programlisting> <para>This warning is also due to static compile time code analysis: The count variable's value is not being altered throughout its scope. It could actually be declared - <code>static int count = 3</code>. Thus the - <code>return</code> inside <code>if (count < 5)</code> will + <code language="java">static int count = 3</code>. 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>count</code> now having a value of 4 when being + <code language="java">count</code> now having a value of 4 when being evaluated inside if (count < 5) is still smaller than 5.</para> </listitem> <listitem> <para>The underlying idea is preventing programmers from - coding carelessly: Even the <code>if (count < 5)</code> + coding carelessly: Even the <code language="java">if (count < 5)</code> statement is most likely an unintended bug.</para> </listitem> </orderedlist> @@ -718,7 +718,7 @@ System.out.println("Failed!");</programlisting> </qandaset> <figure xml:id="sd1_fig_ifElseNested"> - <title>Nested <code>if ... else</code></title> + <title>Nested <code language="java">if ... else</code></title> <informaltable border="1"> <tr> @@ -746,7 +746,7 @@ System.out.println("Failed!");</programlisting> </figure> <figure xml:id="sd1_fig_ifElse_else"> - <title><code>if ... else if ... else</code></title> + <title><code language="java">if ... else if ... else</code></title> <informaltable border="1"> <tr> @@ -770,7 +770,7 @@ System.out.println("Failed!");</programlisting> </figure> <figure xml:id="sd1_fig_ifElse_elseSyntax"> - <title><code>if ... else if ... else</code> syntax</title> + <title><code language="java">if ... else if ... else</code> syntax</title> <programlisting language="java">if (booleanExpression) (block | statement) @@ -818,14 +818,14 @@ You entered 1234567</screen> <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>pointsReached</code>.</para> + <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>Complete the following code by assigning this modified - number of points to the variable <code>newResult</code>.</para> + number of points to the variable <code language="java">newResult</code>.</para> <programlisting language="java"> public static void main(String[] args) { @@ -843,7 +843,7 @@ You entered 1234567</screen> <answer> <para>The basic task is to add up the values of - <code>pointsReached</code> and <code>pointsToAdd</code>. When + <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) { @@ -864,8 +864,8 @@ You entered 1234567</screen> }</programlisting> <para>This basically means calculating the minimum of the two - expressions <code>pointsReached + pointsToAdd</code> and - <code>maximumPoints</code>. This may as well be be implemented + 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) { @@ -1143,10 +1143,10 @@ Decimal value 11 not yet implemented</screen> </section> <section xml:id="sd1_sect_switch"> - <title>The <code>switch</code> statement</title> + <title>The <code language="java">switch</code> statement</title> <figure xml:id="sd1_fig_switch"> - <title>Better: Using <code>switch</code></title> + <title>Better: Using <code language="java">switch</code></title> <programlisting language="java">... switch(number) { @@ -1166,7 +1166,7 @@ Saturday</screen> </figure> <figure xml:id="sd1_fig_switchSyntax"> - <title><code>switch</code> Syntax</title> + <title><code language="java">switch</code> Syntax</title> <programlisting language="java">switch(expression) { case value_1 : @@ -1194,10 +1194,10 @@ Saturday</screen> <qandadiv> <qandaentry> <question> - <para>Do we need <code>break</code> statements in <xref + <para>Do we need <code language="java">break</code> statements in <xref linkend="sd1_fig_switch"/>? Create a complete example as in <xref linkend="sd1_fig_dayNumber2NameByIfElseif"/> but use a - <code>switch</code> statement without any <code>break</code>. What + <code language="java">switch</code> statement without any <code language="java">break</code>. What do you observe on execution?</para> </question> @@ -1227,8 +1227,8 @@ Saturday Sunday Invalid number 5</screen> - <para>So the <code>break</code> statements effectively prohibit - <quote>fall-through</quote> towards subsequent <code>case</code> + <para>So the <code language="java">break</code> statements effectively prohibit + <quote>fall-through</quote> towards subsequent <code language="java">case</code> labels. Moving <quote>7 / Sunday</quote> to the beginning this becomes even clearer:</para> @@ -1245,7 +1245,7 @@ switch(number) { } ...</programlisting> <para>This time entering number 5 again our output no longer - contains <code>"Sunday"</code>:</para> + contains <code language="java">"Sunday"</code>:</para> <screen>Enter a weekday number (1=Monday, 2=Tuesday,...) : 5 Friday @@ -1253,7 +1253,7 @@ Saturday Invalid number 5</screen> <para>Thus fall-through does not depend on numerical - <code>case</code> label ordering but solely on their order of + <code language="java">case</code> label ordering but solely on their order of appearance.</para> </answer> </qandaentry> @@ -1390,7 +1390,7 @@ switch(day % 7) { <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> + based <code language="java">case</code> labels:</para> <programlisting language="java">String month, season; ... @@ -1525,7 +1525,7 @@ Unknown day name July</screen> Midweek</screen> <tip> - <para>Sometimes omitting <code>break</code> statements allowing + <para>Sometimes omitting <code language="java">break</code> statements allowing for fall-through is your friend.</para> </tip> </question> @@ -1551,13 +1551,13 @@ Midweek</screen> </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1QandaRomanNumeralsSwitch"> - <title>Roman numerals, using <code>switch</code></title> + <title>Roman numerals, using <code language="java">switch</code></title> <qandadiv> <qandaentry> <question> <para>Re-implement <xref linkend="sd1QandaRomanNumeralsIf"/> using - a switch statement rather then an <code>if ... else if ... + a switch statement rather then an <code language="java">if ... else if ... else</code>.</para> </question> @@ -1645,7 +1645,7 @@ public class LeapYear { <para>This solution contains two identical print statements. Developers don't favour redundancies. Combining the first two - <code>if</code> branches into one resolves the issue:</para> + <code language="java">if</code> branches into one resolves the issue:</para> <programlisting language="java"> public static void main(String[] args) { @@ -1729,10 +1729,10 @@ switch(repetitions) { </figure> <section xml:id="sd1_sect_while"> - <title><code>while</code></title> + <title><code language="java">while</code></title> <figure xml:id="sd1_fig_loopParamSolution"> - <title>A <code>while</code> loop</title> + <title>A <code language="java">while</code> loop</title> <informaltable border="1"> <tr> @@ -1790,7 +1790,7 @@ Do not copy!</screen></td> </calloutlist> <figure xml:id="sd1_fig_whileSyntax"> - <title><code>while</code> syntax</title> + <title><code language="java">while</code> syntax</title> <programlisting language="java">while (booleanExpression) (block | statement)</programlisting> @@ -1813,10 +1813,10 @@ Do not copy!</screen> </section> <section xml:id="sd1_sect_doWhile"> - <title><code>do ... while</code></title> + <title><code language="java">do ... while</code></title> <para><figure xml:id="sd1_fig_doWhile"> - <title>A <code>do ... while</code> loop</title> + <title>A <code language="java">do ... while</code> loop</title> <informaltable border="1"> <tr> @@ -1836,7 +1836,7 @@ Do not copy!</screen></td> </tr> </informaltable> </figure><figure xml:id="sd1_fig_doWhileSyntax"> - <title><code>do ... while</code> syntax</title> + <title><code language="java">do ... while</code> syntax</title> <programlisting language="java">do (block | statement) @@ -1883,8 +1883,8 @@ Goodbye!</screen> loop's statement will be executed at least once:</parameter><screen>Enter an integer (0 to terminate): 0 0 is an even number -Goodbye!</screen>We thus use a <code>do ... while(...)</code> rather than a - <code>while(...)</code> loop entering the loop's body +Goodbye!</screen>We thus use a <code language="java">do ... while(...)</code> rather than a + <code language="java">while(...)</code> loop entering the loop's body <emphasis role="bold">before</emphasis> any check is about to happen:<programlisting language="java">try(final Scanner scanner = new Scanner(System.in)){ @@ -1910,7 +1910,7 @@ Goodbye!</screen>We thus use a <code>do ... while(...)</code> rather than a <title>for</title> <figure xml:id="sd1_fig_forConsider"> - <title>Frequent usage of <code>while</code></title> + <title>Frequent usage of <code language="java">while</code></title> <programlisting language="java">int i = 1; <co linkends="sd1_callout_whileLoopJustCounting-1" @@ -1945,7 +1945,7 @@ while (i < 5 <co linkends="sd1_callout_whileLoopJustCounting-2" </figure> <figure xml:id="sd1_fig_for"> - <title>Replacing <code>while</code> by <code>for</code></title> + <title>Replacing <code language="java">while</code> by <code language="java">for</code></title> <informaltable border="1"> <tr> @@ -1983,14 +1983,14 @@ while (i < 5 <coref linkend="sd1_callout_for-2-co"/>) { </figure> <figure xml:id="sd1_fig_forSyntax"> - <title><code>for</code> syntax</title> + <title><code language="java">for</code> syntax</title> <programlisting language="java">for ( init ; booleanExpression ; update ) (block | statement)</programlisting> </figure> <figure xml:id="sd1_fig_forVariableScope"> - <title><code>for</code> variable scope</title> + <title><code language="java">for</code> variable scope</title> <informaltable border="1"> <tr> @@ -2016,19 +2016,19 @@ System.out.println(i); // o.K. </programlisting></td> <calloutlist> <callout arearefs="sd1_callout_forVariableScope-1-co" xml:id="sd1_callout_forVariableScope-1"> - <para>Scope of variable i bound to <code>for</code> loop</para> + <para>Scope of variable i bound to <code language="java">for</code> loop</para> </callout> <callout arearefs="sd1_callout_forVariableScope-2-co" xml:id="sd1_callout_forVariableScope-2"> - <para>Variable <code>i</code> defined in <quote>current</quote> + <para>Variable <code language="java">i</code> defined in <quote>current</quote> scope.</para> </callout> </calloutlist> </figure> <figure xml:id="sd1_fig_forVariableScopeEquivalence"> - <title><code>for</code> variable scope equivalence</title> + <title><code language="java">for</code> variable scope equivalence</title> <informaltable border="1"> <tr> @@ -2051,7 +2051,7 @@ System.out.println(i); // o.K. </programlisting></td> </figure> <figure xml:id="sd1_fig_forWhileRelation"> - <title><code>for</code> <abbrev>vs.</abbrev> while + <title><code language="java">for</code> <abbrev>vs.</abbrev> while relationship</title> <informaltable border="1"> @@ -2064,8 +2064,8 @@ System.out.println(i); // o.K. </programlisting></td> </tr> </informaltable> - <para>Observation: <code>for (...)</code> is more general than - <code>while(...)</code>.</para> + <para>Observation: <code language="java">for (...)</code> is more general than + <code language="java">while(...)</code>.</para> </figure> <qandaset defaultlabel="qanda" xml:id="sd1QandaOnlyEven"> @@ -2110,15 +2110,15 @@ while (i < limit) { </listitem> <listitem> - <para>Use a <code>for</code> loop in favour of - <code>while</code>.</para> + <para>Use a <code language="java">for</code> loop in favour of + <code language="java">while</code>.</para> </listitem> </orderedlist> </question> <answer> <para>Rather than filtering unwanted values we use a - <code>for</code> loop having a step width of 2:</para> + <code language="java">for</code> loop having a step width of 2:</para> <programlisting language="java">final int limit = 14; @@ -2165,11 +2165,11 @@ while (i < limit) { <itemizedlist> <listitem> - <para><code>i</code> to <code>row</code></para> + <para><code language="java">i</code> to <code language="java">row</code></para> </listitem> <listitem> - <para><code>j</code> to <code>column</code></para> + <para><code language="java">j</code> to <code language="java">column</code></para> </listitem> </itemizedlist> </tip> @@ -2182,7 +2182,7 @@ while (i < limit) { <qandaentry> <question> <para>Write an application to print an ASCII art Xmas tree. - Provide a configurable parameter <code>numberOfRows</code> which + Provide a configurable parameter <code language="java">numberOfRows</code> which allows for controlling the tree's size. The following example shows a tree spanning 6 rows excluding the top (X) and the two bottom (###) lines:</para> @@ -2287,8 +2287,8 @@ for (int row = 0; row < numberOfRows; row++) { <co <callout arearefs="sd1_callout_printTriangle-3-co" xml:id="sd1_callout_printTriangle-3"> <para>Print a number of '*' characters. The exact - number equals the outer loop's <code>row</code> value - plus 1. So a value of <code>row == 4</code> will print + number equals the outer loop's <code language="java">row</code> value + plus 1. So a value of <code language="java">row == 4</code> will print 4 + 1 '*' characters.</para> </callout> @@ -2306,7 +2306,7 @@ for (int row = 0; row < numberOfRows; row++) { <co <callout arearefs="sd1_callout_printTriangle-5-co" xml:id="sd1_callout_printTriangle-5"> <para>After each line we need a line break - <code>'\n'</code> for starting the next one using + <code language="java">'\n'</code> for starting the next one using <methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/io/PrintStream.html#println--">println()</methodname>. If you prefer explicit code you may use <package @@ -2333,7 +2333,7 @@ for (int row = 0; row < numberOfRows; row++) { <co <para>Hint: You will need a second loop inside the <quote>outer</quote> loop for creating spaces using the - <code>' '</code> character.</para> + <code language="java">' '</code> character.</para> </listitem> </orderedlist> </tip> @@ -2659,7 +2659,7 @@ for (int row = 0; row < numberOfRows; row++) { } }</programlisting> - <para>Both variants allow for setting e.g. <code>final int + <para>Both variants allow for setting e.g. <code language="java">final int numberOfRowGroups = 8</code> creating a larger tree (albeit having the same trunk size):</para> @@ -2922,7 +2922,7 @@ for (int row = 0; row < numberOfRows; row++) { 10| 10 20 30 40 50 60 70 80 90 100 </screen> <para>The number of rows and columns are equal. Provide an - appropriate parameter <code>final int limit = ...</code> so + appropriate parameter <code language="java">final int limit = ...</code> so that your implementation works for different values just by changing its value.</para> @@ -3303,7 +3303,7 @@ System.out.println("1 + ... + " + limit + " = " + sum); </programlisting> <answer> <para>First we need to print all values of our loop variable - <code>i</code>:</para> + <code language="java">i</code>:</para> <programlisting language="java">for (int i = 1; i <= limit; i++) { System.out.print(i + " + "); @@ -3312,9 +3312,9 @@ System.out.println("1 + ... + " + limit + " = " + sum); </programlisting> <para>This yields <computeroutput>1 + 2 + 3 + 4 + 5 +</computeroutput>. Notice the last - <quote><code>+</code></quote> operator: To avoid this operator + <quote><code language="java">+</code></quote> operator: To avoid this operator being printed we may split our print statement and add an - <code>if</code> guard inside our loop:</para> + <code language="java">if</code> guard inside our loop:</para> <programlisting language="java">for (int i = 1; i <= limit; i++) { System.out.print(i); @@ -3338,7 +3338,7 @@ System.out.println("1 + ... + " + limit + " = " + sum); </programlisting> } <emphasis role="bold">System.out.println(" = " + sum);</emphasis></programlisting> - <para>Instead of filtering a <quote><code>+</code></quote> + <para>Instead of filtering a <quote><code language="java">+</code></quote> operator we may as well terminate our loop earlier and move the last operand to the second print statement:</para> @@ -3691,7 +3691,7 @@ for (int i = 1; i < 50; i++) { <code xlink:href="https://docs.oracle.com/javase/9/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 + But even declaring <code language="java">long product = 1</code> does not help much:</para> <screen>1! == 1 @@ -3719,7 +3719,7 @@ for (int i = 1; i < 50; i++) { 49! == 8789267254022766592</screen> <para>This time <code>20! == 2432902008176640000</code> is the - last correct value being smaller than <code + last correct value being smaller than <code language="java" xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Long.html#MAX_VALUE">Long.MAX_VALUE</code> == 9223372036854775807.</para> @@ -4379,8 +4379,8 @@ try (final Scanner scan = new Scanner(System.in)) { <para>The remainder operator <quote xlink:href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.3">%</quote> allows for testing whether a given integer value - <code>n</code> can be divided by an another integer value - <code>k</code> without remainder.</para> + <code language="java">n</code> can be divided by an another integer value + <code language="java">k</code> without remainder.</para> <para>Increment an integer candidate in a loop till the desired condition is being fulfilled.</para> @@ -5486,7 +5486,7 @@ try (final Scanner scan = new Scanner(System.in)) { </m:math> </inlineequation> two variable values already determine the third one: For a given value of variable a from the outer - <code>for</code> loop any value of b will imply + <code language="java">for</code> loop any value of b will imply <inlineequation> <m:math display="inline"> <m:mrow> @@ -5515,7 +5515,7 @@ try (final Scanner scan = new Scanner(System.in)) { dependent.</para> <para>For convenience reasons we introduce a variable - <code>int sum = 840</code> representing our sum of all three + <code language="java">int sum = 840</code> representing our sum of all three values. Our filter condition now only needs to check for the remaining Pythagorean condition <inlineequation> <m:math display="inline"> @@ -5574,9 +5574,9 @@ for (int a = 1; a <= sum / 3; a++) { (240, 252, 348)</screen> <para>The above code can be simplified even further. We - introduce the variable s being shorthand for <code>sum</code> + introduce the variable s being shorthand for <code language="java">sum</code> in our <xref linkend="glo_Java"/> code. The inner - <code>for</code> loop so far starts with a value of + <code language="java">for</code> loop so far starts with a value of <inlineequation> <m:math display="inline"> <m:mrow> @@ -5609,11 +5609,11 @@ for (int a = 1; a <= sum / 3; a++) { </m:mrow> </m:mrow> </m:math> - </inlineequation>. Then during each step <code>b</code> is - being incremented by 1 and <code>c</code> will be decremented + </inlineequation>. Then during each step <code language="java">b</code> is + being incremented by 1 and <code language="java">c</code> will be decremented by the same amount.</para> - <para>Regarding the n-th step of the inner <code>for</code> + <para>Regarding the n-th step of the inner <code language="java">for</code> loop we thus look for a value of n so that the following equation becomes true:</para> @@ -5787,13 +5787,13 @@ for (int a = 1; a <= sum / 3; a++) { </informalequation> <para>So for each given value of a there is either no or - exactly one possible solution (since <code>n</code> is of + exactly one possible solution (since <code language="java">n</code> is of integer type). Our <quote>inner</quote> nested loop becomes obsolete.</para> <para>The above equation must hold for integer arithmetic leaving no fractional remainder. We introduce two variables - <code>numerator</code> and <code>denominator</code> + <code language="java">numerator</code> and <code language="java">denominator</code> representing the above terms <inlineequation> <m:math display="inline"> <m:mrow> @@ -5859,7 +5859,7 @@ for (int a = 1; a <= sum / 3; a++) { </m:mrow> </m:math> </inlineequation>. The <quote>remainder of modulus</quote> - operator <code>%</code> testing for a zero remainder filters + operator <code language="java">%</code> testing for a zero remainder filters the desired triples:</para> <programlisting language="java">final int sum = 840; @@ -5878,7 +5878,7 @@ for (int a = 1; a <= sum / 3; a++) { <para>This yields the same result as before but execution is (again) ways faster paying off when looking for larger values - of <code>sum</code>.</para> + of <code language="java">sum</code>.</para> </answer> </qandaentry> </qandadiv> diff --git a/Doc/Sd1/streams.xml b/Doc/Sd1/streams.xml index 06401774f71890eb2e194d6d57a0459f04e8025d..32f1a86898a640d56a9f41e66284de7299f5e810 100644 --- a/Doc/Sd1/streams.xml +++ b/Doc/Sd1/streams.xml @@ -208,19 +208,19 @@ final BufferedReader inputBufferedReader = new BufferedReader(fileReader);</prog <itemizedlist> <listitem> - <para>If <code>args.length == 0</code> assume to read from + <para>If <code language="java">args.length == 0</code> assume to read from standard input.</para> </listitem> <listitem> - <para>if <code>0 < args.length</code> try to interpret + <para>if <code language="java">0 < args.length</code> try to interpret the arguments as filenames.</para> </listitem> </itemizedlist> </listitem> <listitem> - <para>Write a class <code>TextFileStatistics</code> being able + <para>Write a class <classname>TextFileStatistics</classname> being able to and count characters, words and lines of a single input file. Instances of this class may be initialized from a <classname diff --git a/bin/docbook2ilias b/bin/docbook2ilias index 9f941980dfab52e6eca91e4a11a7f5142a441efc..0a23bc280a704ac3821fa4b520557fef66ac58f0 100755 --- a/bin/docbook2ilias +++ b/bin/docbook2ilias @@ -1,10 +1,18 @@ #!/bin/sh -java -cp "/usr/share/hdm-docbook-xsl/docbook-xsl-1.79.2/tools/lib/saxon9he.jar:/usr/share/java/xercesImpl.jar" \ +#java -cp "/usr/share/hdm-docbook-xsl/lib/xslthl-2.1.3.jar:/usr/share/hdm-docbook-xsl/docbook-xsl-1.79.2/tools/lib/saxon9he.jar:/usr/share/java/xercesImpl.jar" \ +# -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl \ +# -Dorg.apache.xerces.xni.parser.XMLParserConfiguration=org.apache.xerces.parsers.XIncludeParserConfiguration \ +# net.sf.saxon.Transform \ +#-s:$1 \ +# -xsl:/usr/share/hdm-docbook-xsl/CustomLayer/Klausur/docbook2klausur.xsl + + +java -cp "/usr/share/hdm-docbook-xsl/lib/xslthl-2.1.3.jar:/usr/share/hdm-docbook-xsl/docbook-xsl-1.79.2/tools/lib/saxon.jar:/usr/share/java/xercesImpl.jar" \ -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl \ -Dorg.apache.xerces.xni.parser.XMLParserConfiguration=org.apache.xerces.parsers.XIncludeParserConfiguration \ - net.sf.saxon.Transform \ - -s:$1 \ - -xsl:/usr/share/hdm-docbook-xsl/CustomLayer/Klausur/docbook2klausur.xsl + com.icl.saxon.StyleSheet \ + $1 \ + /usr/share/hdm-docbook-xsl/CustomLayer/Klausur/docbook2klausur.xsl #end diff --git a/ws/Docbook/CustomLayer/Klausur/docbook2klausur.xsl b/ws/Docbook/CustomLayer/Klausur/docbook2klausur.xsl index 99a02bd7b3ecf95c9ef8386c92298060f7b7aae5..af46583904a6f406adef4cd790ec4392c80d2167 100644 --- a/ws/Docbook/CustomLayer/Klausur/docbook2klausur.xsl +++ b/ws/Docbook/CustomLayer/Klausur/docbook2klausur.xsl @@ -1,6 +1,5 @@ <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" - xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes" @@ -10,6 +9,10 @@ xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:db="http://docbook.org/ns/docbook" + + xmlns:s6hl="java:net.sf.xslthl.ConnectorSaxon6" + xmlns:xslthl="http://xslthl.sf.net" + extension-element-prefixes="s6hl xslthl" exclude-result-prefixes="xs xlink xila xi trans db svg html m" version="2.0"> @@ -19,13 +22,13 @@ </xsl:template> <xsl:template match="db:chapter"> - <xsl:result-document href="{@xml:id}.html" method="html" indent="no"> + <xsl:document href="{@xml:id}.html" method="html" indent="no"> <html> <body > <xsl:apply-templates/> </body> </html> - </xsl:result-document> + </xsl:document> </xsl:template> <xsl:template match="db:title"> @@ -50,13 +53,7 @@ <xsl:apply-templates select="*|text()" mode="link"/> </p> </xsl:template> - - <xsl:template match="db:programlisting"> - <pre style="width: fit-content; border-radius: 10px; padding: 5px; font-size:90%; font-family: 'Lucida Console', Monaco, monospace; background-color: #F3ECFF;"> - <xsl:apply-templates mode="link"/> - </pre> - </xsl:template> - + <xsl:template match="db:screen"> <pre style="width: fit-content; border-radius: 10px; padding: 5px; font-size:90%; font-family: 'Lucida Console', Monaco, monospace; background-color: #E7FFFE;"> <xsl:apply-templates mode="link"/> @@ -73,10 +70,17 @@ <xsl:template match="db:orderedlist"> <ol> + <xsl:apply-templates select="@startingnumber"/> <xsl:apply-templates/> </ol> </xsl:template> + <xsl:template match="@startingnumber"> + <xsl:attribute name="start"> + <xsl:value-of select="."/> + </xsl:attribute> + </xsl:template> + <xsl:template match="db:listitem"> <li> <xsl:apply-templates/> @@ -155,12 +159,6 @@ </em> </xsl:template> - <xsl:template match="db:code"> - <code style='font-size:90%; color:black; font-family: "Lucida Console", Monaco, monospace; background-color: #F3ECFF;'> - <xsl:apply-templates/> - </code> - </xsl:template> - <xsl:template match="db:filename"> <span style="color: #810000;"> <xsl:apply-templates/> @@ -243,7 +241,108 @@ <xsl:text>'</xsl:text> </p> </xsl:template> + + <!-- + <xsl:template match="db:programlisting"> + <pre style="width: fit-content; border-radius: 10px; padding: 5px; font-size:90%; font-family: 'Lucida Console', Monaco, monospace; background-color: #F3ECFF;"> + <xsl:apply-templates select="text()|*" /> + </pre> + </xsl:template> + --> + + <xsl:template match="db:programlisting"> + <pre style="width: fit-content; border-radius: 10px; padding: 5px; font-size:90%; font-family: 'Lucida Console', Monaco, monospace; background-color: #F3ECFF;"> + <xsl:call-template name="syntax-highlight"> + <xsl:with-param name="language" select="@language" /> + <xsl:with-param name="source" select="." /> + <xsl:with-param name="config">file:///usr/share/hdm-docbook-xsl/docbook-xsl-1.79.2/highlighting/xslthl-config.xml</xsl:with-param> + </xsl:call-template> + </pre> + </xsl:template> + + <xsl:template match="db:code"> + <code style='font-size:90%; color:black; font-family: "Lucida Console", Monaco, monospace; background-color: #F3ECFF;'> + <xsl:call-template name="syntax-highlight"> + <xsl:with-param name="language" select="@language" /> + <xsl:with-param name="source" select="." /> + <xsl:with-param name="config">file:///usr/share/hdm-docbook-xsl/docbook-xsl-1.79.2/highlighting/xslthl-config.xml</xsl:with-param> + </xsl:call-template> + </code> + </xsl:template> + + <xsl:template match="xslthl:keyword"> + <span style="font-weight: bold;color: #602060;"> + <xsl:value-of select="."/> + </span> + </xsl:template> + <xsl:template match="xslthl:string"> + <span style="color: #A00000;"> + <xsl:value-of select="."/> + </span> + </xsl:template> + + <xsl:template match="xslthl:number|xslthl:hexnumber"> + <span style="color: #B08000;"> + <xsl:value-of select="."/> + </span> + </xsl:template> + + <xsl:template match="xslthl:comment|xslthl:doccomment"> + <span style="font-style: italic;color: #808080;"> + <xsl:value-of select="."/> + </span> + </xsl:template> + <xsl:template match="xslthl:directive"> + <span style="color: #00A000;"> + <xsl:value-of select="."/> + </span> + </xsl:template> + + <xsl:template match="xslthl:annotation"> + <span style="font-weight: bold;color: #808080;"> + <xsl:value-of select="."/> + </span> + </xsl:template> + + <xsl:template match="xslthl:tag"> + <span style="font-weight: bold; color: #602060;"> + <xsl:value-of select="."/> + </span> + </xsl:template> + + <xsl:template match="xslthl:attribute"> + <span style="color: #0050A0;"> + <xsl:value-of select="."/> + </span> + </xsl:template> + + <xsl:template match="xslthl:value"> + <span style="color: #A00000;"> + <xsl:value-of select="."/> + </span> + </xsl:template> + + <xsl:template match="xslthl:doctype"> + <span style="color: #008080;"> + <xsl:value-of select="."/> + </span> + </xsl:template> + + <xsl:template name="syntax-highlight"> + <xsl:param name="language" /> + <xsl:param name="source" /> + <xsl:param name="config"></xsl:param> + <xsl:choose> + <xsl:when test="function-available('s6hl:highlight')"> + <xsl:variable name="highlighted" select="s6hl:highlight($language, $source, $config)" /> + <xsl:apply-templates select="$highlighted" /> + </xsl:when> + <xsl:otherwise> + <xsl:apply-templates select="$source/*|$source/text()" /> + </xsl:otherwise> + </xsl:choose> + </xsl:template> </xsl:stylesheet> \ No newline at end of file