diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml index fd2e61fc3e3480ae10b286fb35d121e3d5e0056c..742aae5201d539e53a2257df0990f204fcbfacf7 100644 --- a/Doc/Sd1/languageFundamentals.xml +++ b/Doc/Sd1/languageFundamentals.xml @@ -9,6 +9,294 @@ xmlns:db="http://docbook.org/ns/docbook"> <title>Language Fundamentals</title> + <section xml:id="sw1LegalVariableName"> + <title>Legal variable names</title> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaLegalVarNames"> + <title>Legal variable names</title> + + <qandadiv> + <qandaentry> + <question> + <para>Which of the following names are legal variable names? + Complete the following table and explain your decision with + respect to the <quote + xlink:href="http://proquest.safaribooksonline.com/9780992133047/toc4_html_3">variables</quote> + section.</para> + + <informaltable border="1"> + <colgroup width="19%"/> + + <colgroup width="10%"/> + + <colgroup width="71%"/> + + <tr> + <th>Identifier</th> + + <th>is legal? (yes / no)</th> + + <th>Explanation / remark</th> + </tr> + + <tr> + <td><code>for</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>sum_of_data</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>sumOfData</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>first-name</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>ABC</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>42isThesolution</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>println</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>B4</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>AnnualSalary</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>"hello"</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>_average </code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>ανδÏος</code></td> + + <td/> + + <td/> + </tr> + + <tr> + <td><code>$sum </code></td> + + <td/> + + <td/> + </tr> + </informaltable> + + <tip> + <para>You may want to prepare a simple <xref + linkend="glo_Java"/> program and just test the above + names.</para> + </tip> + </question> + + <answer> + <para>We may write a small <xref linkend="glo_Java"/> + program:</para> + + <programlisting language="java">public static void main(String[] args) { + int 42isThesolution = 6; // <emphasis role="bold">Syntax error on token "42", delete this token</emphasis> +}</programlisting> + + <para>Unfortunately the error explanation is not too helpful with + respect to <xref linkend="glo_Java"/> beginners. It does not + immediately point towards an illegal identifier problem. One + indication of a sound compiler is its ability to provide + meaningful, self explanatory error messages.</para> + + <informaltable border="1"> + <colgroup width="19%"/> + + <colgroup width="10%"/> + + <colgroup width="71%"/> + + <tr> + <th>Identifier</th> + + <th>is legal? (yes / no)</th> + + <th>Explanation / remark</th> + </tr> + + <tr> + <td><code>for</code></td> + + <td>no</td> + + <td><code>for</code> is a Java keyword.</td> + </tr> + + <tr> + <td><code>sum_of_data</code></td> + + <td>yes</td> + + <td/> + </tr> + + <tr> + <td><code>sumOfData</code></td> + + <td>yes</td> + + <td/> + </tr> + + <tr> + <td><code>first-name</code></td> + + <td>no</td> + + <td>Operators like <quote>-</quote> must not appear in + variable names.</td> + </tr> + + <tr> + <td><code>ABC</code></td> + + <td>yes</td> + + <td>Best practices: Discouraged variable name, non-constant + variables should start with lowercase letters.</td> + </tr> + + <tr> + <td><code>42isThesolution</code></td> + + <td>no</td> + + <td>Identifiers must not start with a number.</td> + </tr> + + <tr> + <td><code>println</code></td> + + <td>yes</td> + + <td><code>println</code> is part of + <code>System.out.println(...)</code> but is no <xref + linkend="glo_Java"/> keyword.</td> + </tr> + + <tr> + <td><code>B4</code></td> + + <td>yes</td> + + <td>Best practices: Discouraged variable name, non-constant + variables should start with lowercase letters.</td> + </tr> + + <tr> + <td><code>AnnualSalary</code></td> + + <td>yes</td> + + <td>Best practices: Discouraged variable name, non-constant + variables should start with lowercase letters.</td> + </tr> + + <tr> + <td><code>"hello"</code></td> + + <td>no</td> + + <td>String delimiters must not be part of an identifier.</td> + </tr> + + <tr> + <td><code>_average </code></td> + + <td>yes</td> + + <td>Best practices: Should be reserved for system code.</td> + </tr> + + <tr> + <td><code>ανδÏος</code></td> + + <td>yes</td> + + <td>Perfectly legal Greek <orgname>Unicode</orgname> + characters.</td> + </tr> + + <tr> + <td><code>$sum </code></td> + + <td>yes</td> + + <td>Best practices: Should be reserved for system code.</td> + </tr> + </informaltable> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + <section xml:id="sw1IntegerLiterals"> <title>Integer value literals</title> @@ -353,7 +641,7 @@ System.out.println("Maximum short value:" + maximum); <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> + <programlisting language="java">short minumum = -0B10000000_00000000;</programlisting> </answer> </qandaentry> </qandadiv> @@ -463,7 +751,7 @@ System.out.println("Maximum short value:" + maximum); <title>Strange things happen</title> <qandaset defaultlabel="qanda" xml:id="sw1QandaByteOverflow"> - <title>Calculating the area of a square</title> + <title>Byte integer arithmetics</title> <qandadiv> <qandaentry> @@ -527,8 +815,113 @@ System.out.println("New value=" + a);</programlisting> <para>On machine level the above calculation is just an ordinary addition.</para> - <para>Conclusion: Watch out when doing (integer) arithmetic! - </para> + <para>Conclusion: Watch out when doing (integer) + arithmetic!</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sw1SectAssignArithmetics"> + <title>Assignments and simple arithmetics</title> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaGuessingResults"> + <title>Guessing results</title> + + <qandadiv> + <qandaentry> + <question> + <para>Consider the following code segment:</para> + + <programlisting language="java"> int a = 3; + a++; //Incrementing a by 1 --> a==4 + + int b = a; // TODO + + b--; // TODO + --b; // TODO + + int c = b; // TODO + + b = ++a; // TODO + int e = a++; // TODO + + a *= b; // TODO + + System.out.println("a=" + a); + System.out.println("b=" + b); + System.out.println("c=" + c); + System.out.println("e=" + e);</programlisting> + + <para>We want to guess the expected result. Copy/paste the above + code into a main() method body within your IDE of choice + (probably Eclipse). Though it is tempting to just execute this + code ant watch the results <emphasis role="bold">please refrain + from doing so!</emphasis></para> + + <para>Instead start a <quote>dry run</quote> complete the above + comments to predict the expected outcome:</para> + + <para><computeroutput>a=...</computeroutput></para> + + <para><computeroutput>b=...</computeroutput></para> + + <para><computeroutput>c=...</computeroutput></para> + + <para><computeroutput>e=...</computeroutput></para> + + <para>After finishing your guesses execute your code and 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 clarity. The difference is not about + operator precedence rules but simply about the values of these + expressions when e.g. being assigned.</para> + </tip> + </question> + + <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 + evaluates to:</para> + + <itemizedlist> + <listitem> + <para><code>a = x++</code> yields a ==x </para> + </listitem> + + <listitem> + <para><code>a = ++x</code> yields a ==(x + 1)</para> + </listitem> + </itemizedlist> + + <para>The rest is just obeying the <quote>due diligence</quote> + rule set:</para> + + <programlisting language="java"> int a = 3; + a++; //Incrementing a by 1 --> a==4 + + int b = a; // Assigning value of a --> b==4 + + b--; // Decrementing b by 1 --> b==3 + --b; // Decrementing b by 1 --> b==2 + + int c = b; // c == 2; + + b = ++a; // Incrementing a by 1 -->a==5, then assigning to b --> b == 5 + int e = a++; // Assigning a to e --> e==5, then incrementing a --> a==6 + + a *= b; // Multiplying a with b and assigning the result to a --> a==30 + + System.out.println("a=" + a); + System.out.println("b=" + b); + System.out.println("c=" + c); + System.out.println("e=" + e);</programlisting> </answer> </qandaentry> </qandadiv> diff --git a/Doc/Sd1/preliminaries.xml b/Doc/Sd1/preliminaries.xml index 571c0bc357f3e22da4ffd8be58aea80ab645f008..e7320b78a9ef582118da0e355915c26250cd876c 100644 --- a/Doc/Sd1/preliminaries.xml +++ b/Doc/Sd1/preliminaries.xml @@ -6,7 +6,7 @@ xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:db="http://docbook.org/ns/docbook"> - <title>Lecture 1</title> + <title>Preliminaries</title> <section xml:id="sd1CommonRessources"> <title>Common software development related resources</title> @@ -174,7 +174,8 @@ <listitem> <para><link xlink:href="http://wiki.eclipse.org/Eclipse/Installation">Installation</link>, - choose <quote>Eclipse IDE for Java Developers</quote> from <link + choose <quote>Eclipse IDE for Java EE Developers</quote> from + <link xlink:href="http://eclipse.org/downloads">http://eclipse.org/downloads</link>.</para> </listitem> </itemizedlist> diff --git a/Doc/Sd1/steinbruch.xml b/Doc/Sd1/steinbruch.xml index 90c9d3e53ac40f2656c3611192967724c96e3ebf..f0ad7e07325468373db3cc405e77948908dd236d 100644 --- a/Doc/Sd1/steinbruch.xml +++ b/Doc/Sd1/steinbruch.xml @@ -131,4 +131,67 @@ public class CircleAreaCalculator { </qandaentry> </qandadiv> </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sw1QandaExamBonuspoints"> + <title>Exam bonus points</title> + + <qandadiv> + <qandaentry> + <question> + <para>After completely marking an examination a lecturer decides to + globally add a number extra points to a specific task . There is an + upper limit of points for the task in question. We provide an + example:</para> + + <para>On completely fulfilling a task 12 points will be awarded. So + after marking the exam participants' points range from 0 to 12 + points.</para> + + <para>Basically our lecturer wants to add three more points without + breaching the 12 point limit.</para> + + <para>Complete the following code for variable by assigning the + variable <code>newResult</code>. The values of + <code>maximumPoints</code> and <code>pointsToAdd</code> may be + adjusted.</para> + + <programlisting language="java"> public static void main(String[] args) { + + int pointsReached = 1; + int maximumPoints = 12; + int pointsToAdd = 3; + + final int newResult; + + // TODO: Assignment to variable newResult + + System.out.println("New Result:" + newResult); + }</programlisting> + </question> + + <answer> + <para>The basic task is to add up the values of + <code>pointsReached</code> and <code>pointsToAdd</code>. When + exceeding the limit we just assign the limit itself:</para> + + <programlisting language="java"> public static void main(String[] args) { + + int pointsReached = 1; + int maximumPoints = 12; + int pointsToAdd = 3; + + final int newResult; + + if (maximumPoints <= pointsReached + pointsToAdd) { + newResult = maximumPoints; + } else { + newResult = pointsReached + pointsToAdd; + } + + System.out.println("New Result:" + newResult); + }</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> </chapter> diff --git a/Doc/lectures.xml b/Doc/lectures.xml index c4c886c9842605792d549e1319e694aa927b87d2..d0fc71fd3feccedd8d41ba6139d0f6d034b85d91 100644 --- a/Doc/lectures.xml +++ b/Doc/lectures.xml @@ -33,8 +33,6 @@ <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)"/> @@ -48,6 +46,8 @@ <xi:include href="Sd1/streams.xml" xpointer="element(/1)"/> <xi:include href="Sd1/collections.xml" xpointer="element(/1)"/> + + <xi:include href="Sd1/steinbruch.xml" xpointer="element(/1)"/> </part> <part xml:id="sda1">