Skip to content
Snippets Groups Projects
Commit 057d3a31 authored by Goik Martin's avatar Goik Martin
Browse files

Better code error explanation

parent 151c46fd
No related branches found
No related tags found
No related merge requests found
......@@ -107,7 +107,15 @@
<answer>
<para>There are errors:</para>
<programlisting language="java">/**
<informaltable border="1">
<tr>
<th>Erroneous code</th>
<th>Correct code</th>
</tr>
<tr>
<td valign="top"><programlisting language="java">/**
* Summing up an int array's values
*
* @param values An array of int values
......@@ -116,35 +124,54 @@
public static int sum(int[] values) {
int sum; <co linkends="sd1_exam_2023_winter_sumErrors-1"
xml:id="sd1_exam_2023_winter_sumErrors-1-co"/>
xml:id="sd1_exam_2023_winter_sumErrors-1-co"/>
for (<co linkends="sd1_exam_2023_winter_sumErrors-2"
xml:id="sd1_exam_2023_winter_sumErrors-2-co"/> i = 0; i &lt;=<co
linkends="sd1_exam_2023_winter_sumErrors-3"
xml:id="sd1_exam_2023_winter_sumErrors-3-co"/> values.length; i++) {
xml:id="sd1_exam_2023_winter_sumErrors-2-co"/> i = 0; i &lt;=<co
linkends="sd1_exam_2023_winter_sumErrors-3"
xml:id="sd1_exam_2023_winter_sumErrors-3-co"/> values.length; i++) {
sum += values[i];
}
return sum;
}</programlisting></td>
<td valign="top"><programlisting language="java">/**
* Summing up an int array's values
*
* @param values An array of int values
* @return The sum of all array values
*/
public static int sum(int[] values) {
int sum = 0;
for (int i = 0; i &lt; values.length; i++) {
sum += values[i];
}
return sum;
}</programlisting>
}</programlisting></td>
</tr>
</informaltable>
<calloutlist>
<callout arearefs="sd1_exam_2023_winter_sumErrors-1-co"
xml:id="sd1_exam_2023_winter_sumErrors-1">
<para>Missing initialization <code language="java">int sum =
<para>Missing initialization <code language="java">sum =
0</code>.</para>
</callout>
<callout arearefs="sd1_exam_2023_winter_sumErrors-2-co"
xml:id="sd1_exam_2023_winter_sumErrors-2">
<para>Missing type i.e. <code language="java">int i =
0</code>;</para>
<para>Missing type i.e. <code language="java">int
i</code>.</para>
</callout>
<callout arearefs="sd1_exam_2023_winter_sumErrors-3-co"
xml:id="sd1_exam_2023_winter_sumErrors-3">
<para>Off by one error: Last array index being <code
language="java">values.length - 1</code> requires <code
language="java">i &lt; values.length</code>.</para>
language="java">i &lt; values.length</code> avoiding an
<classname>ArrayIndexOutOfBoundsException</classname>.</para>
</callout>
</calloutlist>
</answer>
......@@ -174,33 +201,52 @@ public static int sum(int[] values) {
</question>
<answer>
<para>Every expression of type byte is bounded by <code
language="java">Byte.MAX_VALUE</code>. The expression <code
language="java">b &lt;= Byte.MAX_VALUE</code> is thus always
<para>Every variable of type byte is bounded by <code
language="java">Byte.MAX_VALUE</code>. The boolean expression
<code language="java">b &lt;= Byte.MAX_VALUE</code> is thus always
true.</para>
<para>A closer look on the operator ++ acting on a byte variable
reveals a cycle passing between <code
language="java">Byte.MIN_VALUE</code> and <code
language="java">Byte.MAX_VALUE</code>:</para>
<para>An even closer look reveals <code language="java">b++</code>
oscillating between its lower <code
language="java">Byte.MIN_VALUE</code> and upper <code
language="java">Byte.MAX_VALUE</code> bound:</para>
<informaltable border="1">
<colgroup width="36%"/>
<colgroup width="12%"/>
<colgroup width="52%"/>
<tr>
<th>Code</th>
<th>Output</th>
<th>Explanation</th>
</tr>
<tr>
<td valign="top"><programlisting language="java">byte b = Byte.MAX_VALUE - 1;
System.out.println(b++);
System.out.println(b++);
System.out.println(b++);
...
System.out.println(b++);</programlisting></td>
<td valign="top"><screen> 126
127
<emphasis role="red">127</emphasis>
-128
-127</screen></td>
-127
...
<emphasis role="red">127</emphasis></screen></td>
<td valign="top"><screen>
Byte.MAX_VALUE
Byte.MIN_VALUE
Byte.MAX_VALUE</screen></td>
</tr>
</informaltable>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment