From 4c13972c5b0c8a7997b536734260dd65424029d9 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Sun, 22 Mar 2015 21:56:48 +0100 Subject: [PATCH] Two octal representation examples --- Doc/Sd1/extraExercise.xml | 6 +++ Doc/Sd1/languageFundamentals.xml | 82 ++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/Doc/Sd1/extraExercise.xml b/Doc/Sd1/extraExercise.xml index 7160f239a..c9712eb2d 100644 --- a/Doc/Sd1/extraExercise.xml +++ b/Doc/Sd1/extraExercise.xml @@ -27,6 +27,12 @@ xlink:href="https://projecteuler.net/problem=1">1</link>, <link xlink:href="https://projecteuler.net/problem=2">2</link>, <link xlink:href="https://projecteuler.net/problem=4">4</link>, <link + xlink:href="https://projecteuler.net/problem=5">5</link>, <link + xlink:href="https://projecteuler.net/problem=8">8</link>, <link + xlink:href="https://projecteuler.net/problem=9">9</link>, <link + xlink:href="https://projecteuler.net/problem=1">1</link>, <link + xlink:href="https://projecteuler.net/problem=1">1</link>, <link + xlink:href="https://projecteuler.net/problem=1">1</link>, <link xlink:href="https://projecteuler.net/problem=1">1</link>, <link xlink:href="https://projecteuler.net/problem=1">1</link>, <link xlink:href="https://projecteuler.net/problem=1">1</link>, <link diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml index 106268f3e..53a203c9c 100644 --- a/Doc/Sd1/languageFundamentals.xml +++ b/Doc/Sd1/languageFundamentals.xml @@ -1141,4 +1141,86 @@ System.out.println("New value=" + a);</programlisting> </qandadiv> </qandaset> </section> + + <section xml:id="sd1fundamentalsWildThings"> + <title>Wild things</title> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaWild"> + <qandadiv> + <qandaentry> + <question> + <para>Consider the following program:</para> + + <programlisting language="java"> public static void main(String[] args) { + + int a = 20, + b = 3, + c = 9; + + System.out.println(a + " + " + b + " + " + c + " = " + (a + b + c)); + + }</programlisting> + + <para>This will run smoothly producing the expected output:</para> + + <programlisting language="none">20 + 3 + 9 = 32</programlisting> + + <para>We now prettify our variable definitions by introducing + right aligning numbers thereby padding leading positions with + zeros:</para> + + <programlisting language="java"> public static void main(String[] args) { + + int a = 20, + b = 03, + <emphasis role="bold">c = 09; // Compiler error: The literal 09 of type int is out of range</emphasis> + + System.out.println(a + " + " + b + " + " + c + " = " + (a + b + c)); + }</programlisting> + + <para>The above code does not compile due to the compiler error + when defining variable <code>c</code>.</para> + + <para>Explain the underlying cause of this error message. Why is + <code>b = 03</code> just fine in contrast to <code>c = 09</code> + ?</para> + </question> + + <answer> + <para>Integer literals starting with <quote>0</quote> are being + interpreted as octal representation. Since the octal system's set + of digits is {0,1,2,3,4,5,6,7} the value <quote>09</quote> is + simply not valid.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sd1OctalOutput"> + <title>Strange output</title> + + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code:</para> + + <programlisting language="java"> public static void main(String[] args) { + + int a = 041; + System.out.println("Value = " + a); + }</programlisting> + + <para>On execution we receive the output <code>Value = 33</code>. + Explain this result</para> + </question> + + <answer> + <para>This problem is related to the previous exercise: The + integer literal 041 defines octal representation. Changing from + octal to decimal representation takes us to 4 * 8 + 1 = 33.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> </chapter> -- GitLab