<?xml version="1.0" encoding="UTF-8"?> <section version="5.0" xml:id="sd1_exam_2024_winter" xml:lang="en" 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" xmlns:trans="http://docbook.org/ns/transclusion" 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>SD1 examination winter 2024</title> <section xml:id="sd1_exam_2024_winter_task1"> <title>Implementing tasks</title> <section xml:id="sd1_exam_2024_winter_task1_preparation"> <title>Preparation</title> <orderedlist> <listitem> <para>Download and unzip above <filename>exam.zip</filename>. You should see a directory »<filename>Exam</filename>« containing a <filename>pom.xml</filename> file.</para> </listitem> <listitem> <para>Open this project in your <productname>IDEA</productname> IDE by selecting the <filename>Exam/pom.xml</filename> file as a project.</para> </listitem> </orderedlist> </section> <section xml:id="sd1_exam_2024_winter_task1_task"> <title>Task</title> <qandaset defaultlabel="qanda" xml:id="sd1_exam_2024_winter_task1Qanda"> <qandadiv> <qandaentry> <question> <para>Open the <filename>Readme.md</filename> file in your project's root. It contains all necessary instructions for solving the implementation tasks.</para> </question> <answer> <para>A simple solution will implement the matches(...) method:</para> <programlisting language="java">private boolean matches(Month month, int day) { return fromMonth == month && fromDay <= day || toMonth == month && day <= toDay; }</programlisting> <para>This method will return e.g. <code>Aries</code> for <code language="java">month == APR</code> and <code language="java">day == 7</code>. We are now able providing a first solution :</para> <programlisting language="java">static public Zodiac getZodiac(final Month month, final int day) { for (Zodiac zodiac : Zodiac.values()) { // Looping over all zodiacs if (zodiac.matches(month, day)) { // Exactly one zodiac must match mounth and day return zodiac; } } return null; // Unreachable for correct month and day values but required for keeping the compiler happy }</programlisting> <para>Apart from requiring the ugly <code language="java">return null</code> statement this approach is also ineffective due to possibly looping over all 12 zodiacs before finding the desired match. A better approach creates a two-dimensional array <code>private static final Zodiac[][] zodiacByMonthByDay</code> of zodiacs beforehand using <code>month.ordinal()</code> and <code>day</code> as index values:</para> <screen> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 JAN: 0 Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu FEP: 1 Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis MAR: 2 Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari APR: 3 Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau MAY: 4 Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem JUN: 5 Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Can Can Can Can Can Can Can Can Can JUL: 6 Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Leo Leo Leo Leo Leo Leo Leo Leo Leo AUG: 7 Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Vir Vir Vir Vir Vir Vir Vir Vir Vir SEP: 8 Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Lib Lib Lib Lib Lib Lib Lib Lib OCT: 9 Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Sco Sco Sco Sco Sco Sco Sco Sco NOV: 10 Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sag Sag Sag Sag Sag Sag Sag Sag Sag DEC: 11 Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap</screen> <para>This table uses shortcuts <abbrev>e.g.</abbrev> <abbrev>Aqu</abbrev> denoting <code>Aquarius</code>. Note the variable month lengths in the second (day) dimension varying from 29 days for February to 31.</para> <para>For exact implementation details see <link xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/-/tree/master/Klausuren/Sd1/2024winter/Solve">winter 2024 exam</link>. The approach uses a so called <link xlink:href="https://www.baeldung.com/java-static-instance-initializer-blocks">static initializer block</link>:</para> <programlisting language="java">public enum Zodiac { ... static { // First array dimension: 12 months zodiacByMonthByDay = new Zodiac[Month.values().length][]; ... } }</programlisting> <para>static blocks get processed before starting <code>main(...)</code>. This guarantees our array being initialized before being accessed. Our final solution now simply reads:</para> <programlisting language="java">static public Zodiac getZodiac(final Month month, final int day) { return zodiacByMonthByDay[month.ordinal()][day - 1]; }</programlisting> </answer> </qandaentry> </qandadiv> </qandaset> </section> <section xml:id="sd1_exam_2024_winter_task1Warning"> <title>Warning</title> <itemizedlist> <listitem> <para>When approaching end of examination check your input for completeness prior to being automatically logged out by the system. Remember: There is 120 minutes for the examination and another 5 minutes to check for completeness.</para> </listitem> <listitem> <para>Projects residing just on your local workstation's file system cannot be recovered after finishing the exam.</para> </listitem> </itemizedlist> </section> </section> <section xml:id="sd1_exam_2024_winter_task2"> <title>Swapping two variables</title> <qandaset defaultlabel="qanda" xml:id="sd1_exam_2024_winter_task3Qanda"> <qandadiv> <qandaentry> <question> <para>We consider:</para> <informaltable border="1"> <colgroup width="9%"/> <colgroup width="91%"/> <tr> <th>Code</th> <td valign="top"><programlisting language="java">public static void main(String[] args) { int a = 1, b = 2; System.out.println("a = " + a + ", b = " + b); { // Swapping values of a and b final int bCopy = b; b = a; a = bCopy; } System.out.println("a = " + a + ", b = " + b); }</programlisting></td> </tr> <tr> <th>Result</th> <td valign="top"><screen>a = 1, b = 2 a = 2, b = 1</screen></td> </tr> </informaltable> <para>The values of <code>a</code> and <code>b</code> are being swapped within the block as expected. We now replace this block by a class method <methodname>swap(int a, int b){...}</methodname>:</para> <informaltable border="1"> <colgroup width="9%"/> <colgroup width="91%"/> <tr> <th>Code</th> <td valign="top"><programlisting language="java">public static void main(String[] args) { int a = 1, b = 2; System.out.println("a = " + a + ", b = " + b); swap(a, b); System.out.println("a = " + a + ", b = " + b); } /** * Swapping values of two variables * @param a First variable * @param b Second variable */ static void swap(int a, int b) { final int bCopy = b; b = a; a = bCopy; }</programlisting></td> </tr> <tr> <th>Result</th> <td valign="top"><screen>a = 1, b = 2 a = 1, b = 2</screen></td> </tr> </informaltable> <para>This time the values of a and b in <methodname>main(...)</methodname> remain unchanged. Why is this not at all surprising?</para> </question> <answer> <para>For the sake of easy variable identification by name we re-factor all tree variables in <methodname>swap(...) by {a->x, b->y, bCopy->yCopy}</methodname>:</para> <programlisting language="java">public static void main(String[] args) { int a = 1, b = 2; System.out.println("a = " + a + ", b = " + b); swap(a, b); System.out.println("a = " + a + ", b = " + b); } /** * Swapping values of two variables * @param x First variable * @param y Second variable */ static void swap(int x, int y) { int yCopy = y; y = x; x = yCopy; }</programlisting> <para>This re-factoring has no effect on execution since both sets of variables {<varname>a</varname>, <varname>b</varname>, <varname>bcopy</varname>} or {<varname>x</varname>, <varname>y</varname>, <varname>yCopy</varname>} are local to our <methodname>swap(...)</methodname> method.</para> <para>Calling <methodname>swap(a, b)</methodname> in <methodname>main(...)</methodname> <emphasis>copies</emphasis> the values of <varname>a</varname> and <varname>b</varname> into <varname>x</varname> and <varname>y</varname> respectively. See <xref linkend="sd1_fig_callByValueDetails"/> showing a <acronym>call-by-value</acronym> example being the only way passing parameters in a Java method call. Hence swapping these copied <varname>x</varname> and <varname>y</varname> values in <methodname>swap(...)</methodname>'s stack frame has no effect on <methodname>main(...)</methodname>.</para> </answer> </qandaentry> </qandadiv> </qandaset> </section> <section xml:id="sd1_exam_2024_winter_task3"> <title>0 and null</title> <qandaset defaultlabel="qanda" xml:id="sd1_exam_2024_winter_task2Qanda"> <qandadiv> <qandaentry> <question> <para>Consider the following two code snippets:</para> <itemizedlist> <listitem> <para><code language="java">if (0 == someVariable) {/* Code omitted for brevity */}</code></para> </listitem> <listitem> <para><code language="java">if (null == someOtherVariable) {/* Code omitted for brevity */}</code></para> </listitem> </itemizedlist> <para>Answer the following two questions:</para> <orderedlist> <listitem> <para>What is the difference between <code>0</code> and <code>null</code>?</para> </listitem> <listitem> <para>Assuming the above snippets are part of well compiling Java code: What do we know about the possible data types of <code>someVariable</code> and <code>someOtherVariable</code> ?</para> </listitem> </orderedlist> </question> <answer> <orderedlist> <listitem> <itemizedlist> <listitem> <para>0 is a literal denoting the value zero of type <code>int</code>.</para> </listitem> <listitem> <para>A variable of class or array type of value <code language="java">null</code> does not reference an object.</para> </listitem> </itemizedlist> </listitem> <listitem> <itemizedlist> <listitem> <para><code language="java">someVariable</code> must be of type <code language="java">byte</code>, <code language="java">short</code>, <code language="java">int</code>, <code language="java">long</code>, <code language="java">char</code>, <code language="java">float</code> or <code language="java">double</code>. In other words: Any primitive type except <code language="java">boolean</code>.</para> </listitem> <listitem> <para><code language="java">someOtherVariable</code> must be of either class / <code>enum</code> / <code>record</code> or array type.</para> </listitem> </itemizedlist> </listitem> </orderedlist> </answer> </qandaentry> </qandadiv> </qandaset> </section> </section>