Commit 9811e200 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Providing 2019 exam winter solutions

parent 77e38d26
......@@ -9,15 +9,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">
<info>
<title/>
<author>
<personname/>
</author>
<pubdate/>
</info>
<title>SD1 examination winter 2019</title>
<section xml:id="sd1_exam_2019_winter_task1">
<title>Implementing tasks</title>
......@@ -177,6 +169,23 @@
<para>Why does our variable <code>result</code> shows a value of 0.0
?</para>
<section xml:id="sd1_exam_2019_winter_task2_solve">
<title>Solution</title>
<para><code language="java">3</code> and <code language="java">5</code>
are both literals of type <code language="java">int</code>. According to
<xref linkend="sd1_fig_genericBinaryTypeExamples"/> the operator
<quote><code language="java">/</code></quote> acting on two <code
language="java">int</code> values evaluates to an <code
language="java">int</code>. The resulting value denotes the number of
times <code language="java">5</code> fits into <code
language="java">3</code> being equal to <code language="java">0</code>
of type <code language="java">int</code>. This value of <code
language="java">0</code> is being assigned to the variable <code
language="java">result</code> of type double using widening resulting in
<code language="java">0.0</code>.</para>
</section>
</section>
<section xml:id="sd1_exam_2019_winter_task3">
......@@ -216,6 +225,27 @@ Found: int</emphasis></screen></td>
<para>Give an explanation: Why does the first snippet compiles flawlessly
but the second second causes a <foreignphrase xml:lang="en">compile
time</foreignphrase> error ?</para>
<section xml:id="sd1_exam_2019_winter_task3_solve">
<title>Solution</title>
<para>The <code language="java">final</code> modifier prohibits
subsequent assignments to our variable <code language="java">i</code>.
The compiler can thus safely assume its value to remain constant. The
int value of <code language="java">33</code> fits nicely into a <code
language="java">byte</code> variable ranging from -128 to 127. With
respect just to the variable <code language="java">b</code> the first
code snippet is equivalent to:</para>
<programlisting language="java">byte b = 33; // int to byte narrowing by compile time range check</programlisting>
<para>Without the <code language="java">final</code> modifier in general
a more complex compile time analysis would be required. (Standard) <xref
linkend="glo_Java"/> compilers do not implement this. Albeit no
statement being present between the two lines the compiler cannot assume
the variable <code language="java">i</code>'s value to remain unchanged
thus possibly receiving a value outside [-128, 127];</para>
</section>
</section>
<section xml:id="sd1_exam_2019_winter_task4">
......@@ -259,7 +289,7 @@ Found: int</emphasis></screen></td>
* @return The sum of all string's length values. &lt;code&gt;null&lt;/code&gt; entries will be treated
* as empty strings.
*/
static int getLength(final String[] strings) {
static int getLength(final String[] strings) {
int length = 0;
for (final String s: strings) {
<emphasis role="red">length += s.length();</emphasis>
......@@ -276,9 +306,46 @@ static int getLength(final String[] strings) {
at mi.NpeString.getLength(<emphasis role="red">NpeString.java:26</emphasis>)
at ...</screen>
<para>What caused this error? Provide a possible sample data array causing
the exception. Explain the implementation error with respect to the
provided <acronym>Javadoc</acronym> promise and propose a solution
resolving the flaw.</para>
<para>What caused this <acronym>NPE</acronym> error? Provide a possible
sample data array causing this exception. Explain the implementation error
with respect to the provided <acronym>Javadoc</acronym> promise and
propose a solution resolving the flaw.</para>
<section xml:id="sd1_exam_2019_winter_task4_solve">
<title>Solution</title>
<para>The <xref linkend="glo_Javadoc"/> contract regarding our method
<methodname>getLength(final String[] strings)</methodname>'s parameter
<code language="java">strings</code> states:</para>
<programlisting language="java">/**
...
* @param strings An array of string or null values.
...</programlisting>
<para> The variable <code language="java">strings</code> may thus
contain <code language="java">null</code> values e.g.:</para>
<programlisting language="java">String[] names = {"Jim", null, "Eve"};
int length = getLength(names); // Will cause a NPE</programlisting>
<para>The local variable <code language="java">s</code> may therefore
become <code language="java">null</code> causing a <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/NullPointerException.html#%3Cinit%3E()">NullPointerException</classname>
when invoking <methodname>s.length()</methodname> requiring an <code
language="java">if</code> - guard filtering <code
language="java">null</code> values beforehand:</para>
<programlisting language="java" linenumbering="numbered"
startinglinenumber="13">static int getLength(final String[] strings) {
int length = 0;
for (final String s: strings) {
<emphasis role="red">if (null != s) {
length += s.length();
}</emphasis>
}
return length;
}</programlisting>
</section>
</section>
</section>
......@@ -16,7 +16,7 @@ public class _2_Purchase {
* <li>200 €-cent on sunday / {@link Day#SUNDAY}.</li>
* </ul>
*
* <p>Example: For a purchase of 810 €-cent (== 8.12€) the total price will be:</p>
* <p>Example: For a purchase of 810 €-cent (== 8.10€) the total price will be:</p>
* <ul>
* <li>810 €-cent during monday till friday.</li>
* <li>(810 + 100) == 910 €-cent on saturdays.</li>
......@@ -26,14 +26,8 @@ public class _2_Purchase {
* @param purchaseCosts The cost following regular product prizes.
* @param day Day of week i.e. monday, tuesday,...
* @return The total cost including possible weekend service fees.
*
* <section class="implementationHints">
* <h3 class="implementationHints">Hint:</h3>
* <p>There are different ways implementing this method. You may extend enum {@link Day} suiting your needs.</p>
* </section>
*/
static public int totalCosts(int purchaseCosts, final Day day) {
// TODO: Remove Day implementation as well!!
return purchaseCosts + day.serviceFee;
}
......
......@@ -50,12 +50,6 @@ public class _3_ByteMax {
* @param a First value
* @param b Second value
* @return the closest <code>byte</code> value to the maximum of both arguments.
*
* <section class="implementationHints">
* <h3 class="implementationHints">Hint:</h3>
* <p>You may need a cast from <code>int</code> to <code>byte</code> i.e.:</p>
* <pre>... = (byte) ...</pre>
* </section>
*/
static public byte getClosestMaximum(final int a, final int b) {
......
......@@ -55,17 +55,6 @@ public class _5_Scrabble {
*
* @param word A string or null
* @return The word's score. Zero for empty string or <code>null</code>.
*
* <section class="implementationHints">
* <h3 class="implementationHints">Helpful classes and methods:</h3>
*
* <ul>
* <li>{@link String#toCharArray()} for splitting a word into its characters</li>
* <li>{@link String#toUpperCase()} to normalize e.g. <code>"Cyphertext"</code> to
* <code>"CYPHERTEXT"</code>.</li>
* </ul>
*
* </section>
*/
static public int getScore(final String word) {
if (null == word) {
......
package de.hdm_stuttgart.mi.sd1.task2;
/**
* <p>Directions as in
* <a href="https://freedocs.mi.hdm-stuttgart.de/sd1_enum_sect_dayAsEnumType.html#sd1_qanda_enumDirections"
......
......@@ -69,10 +69,6 @@ import de.hdm_stuttgart.mi.sd1.task1.extra.Day;
* </tr>
* </table>
*
* <section class="implementationHints">
* <h3 class="implementationHints">Hint:</h3>
* <p>Consider implementing <code>toString()</code>.</p>
* </section>
*/
public class Position {
......
/**
* Defining positions of characters in a computer game.
*
* <p>Hints:</p>
*
* <ul>
* <li>Run <code>mvn javadoc:javadoc</code> and open the generated index.html in your browser of choice.</li>
*
* <li>Use the corresponding unit tests to check your implementation's consistency and class
* <code>de.hdm_stuttgart.mi.sd1.test.ShowReachedPoints</code> from your project's “unit test” branch.</li>
*
* </ul>
*
*/
package de.hdm_stuttgart.mi.sd1.task2;
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment