From 5d91169d071555dad315ed333a5c83d0ee6ce375 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Sun, 15 Mar 2015 22:08:57 +0100 Subject: [PATCH] data type related exercises --- Doc/Common/glossary.xml | 70 ++- Doc/Sd1/class.xml | 3 +- Doc/Sd1/languageFundamentals.xml | 752 +++++++++++++++++++++++++++++++ Doc/Sd1/steinbruch.xml | 134 ++++++ Doc/lectures.xml | 17 +- 5 files changed, 960 insertions(+), 16 deletions(-) create mode 100644 Doc/Sd1/languageFundamentals.xml create mode 100644 Doc/Sd1/steinbruch.xml diff --git a/Doc/Common/glossary.xml b/Doc/Common/glossary.xml index e566cf719..56ac475f4 100644 --- a/Doc/Common/glossary.xml +++ b/Doc/Common/glossary.xml @@ -36,6 +36,15 @@ </glossdef> </glossentry> + <glossentry xml:id="glo_byte"> + <glossterm>Byte</glossterm> + + <glossdef> + <para xlink:href="http://en.wikipedia.org/wiki/Byte">Unit of + information</para> + </glossdef> + </glossentry> + <glossentry xml:id="glo_CSV"> <glossterm><acronym>CSV</acronym></glossterm> @@ -240,22 +249,41 @@ </glossdef> </glossentry> - <glossentry xml:id="glo_Java"> - <glossterm><trademark>Java</trademark></glossterm> + <glossentry xml:id="glo_JavaLevel1"> + <glossterm>Java</glossterm> <glossdef> - <para><link - xlink:href="http://www.oracle.com/us/legal/third-party-trademarks/index.html">The - Java programming language</link></para> - </glossdef> - </glossentry> + <glosslist> + <glossentry xml:id="glo_Java_integralTypes"> + <glossterm><code>int</code></glossterm> - <glossentry xml:id="glo_Javadoc"> - <glossterm><trademark>Javadoc</trademark></glossterm> + <glossdef> + <para + xlink:href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.1">Integral + Types and Values</para> + </glossdef> + </glossentry> - <glossdef> - <para><link xlink:href="http://en.wikipedia.org/wiki/Javadoc">The Java - documentation system</link></para> + <glossentry xml:id="glo_Java"> + <glossterm><trademark>Java</trademark></glossterm> + + <glossdef> + <para><link + xlink:href="http://www.oracle.com/us/legal/third-party-trademarks/index.html">The + Java programming language</link></para> + </glossdef> + </glossentry> + + <glossentry xml:id="glo_Javadoc"> + <glossterm><trademark>Javadoc</trademark></glossterm> + + <glossdef> + <para><link + xlink:href="http://en.wikipedia.org/wiki/Javadoc">The Java + documentation system</link></para> + </glossdef> + </glossentry> + </glosslist> </glossdef> </glossentry> @@ -421,6 +449,15 @@ </glossdef> </glossentry> + <glossentry xml:id="glo_RGB"> + <glossterm><acronym>RGB</acronym></glossterm> + + <glossdef> + <para xlink:href="http://en.wikipedia.org/wiki/RGB_color_model">RGB + color model</para> + </glossdef> + </glossentry> + <glossentry xml:id="glo_SAX"> <glossterm><acronym>SAX</acronym></glossterm> @@ -477,6 +514,15 @@ </glossdef> </glossentry> + <glossentry xml:id="glo_unicode"> + <glossterm><acronym>Unicode</acronym></glossterm> + + <glossdef> + <para><link xlink:href="http://en.wikipedia.org/wiki/Unicode">The + Unicode standard</link></para> + </glossdef> + </glossentry> + <glossentry xml:id="glo_UNIX"> <glossterm><acronym>UNIX</acronym></glossterm> diff --git a/Doc/Sd1/class.xml b/Doc/Sd1/class.xml index 36b1ebd5c..3e9efc690 100644 --- a/Doc/Sd1/class.xml +++ b/Doc/Sd1/class.xml @@ -7,7 +7,8 @@ xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:db="http://docbook.org/ns/docbook"> - <title>Lecture 5 - Classes, instances and internal state (20.10)</title> + <title>-------------------------------- Classes, instances and internal + state (20.10)</title> <section xml:id="sd1CrabsEnhancePrepare"> <title>Preparations</title> diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml new file mode 100644 index 000000000..fd2e61fc3 --- /dev/null +++ b/Doc/Sd1/languageFundamentals.xml @@ -0,0 +1,752 @@ +<?xml version="1.0" encoding="UTF-8"?> +<chapter version="5.0" xml:id="sw1LanguageFundamental" + xmlns="http://docbook.org/ns/docbook" + xmlns:xlink="http://www.w3.org/1999/xlink" + xmlns:xi="http://www.w3.org/2001/XInclude" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns:m="http://www.w3.org/1998/Math/MathML" + xmlns:html="http://www.w3.org/1999/xhtml" + xmlns:db="http://docbook.org/ns/docbook"> + <title>Language Fundamentals</title> + + <section xml:id="sw1IntegerLiterals"> + <title>Integer value literals</title> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaMaxMinInt"> + <title>An <code>int</code>'s minimum and maximum value</title> + + <qandadiv> + <qandaentry> + <question> + <para>A <xref linkend="glo_Java"/> <code>int</code> is internally + being represented by 4 <link linkend="glo_byte">bytes</link>. + <inlineequation> + <m:math display="inline"> + <m:msub> + <m:mi>00000000000000000000000000000101</m:mi> + + <m:mi>2</m:mi> + </m:msub> + </m:math> + </inlineequation> for example represents the decimal value + 5.</para> + + <para>In order to represent negative values as well <xref + linkend="glo_Java"/> uses <link + xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's + complement</link> representation. We provide some values:</para> + + <table border="1" xml:id="sw1Table4ByteIntegerRepresentation"> + <caption>4 Byte <link + xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's + complement</link> representation of <code>int</code> + values.</caption> + + <tr> + <th>Two complement representation</th> + + <th>Decimal representation</th> + </tr> + + <tr> + <td><code>00000000000000000000000000000000</code></td> + + <td>0</td> + </tr> + + <tr> + <td><code>01111111111111111111111111111111</code></td> + + <td><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:msup> + <m:mi>2</m:mi> + + <m:mrow> + <m:mi>16</m:mi> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:msup> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:math> + </inlineequation> (Maximum)</td> + </tr> + + <tr> + <td><code>10000000000000000000000000000000</code></td> + + <td><inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>-</m:mo> + + <m:msup> + <m:mi>2</m:mi> + + <m:mrow> + <m:mi>16</m:mi> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:msup> + </m:mrow> + </m:math> + </inlineequation> (Minimum)</td> + </tr> + + <tr> + <td><code>11111111111111111111111111111111</code></td> + + <td>-1</td> + </tr> + </table> + + <para>Use <link + xlink:href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1"><code>int</code> + literals</link> in <link + xlink:href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-BinaryNumeral">binary + representation</link> like e.g. <code>0B1100</code> in section + <quote + xlink:href="http://proquest.safaribooksonline.com/9780992133047/toc6_html_2">Literals</quote> + in order to write an <code>int</code>'s minimum and maximum + possible value to standard output.</para> + + <programlisting language="java"> public static void main(String[] args) { + + int minumum = ... , //TODO: provide values by + maximum = ...; // binary int literals + + System.out.println("Minimum:" + minumum); + System.out.println("Maximum:" + maximum); + }</programlisting> + </question> + + <answer> + <para>We insert <link + xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's + complement</link> representations of minimum and maximum int + values according to <xref + linkend="sw1Table4ByteIntegerRepresentation"/>.</para> + + <programlisting language="java">public static void main(String[] args) { + + int minumum = 0B10000000_00000000_00000000_00000000, + maximum = 0B01111111_11111111_11111111_11111111; + + System.out.println("Minimum int value:" + minumum); + System.out.println("Maximum int value:" + maximum); + }</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <section xml:id="sw1SectHexadecimalLiterals"> + <title>Hexadecimal literals</title> + + <para>Using the decimal system to represent integer values we tend to + ignore other important numbering systems. As you may know the <xref + linkend="glo_RGB"/> color model uses triplets of numbers to define color + value components representing intensities of three base colors + <foreignphrase><emphasis role="bold">R</emphasis>ed</foreignphrase>, + <foreignphrase><emphasis role="bold">G</emphasis>reen</foreignphrase> + and <foreignphrase><emphasis + role="bold">B</emphasis>lue</foreignphrase>. The component values range + from 0 to 255, the latter defining maximum intensity.</para> + + <para>The color <quote>red</quote> for example is being represented by + (255, 0, 0). So the red component has maximum intensity, blue and green + are zero.</para> + + <para>It is common to use <link + xlink:href="http://www.december.com/html/spec/color16codes.html">hexadecimal + in favour of decimal values</link>. Thus the font color + <quote>red</quote> in the subsequent HTML example's heading is being + represented by <emphasis role="bold">(FF,0,0)</emphasis>:</para> + + <programlisting language="html"><!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <title>A simple color example</title> + </head> + + <body><h1 style="<emphasis role="bold">color: #FF0000;</emphasis>">My heading</h1></body> +</html></programlisting> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaIntByBinary"> + <title>Integer values by binary representations</title> + + <qandadiv> + <qandaentry> + <question> + <para>Calculate the decimal representation of the hexadecimal + value <inlineequation> + <m:math display="inline"> + <m:msub> + <m:mi>CO</m:mi> + + <m:mi>16</m:mi> + </m:msub> + </m:math> + </inlineequation>.</para> + + <para>Complete the following code by assigning the hexadecimal + value <inlineequation> + <m:math display="inline"> + <m:msub> + <m:mi>CO</m:mi> + + <m:mi>16</m:mi> + </m:msub> + </m:math> + </inlineequation> (The <quote>silver</quote> color all three + component's intensity in <uri + xlink:href="http://www.december.com/html/spec/color16codes.html">http://www.december.com/html/spec/color16codes.html</uri>) + to get the output in decimal representation.</para> + + <programlisting language="java">public static void main(String[] args) { + short intensity = ...; + + System.out.println(intensity); +}</programlisting> + + <tip> + <para>You may want to consider <quote + xlink:href="http://proquest.safaribooksonline.com/9780992133047/toc6_html_2">Literals</quote> + to find out how to represent integer values in hexadecimal + notation.</para> + </tip> + </question> + + <answer> + <para>The solution is straightforward:</para> + + <programlisting language="java"> public static void main(String[] args) { + + short intensity = 0xC0; + + System.out.println(intensity); + }</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaIntLimitShort"> + <title>Testing the limits, part 2 (Difficult)</title> + + <qandadiv> + <qandaentry> + <question> + <para>A careful programmer is worried whether a short variable + is <emphasis role="bold">always</emphasis> large enough to hold + color intensity values ranging from 0 to 255. Give an answer + being based on the <link + xlink:href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.1">specification</link>.</para> + + <para>The programmer tries to adopt the <link + linkend="sw1QandaMaxMinInt">related int example</link> and + adopts it accordingly:</para> + + <programlisting language="java">short minumum = 0B10000000_00000000, + maximum = 0B01111111_11111111; + +System.out.println("Minimum short value:" + minumum); +System.out.println("Maximum short value:" + maximum); +</programlisting> + + <para>Our programmer is baffled since:</para> + + <itemizedlist> + <listitem> + <para>The first assignment <code language="java">minumum = + 0B10000000_00000000</code> is being flagged as a compile + time error:</para> + + <para><computeroutput>Type mismatch: cannot convert from int + to short</computeroutput></para> + </listitem> + + <listitem> + <para>On contrary the second assignment <code + language="java">maximum = 0B01111111_11111111</code> gets + smoothly accepted.</para> + </listitem> + </itemizedlist> + + <tip> + <para>Which integer literals types do exist according to the + <xref linkend="glo_Java"/> <link + xlink:href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1">standard</link>?</para> + </tip> + </question> + + <answer> + <para>The programmer's question is basically easy to answer: + Since <code>short</code> variables are being represented by to + bytes their range is <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mo>[</m:mo> + + <m:mrow> + <m:mrow> + <m:mo>-</m:mo> + + <m:msup> + <m:mi>2</m:mi> + + <m:mi>15</m:mi> + </m:msup> + </m:mrow> + + <m:mo>,</m:mo> + + <m:mrow> + <m:msup> + <m:mi>2</m:mi> + + <m:mi>15</m:mi> + </m:msup> + + <m:mo>-</m:mo> + + <m:mi>1</m:mi> + </m:mrow> + </m:mrow> + + <m:mo>]</m:mo> + </m:mrow> + </m:math> + </inlineequation>. Thus intensity values ranging from 0 to 255 + will be easily accommodated.</para> + + <para>The second question is more difficult to explain: The + <xref linkend="glo_Java"/> standard does define <code>int</code> + literals. It does however not define <code>short</code> (or + <code>byte</code>) literals.</para> + + <para>Thus the sequence 0B01111111_11111111 will be treated as + an integer value which is compatible with a short and is thus + assignable.</para> + + <para>On the other hand the <code>int</code> literal + <code>0B10000000_00000000</code> evaluates to +32768 and can + thus not be assigned to a short at all not to mention its non- + <link + xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's + complement</link> representation. In order to avoid this error + we need an explicit cast:</para> + + <programlisting language="java">short minumum = (short) 0B10000000_00000000;</programlisting> + + <para>Since a short ranges from -32768 to 32767 we may as well + use a unary minus sign instead:</para> + + <programlisting>short minumum = -0B10000000_00000000;</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + </section> + + <section xml:id="sd1SimpleExpressions"> + <title>Simple expressions</title> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaSquareArea"> + <title>Calculating the area of a square</title> + + <qandadiv> + <qandaentry> + <question> + <para>The area <inlineequation> + <m:math display="inline"> + <m:mi>a</m:mi> + </m:math> + </inlineequation> of a given square having radius + <inlineequation> + <m:math display="inline"> + <m:mi>r</m:mi> + </m:math> + </inlineequation> is being obtained by <inlineequation> + <m:math display="inline"> + <m:mrow> + <m:mi>a</m:mi> + + <m:mo>=</m:mo> + + <m:mrow> + <m:mi>π</m:mi> + + <m:mo>×</m:mo> + + <m:msup> + <m:mi>r</m:mi> + + <m:mi>2</m:mi> + </m:msup> + </m:mrow> + </m:mrow> + </m:math> + </inlineequation>. Complete the following code to calculate the + result and write it to standard output using + <code>System.out.println(...)</code>:</para> + + <programlisting language="java">public static void main(String[] args) { + + double radius = 2.31; // A square having a radius (given e.g. in mm). + double pi = 3.1415926; // Constant relating a square's radius, perimeter and area. + + // TODO: Write the circle's area to standard output +}</programlisting> + + <tip> + <para>You may want to read the <link + xlink:href="http://proquest.safaribooksonline.com/9780992133047/toc1_html_4">overview + section</link> on statements.</para> + </tip> + </question> + + <answer> + <glosslist> + <glossentry> + <glossterm>Using a mere expression</glossterm> + + <glossdef> + <programlisting language="java">public static void main(String[] args) { + + double radius = 2.31; // A square having a radius (given e.g. in mm). + double pi = 3.1415926; // Constant relating a square's radius, perimeter and area. + + System.out.println(pi * radius * radius); +}</programlisting> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Using a variable</glossterm> + + <glossdef> + <para>Instead of immediately using the expression as an + argument to <code>System.out.println(...)</code> we may + assign its value to a variable prior to output + creation:</para> + + <programlisting language="java">public static void main(String[] args) { + + double radius = 2.31; // A square having a radius (given e.g. in mm). + double pi = 3.1415926; // Constant relating a square's radius, perimeter and area. + + double area = pi * radius * radius; + System.out.println(area); +}</programlisting> + </glossdef> + </glossentry> + </glosslist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <section xml:id="sd1ExpressionStrangeThings"> + <title>Strange things happen</title> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaByteOverflow"> + <title>Calculating the area of a square</title> + + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code snippet:</para> + + <programlisting language="java">byte a = 127; +System.out.println("value=" + a); +a++; +System.out.println("New value=" + a); +</programlisting> + + <para>This will produce the following output:</para> + + <programlisting language="none">value=127 +New value=-128</programlisting> + + <para>Explain this strange behaviour. Moreover you'll find the + following code to rise a compile time error:</para> + + <programlisting language="java">byte a = 127; +System.out.println("value=" + a); +a = a + 1; // Error: Type mismatch: cannot convert from int to byte +System.out.println("New value=" + a);</programlisting> + + <tip> + <para>You may want to read the <link + xlink:href="http://proquest.safaribooksonline.com/9780992133047/toc1_html_4">overview + section</link> on statements.</para> + </tip> + </question> + + <answer> + <para>A byte variable ranges from -128 to +127. Thus + incrementing 127 by 1 yields an overflow error.</para> + + <para>Since <xref linkend="glo_Java"/> uses <link + xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's + complement</link> representation we have:</para> + + <informaltable border="1"> + <tr> + <td><code> 01111111</code></td> + + <td><code> 127</code></td> + </tr> + + <tr> + <td><code>+00000001</code></td> + + <td><code> +1</code></td> + </tr> + + <tr> + <td><code>=10000000</code></td> + + <td><code>=-128</code></td> + </tr> + </informaltable> + + <para>On machine level the above calculation is just an ordinary + addition.</para> + + <para>Conclusion: Watch out when doing (integer) arithmetic! + </para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + </section> + + <section xml:id="sw1StringLiterals"> + <title>Dealing with strings</title> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaStringVariableMix"> + <title>Composing strings of literals and variables</title> + + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code:</para> + + <programlisting language="java">public static void main(String[] args) { + System.out.println("Some 'special' words."); +}</programlisting> + + <para>The corresponding output will be <computeroutput>Some + 'special' words.</computeroutput>. Change the above code to + replace the single quotes by double quotes producing the output + <computeroutput>Some "special" words.</computeroutput> + instead.</para> + + <tip> + <para>Hunt for <quote>java escape double quote</quote> and read + about <link + xlink:href="http://proquest.safaribooksonline.com/book/programming/java/9780992133047/chapter-2-language-fundamentals/toc6_html_2#readertoolbar2">character + literals</link>.</para> + </tip> + </question> + + <answer> + <para>There are at least two solutions on offer:</para> + + <glosslist> + <glossentry> + <glossterm>Perfectly obvious</glossterm> + + <glossdef> + <para>Inside a string literal the terminating character (") + may be escaped using a backslash (\):</para> + + <programlisting language="java">System.out.println("Some \"special\" words.");</programlisting> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Even more clumsy</glossterm> + + <glossdef> + <para>Double quotes may be represented by the character (not + string!) literal <quote>'"'</quote>. We may then compose our + output string by:</para> + + <programlisting language="java">System.out.println("Some " + '"' + "special" + '"' + " words.");</programlisting> + </glossdef> + </glossentry> + </glosslist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaStringEscapeDoubleQuote"> + <title>Escaping double quotes</title> + + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code:</para> + + <programlisting language="java">public static void main(String[] args) { + System.out.println("Some 'special' words."); +}</programlisting> + + <para>The corresponding output will be <computeroutput>Some + 'special' words.</computeroutput>. Change the above code to + replace the single quotes by double quotes producing the output + <computeroutput>Some "special" words.</computeroutput> + instead.</para> + + <tip> + <para>Hunt for <quote>java escape double quote</quote> and read + about <link + xlink:href="http://proquest.safaribooksonline.com/book/programming/java/9780992133047/chapter-2-language-fundamentals/toc6_html_2#readertoolbar2">character + literals</link>.</para> + </tip> + </question> + + <answer> + <para>There are at least two solutions on offer:</para> + + <glosslist> + <glossentry> + <glossterm>Perfectly obvious</glossterm> + + <glossdef> + <para>Inside a string literal the terminating character (") + may be escaped using a backslash (\):</para> + + <programlisting language="java">System.out.println("Some \"special\" words.");</programlisting> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Even more clumsy</glossterm> + + <glossdef> + <para>Double quotes may be represented by the character (not + string!) literal <quote>'"'</quote>. We may then compose our + output string by:</para> + + <programlisting language="java">System.out.println("Some " + '"' + "special" + '"' + " words.");</programlisting> + </glossdef> + </glossentry> + </glosslist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sw1LanguageFundamentalUsingFinal"> + <title>Using <code>final</code></title> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaSquareAreaFinal"> + <title>Calculating the area of a square avoiding accidental + redefinition</title> + + <qandadiv> + <qandaentry> + <question> + <para>In Exercise <xref linkend="sw1QandaSquareArea"/> you + calculated a given square's area:</para> + + <programlisting language="java"> public static void main(String[] args) { + + double radius = 2.31; // A square having a radius (given e.g. in mm). + double pi = 3.1415926; // Constant relating a square's radius, perimeter and area. + + double area = pi * radius * radius; + System.out.println(area); + }</programlisting> + + <para>Though there is nothing wrong with this approach but it may + be considered error prone. A careless programmer may accidentally + redefine the value of <code>pi</code>:</para> + + <programlisting language="java"> public static void main(String[] args) { + + double radius = 2.31; // A square having a radius (given e.g. in mm). + double pi = 3.1415926; // Constant relating a square's radius, perimeter and area. + + // Some lines of code in between + // ... + + pi = -17; // Woops! This shall not happen! + + // More lines of code + // ... + + double area = pi * radius * radius; + System.out.println(area); + }</programlisting> + + <para>Modify the original code to avoid this type of error.</para> + + <tip> + <para>You may want to read the <quote + xlink:href="http://proquest.safaribooksonline.com/9780992133047/toc5_html_2">Constants</quote> + section.</para> + </tip> + </question> + + <answer> + <para>The solution is straightforward. We add the + <code>final</code> modifier to the definition of our variable + <code>pi</code>:</para> + + <programlisting language="java" linenumbering="numbered">public static void main(String[] args) { + + double radius = 2.31; // A square having a radius (given e.g. in mm). + <emphasis role="bold">final</emphasis> double pi = 3.1415926; // Creating pi as a constant (non-modifiable/assignable) variable. + + // Longer code section in between + // ... + + pi = -17; // Compile time error. + + // Another longer code section + // ... + + <emphasis role="bold">final</emphasis> double area = pi * radius * radius; + System.out.println(area); +}</programlisting> + + <para>Now our flawed assignment at line 9 will be flagged as a + compile time error:</para> + + <para><computeroutput>The final local variable pi cannot be + assigned. It must be blank and not using a compound + assignment</computeroutput></para> + + <para>Note the second <code>final</code> modification in the + definition of our variable <code>area</code> to avoid erroneous + redefinitions as well.</para> + + <para>As a rule of thumb: Whenever you expect a variable not to + change after initial assignment use <code>final</code> to declare + it as being constant.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> +</chapter> diff --git a/Doc/Sd1/steinbruch.xml b/Doc/Sd1/steinbruch.xml new file mode 100644 index 000000000..90c9d3e53 --- /dev/null +++ b/Doc/Sd1/steinbruch.xml @@ -0,0 +1,134 @@ +<?xml version="1.0" encoding="UTF-8"?> +<chapter version="5.0" xml:id="steinbruch" + xmlns="http://docbook.org/ns/docbook" + xmlns:xlink="http://www.w3.org/1999/xlink" + xmlns:xi="http://www.w3.org/2001/XInclude" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns:m="http://www.w3.org/1998/Math/MathML" + xmlns:html="http://www.w3.org/1999/xhtml" + xmlns:db="http://docbook.org/ns/docbook"> + <title xml:id="sw1Steinbruch" xml:lang="de">Steinbruch</title> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaSquareAreaMathPackage"> + <title>Using constants from the <classname + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html">java.lang.Math</classname> + class.</title> + + <qandadiv> + <qandaentry> + <question> + <para>In Exercise <xref linkend="sw1QandaSquareAreaFinal"/> you + calculated a given square's area protecting variables against + accidental redefinition using the <code>final</code> + modifier:</para> + + <programlisting language="java">public static void main(String[] args) { + + double radius = 2.31; // A square having a radius (given e.g. in mm). + final double pi = 3.1415926; // Creating pi as a constant (non-modifiable/assignable) variable. + + final double area = pi * radius * radius; + System.out.println(area); +}</programlisting> + + <para>You may have wondered why you had to punch in the value of + <inlineequation> + <m:math display="inline"> + <m:mi>π</m:mi> + </m:math> + </inlineequation> yourself. Actually <xref linkend="glo_Java"/> + predefines a lot of constants in its standard <classname + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html">java.lang.Math</classname> + class. Read its documentation to replace your own <code>pi</code> + variable.</para> + + <tip> + <para>You may want to read the <quote + xlink:href="http://proquest.safaribooksonline.com/9780992133047/toc11_html_2">Static + Members</quote> and <quote + xlink:href="http://proquest.safaribooksonline.com/9780992133047/toc6_html_4">Java + Packages</quote> sections .</para> + </tip> + </question> + + <answer> + <para>The short answer simply is:</para> + + <programlisting language="java" linenumbering="numbered">public static void main(String[] args) { + + double radius = 2.31; // A square having a radius (given e.g. in mm). + + final double area = Math.PI * radius * radius; + System.out.println(area); +}</programlisting> + + <para>In case you bother about the somewhat clumsy Math.PI rather + than just having PI itself some syntactic sugar may be added on + top:</para> + + <programlisting language="java" linenumbering="numbered">import static java.lang.Math.PI; + +public class CircleAreaCalculator { + + public static void main(String[] args) { + + double radius = 2.31; // A square having a radius (given e.g. in mm). + + final double area = PI * radius * radius; + System.out.println(area); + } +}</programlisting> + + <para>We dig a little deeper to fully understand the underlying + concepts. Obtaining a <xref linkend="glo_JDK"/>'s source code and + browsing its implementation <filename>Math.java</filename> + reveals:</para> + + <programlisting language="java">/* + * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved. + * ... + */ + +package java.lang; +... + +public final class Math { + +... + /** + * The {@code double} value that is closer than any other to + * <i>pi</i>, the ratio of the circumference of a circle to its + * diameter. + */ + <emphasis role="bold">public static final double PI = 3.14159265358979323846;</emphasis> +...</programlisting> + + <para>This accounts for using the expression <code><varname + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#field.summary">Math.PI</varname></code>. + The careful reader may have expected an <code>import</code> + statement in order to use the <classname + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html">Math</classname> + class:</para> + + <programlisting language="java">import java.lang.<emphasis + role="bold">Math</emphasis>; // Optional: Classes from java.lang are imported + // automatically as per Java language specification. + +public class CircleAreaCalculator { + + public static void main(String[] args) { + + double radius = 2.31; // A square having a radius (given e.g. in mm). + + final double area = <emphasis role="bold">Math</emphasis>.PI * radius * radius; + System.out.println(area); + } +}</programlisting> + + <para>The comment states the mere fact that this import is simply + unnecessary.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> +</chapter> diff --git a/Doc/lectures.xml b/Doc/lectures.xml index 1483f1618..c4c886c98 100644 --- a/Doc/lectures.xml +++ b/Doc/lectures.xml @@ -30,14 +30,24 @@ </info> <xi:include href="Sd1/preliminaries.xml" xpointer="element(/1)"/> + + <xi:include href="Sd1/languageFundamentals.xml" xpointer="element(/1)"/> + + <xi:include href="Sd1/steinbruch.xml" xpointer="element(/1)"/> + <xi:include href="Sd1/class.xml" xpointer="element(/1)"/> + <xi:include href="Sd1/loop.xml" xpointer="element(/1)"/> + <xi:include href="Sd1/array.xml" xpointer="element(/1)"/> + <xi:include href="Sd1/identity.xml" xpointer="element(/1)"/> + <xi:include href="Sd1/deployment.xml" xpointer="element(/1)"/> + <xi:include href="Sd1/streams.xml" xpointer="element(/1)"/> - <xi:include href="Sd1/collections.xml" xpointer="element(/1)"/> + <xi:include href="Sd1/collections.xml" xpointer="element(/1)"/> </part> <part xml:id="sda1"> @@ -62,7 +72,6 @@ <xi:include href="Sda1/testng.xml" xpointer="element(/1)"/> <xi:include href="Sda1/fo.xml" xpointer="element(/1)"/> - </part> <part xml:id="sda2"> @@ -71,11 +80,13 @@ </info> <xi:include href="Sda2/ldap.xml" xpointer="element(/1)"/> + <xi:include href="Sda2/jpa.xml" xpointer="element(/1)"/> + <xi:include href="Sda2/jax-rs.xml" xpointer="element(/1)"/> </part> <xi:include href="Common/bibliography.xml" xpointer="element(/1)"/> - <xi:include href="Common/glossary.xml" xpointer="element(/1)"/> + <xi:include href="Common/glossary.xml" xpointer="element(/1)"/> </book> -- GitLab