Commit c93a6f55 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Sd1 Exam winter 2019

parent 498ff0a1
<?xml version="1.0" encoding="UTF-8"?>
<section version="5.0" xml:id="exam_winter2019" 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">
<info>
<title/>
<author>
<personname/>
</author>
<pubdate/>
</info>
<section xml:id="sd1_exam_2019_winter_task1">
<title>Implementing tasks</title>
<section xml:id="sd1_exam_2019_winter_task1_preparation">
<title>Preparations</title>
<para>Download and unzip the above file <filename>exam.zip</filename>
creating a subfolder <filename>Exam</filename> containing a
<productname>Maven</productname> skeleton project. Start your
<productname>IDEA</productname> IDE choosing »Import Project« and
subsequently select the unzipped <filename>Exam/pom.xml</filename>
file.</para>
<para>The <productname>Javadoc</productname> comments describe the
expected behaviour. They are meant to be converted to HTML either by
<link
xlink:href="https://freedocs.mi.hdm-stuttgart.de/__slidesd1GettingStarted.html#/sd1_fig_mavenIdeaExecuteJavadocGoal">Idea
Maven goal execution</link> or by command line using <link
xlink:href="https://freedocs.mi.hdm-stuttgart.de/__slidesd1GettingStarted.html#/sd1_fig_mavenJavadoc"><command>mvn</command>
<option>javadoc:javadoc</option></link>.</para>
</section>
<section xml:id="sd1_exam_2019_winter_task1_description">
<title>Description</title>
<para>Your imported project contains:</para>
<itemizedlist>
<listitem>
<para>Partial implementations of classes and methods.</para>
</listitem>
<listitem>
<para><productname>Javadoc</productname> comments describing the
desired behaviour</para>
</listitem>
<listitem>
<para><productname>Junit</productname> tests for testing the desired
(not yet implemented) behaviour.</para>
</listitem>
</itemizedlist>
</section>
<section xml:id="sd1_exam_2019_winter_task1_tasks">
<title>Tasks</title>
<para>Complete the implementation of classes and methods in both
packages <package>de.hdm_stuttgart.mi.sd1.task1</package> and
<package>de.hdm_stuttgart.mi.sd1.task2</package>. Your project's
<filename>test</filename> branch contains corresponding
<productname>Junit</productname> tests for each class / method to be
implemented.</para>
</section>
<section xml:id="sd1_exam_2019_winter_task1_hints">
<title>Hints</title>
<itemizedlist>
<listitem>
<para>Your score solely depends on the number of successfully
executing unit tests. A »nearly correct« implementation failing with
respect to a given unit tests will contribute no points at
all.</para>
<para>General advice: Implement less but correctly.</para>
</listitem>
<listitem>
<para>Mind special cases <abbrev>i.e.</abbrev> <code>null</code>
variable values or <code>null</code> values being contained in
arrays.</para>
</listitem>
<listitem>
<para>In case of test failures both the
<productname>IDEA</productname> debugger and logging statements are
your friend.</para>
</listitem>
<listitem>
<para>Executing
<package>de.hdm_stuttgart.mi.sd1.test</package>.<classname>ShowReachedPoints</classname>
in your project's <filename>test</filename> branch as a
<productname>Java</productname> application (not as
<productname>Junit</productname> test!) shows your number of points
reached so far.</para>
</listitem>
</itemizedlist>
</section>
<section version="5.1" xml:id="sd1_exam_2019_winter_uploadFirst">
<title>Project upload</title>
<para>Export your project by hitting »File« --&gt; »Export to Zip File«
in IDEA creating an <abbrev>e.g.</abbrev>
<filename>solution-1.zip</filename> file. Hit the <quote> choose
file</quote> button in your <productname>ILIAS</productname> browser tab
and select <filename>solution-1.zip</filename>. Subsequently click
<quote>upload</quote>. Do not forget to advance to the next question for
actually saving your upload. Common pitfalls:</para>
<itemizedlist>
<listitem>
<para>Do not select the wrong archive! In particular avoid choosing
the original <filename>exam.zip</filename> skeleton.</para>
</listitem>
<listitem>
<para>After uploading check for <filename>solution_1.zip</filename>
being visible in the examination system.</para>
</listitem>
<listitem>
<para>You may upload multiple versions i.e.
<filename>solution_2.zip</filename> etc. . Only your least uploaded
archive will become subject to marking.</para>
</listitem>
</itemizedlist>
</section>
<section xml:id="sd1_exam_2019_winter_examCaveats">
<title>Caveats</title>
<itemizedlist>
<listitem>
<para>When approaching examination ending time check your input for
completeness.</para>
</listitem>
<listitem>
<para>Projects residing just on your local workstation cannot be
recovered after exam termination.</para>
</listitem>
</itemizedlist>
</section>
<section xml:id="sd1_exam_2019_winter_solve1">
<title>Solution</title>
<itemizedlist>
<listitem>
<para>Maven project containing <filename
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/blob/master/Klausuren/Sd1/2019winter/Solve/pom.xml">pom.xml</filename>.</para>
</listitem>
</itemizedlist>
</section>
</section>
<section xml:id="sd1_exam_2019_winter_task2">
<title>Unexpected value</title>
<para>We consider:</para>
<programlisting language="java">double result = 3 / 5;</programlisting>
<para>Why does our variable <code>result</code> shows a value of 0.0
?</para>
</section>
<section xml:id="sd1_exam_2019_winter_task3">
<title>Assignment problems</title>
<para>We consider:</para>
<informaltable border="1">
<colgroup width="22%"/>
<colgroup width="78%"/>
<tr>
<th>Code</th>
<th>Result</th>
</tr>
<tr>
<td valign="top"><programlisting language="java"><emphasis role="red">final</emphasis> int i = 33;
byte b = i;</programlisting></td>
<td valign="top"><screen>Compiles flawlessly.</screen></td>
</tr>
<tr>
<td valign="top"><programlisting language="java">int i = 33;
byte b = i;</programlisting></td>
<td valign="top"><screen>Compile time Error:
<emphasis role="red">Incompatible types.
Required: byte
Found: int</emphasis></screen></td>
</tr>
</informaltable>
<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>
<section xml:id="sd1_exam_2019_winter_task4">
<title>NullPointerException problem</title>
<para>Consider the following method inside a class
<package>mi</package><code>.</code><classname>NpeString</classname>:</para>
<informaltable border="1">
<colgroup width="5%"/>
<colgroup width="95%"/>
<tr>
<td><programlisting language="none">13
14
15
16
17
18
19
20
21
22
23
24
25
<emphasis role="red">26</emphasis>
27
28
29</programlisting></td>
<td><programlisting language="java" linenumbering="numbered"
startinglinenumber="13">/**
* &lt;p&gt;Calculate the sum of lengths of all strings belonging to a given array.&lt;/p&gt;
*
* &lt;p&gt;Example: The array {"a", "string"} consists of two strings having lengths 1 and 6 respectively. The
* overall sum of lenghts is thus 1 + 6 == 7.&lt;/p&gt;
*
* @param strings An array of string or null values.
* @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) {
int length = 0;
for (final String s: strings) {
<emphasis role="red">length += s.length();</emphasis>
}
return length;
}</programlisting></td>
</tr>
</informaltable>
<para>We observe the following run-time error at line <emphasis
role="red">26</emphasis>:</para>
<screen>Exception in thread "main" java.lang.NullPointerException
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>
</section>
</section>
......@@ -3081,6 +3081,9 @@ Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.372 sec</scree
ns:idfixup="auto"/>
<xi:include href="Exam/2019/Summer/exam.xml" xpointer="element(/1)"
ns:idfixup="auto"/>
<xi:include href="Exam/2019/Winter/tasks.xml" xpointer="element(/1)"
ns:idfixup="auto"/>
</section>
</appendix>
/.classpath
/.project
/.settings
/.idea
/*.iml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hdm-stuttgart.mi.sd1</groupId>
<artifactId>sd1_2019winter_exam</artifactId>
<version>0.9</version>
<packaging>jar</packaging>
<name>sd1_2019winter_exam</name>
<url>https://freedocs.mi.hdm-stuttgart.de/sd1_sect_mavenCli.html</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>hdm-mi-internal-maven-repo</id>
<url>https://maven.mi.hdm-stuttgart.de/nexus/repository/mi-maven</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>de.hdm_stuttgart.mi.exam</groupId>
<artifactId>unitmarking</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<javaApiLinks>
<property>
<name>api_11</name>
<value>https://klausur.mi.hdm-stuttgart.de/doc/openjdk-11-doc/api/</value>
</property>
</javaApiLinks>
<stylesheetfile>localstyles.css</stylesheetfile>
<additionalJOptions>
<additionalJOption>-html5</additionalJOption>
</additionalJOptions>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>de.hdm_stuttgart.mi.sd1.test.ShowReachedPoints</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>fat-tests</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory/>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory/>
<includes>
<include>**/*.class</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/classes</directory>
<outputDirectory/>
<includes>
<include>**/*.class</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>
\ No newline at end of file
package de.hdm_stuttgart.mi.sd1.sample;
import de.hdm_stuttgart.mi.sd1.task2.Direction;
import de.hdm_stuttgart.mi.sd1.task2.Position;
public class CharacterMoves {
public static void main(String[] args) {
/* Code may work after completing implementation of class Position
final Position jim =
new Position(3,2); // At (3|2)
System.out.println("Start: " + jim); // Output »Start: (3|2)«
jim.move(Direction.S); // Moving to (3|1)
jim.move(Direction.SW); // Moving to (2|0)
System.out.println(jim); // Output: »(2|0)«
System.out.println(
"Result moving SE: " + // Fails: already at
jim.move(Direction.SE)); // grid's lower edge
System.out.println(jim); // Unchanged position (2|0)
*/
}
}
package de.hdm_stuttgart.mi.sd1.task1;
public class _1_OddValues {
/**
* <p>Calculate the sum of <em>odd</em> values only. Examples:</p>
*
* <ul>
* <li>a = 3, b = 5: Both values are odd, thus the result is 3 + 5 == 8.</li>
* <li>a = -4, b = 8: Both values are even resulting in 0.</li>
* <li>a = 7, b = 2: Only <code>a</code> is odd resulting in 7.</li>
* </ul>
*
* @param a First value
* @param b Second value
* @return The sum of all odd arguments, 0 if both arguments are even.
*/
static public int sumOfOdd(int a, int b) {
return -22; // TODO: Implement me correctly
}
private _1_OddValues(){/* Ignore me: My presence suppresses an otherwise visible default constructor's Javadoc */}
}
package de.hdm_stuttgart.mi.sd1.task1;
import de.hdm_stuttgart.mi.sd1.task1.extra.Day;
public class _2_Purchase {
/**
* <p>Calculate total cost of products depending on day of week.</p>
*
* <p>A shop in a holiday region sells products seven days a week. At weekends an additional service fee is being
* added to each purchase:</p>
*
* <ul>
* <li>0 €-cent on monday till friday.</li>
* <li>100 €-cent on saturday / {@link Day#SATURDAY}.</li>
* <li>200 €-cent on sunday / {@link Day#SUNDAY}.</li>
* </ul>
*
* <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>
* <li>(810 + 200) == 1010 €-cent on sundays.</li>
* </ul>
*
* @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) {
return -123; // TODO: Implement me correctly
}
private _2_Purchase(){/* Ignore me: My presence suppresses an otherwise visible default constructor's Javadoc */}
}
package de.hdm_stuttgart.mi.sd1.task1;
/**
* Computing the closest <code>byte</code> value to the maximum of two <code>int</code> values.
*/
public class _3_ByteMax {
/**
* <p>Get the closest <code>byte</code> value to the maximum of two <code>int</code> values.</p>
*
* <p>Values of type <code>byte</code> are limited to the range [{@link Byte#MIN_VALUE}, {@link Byte#MAX_VALUE}].
* The maximum of two <code>int</code> values may thus be larger or smaller than any possible <code>byte</code>
* value. If a maximum value of type <code>int</code> does not fit into this range we return the closest possible
* value. Examples:</p>
*
* <table class="goikTableDefaults" style="text-align: right;">
* <tr>
* <th>a</th>
* <th>b</th>
* <th>Maximum</th>
* <th>Closest <code>byte</code> value</th>
* <th style="text-align: center;">Remark</th>
* </tr>
* <tr>
* <td >-5</th>
* <td>60</th>
* <td>60</th>
* <td>60</th>
* <td style="text-align: left;">Maximum is in <code>byte</code> range.</th>
* </tr>
* <tr>
* <td>100</th>
* <td>200</th>
* <td>200</th>
* <td>127</th>
* <td style="text-align: left;">An <code>int</code> value of 200 is larger than the largest possible
* <code>byte</code> value of 127. Thus 127 is the best possible <code>byte</code> value
* approximation.</th>
* </tr>
* <tr>
* <td>-300</th>
* <td>-500</th>
* <td>-300</th>
* <td>-128</th>
* <td style="text-align: left;">An <code>int</code> value of -300 is smaller than the smallest
* <code>byte</code> value -128. Thus -128 provides the best possible <code>byte</code> value
* approximation.</th>
* </tr>
* </table>
*
* @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) {
return -120; // TODO: Implement me correctly
}
private _3_ByteMax(){/* Ignore me: My presence suppresses an otherwise visible default constructor's Javadoc */}
}
package de.hdm_stuttgart.mi.sd1.task1;
/**
* Integer array helper.
*/
public class _4_ArrayOrder {
/**
* <p>Check whether a list of values appears in ascending order.</p>
*
* <p>An array is said to be in ascending order if each element's »right« neighbour is greater than or equal to the
* element itself. Examples:</p>
*
* <table class="goikTableDefaults" style="text-align: right;">
* <tr>
* <th>Values</th>
* <th>Result</th>
* </tr>
*
* <tr>
* <td>{1, 3, 7, 11}</td>
* <td>Ascending</td>
* </tr>
*
* <tr>
* <td>{1, 6, 6, 9}</td>
* <td>Ascending</td>
* </tr>
*
* <tr>
* <td>{4, 3, 6}</td>
* <td>Not ascending since 3 &lt; 4</td>
* </tr>
*
* </table>
*
* <p>An array containing less than two elements is considered to be ordered by definition.</p>
*
* @param values A list of integer values
* @return <code>true</code> if the array contains less than two elements or all values appear in ascending order,
* <code>false</code> otherwise.
*/
static public boolean isInAscendingOrder(final int [] values) {
return true; // TODO: Implement me correctly
}
private _4_ArrayOrder(){/* Ignore me: My presence suppresses an otherwise visible default constructor's Javadoc */}
}
package de.hdm_stuttgart.mi.sd1.task1;
/**
* Scrabble game scores.
*
* <object data="doc-files/scrabble.jpg" type="image/jpg" ></object>
*
*/
public class _5_Scrabble {
/**
* <p>Compute the Scrabble score of a given word.</p>
*
* <p>The Scrabble game awards scores for words using the usual the A-Z alphabet. Each character receives a case
* independent score:</p>
*
* <table class="goikTableDefaults">
* <tr>
* <th>Letters</th>
* <th>Score</th>