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 @@ ...@@ -107,7 +107,15 @@
<answer> <answer>
<para>There are errors:</para> <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 * Summing up an int array's values
* *
* @param values An array of int values * @param values An array of int values
...@@ -116,35 +124,54 @@ ...@@ -116,35 +124,54 @@
public static int sum(int[] values) { public static int sum(int[] values) {
int sum; <co linkends="sd1_exam_2023_winter_sumErrors-1" 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" for (<co linkends="sd1_exam_2023_winter_sumErrors-2"
xml:id="sd1_exam_2023_winter_sumErrors-2-co"/> i = 0; i &lt;=<co xml:id="sd1_exam_2023_winter_sumErrors-2-co"/> i = 0; i &lt;=<co
linkends="sd1_exam_2023_winter_sumErrors-3" 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-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]; sum += values[i];
} }
return sum; return sum;
}</programlisting> }</programlisting></td>
</tr>
</informaltable>
<calloutlist> <calloutlist>
<callout arearefs="sd1_exam_2023_winter_sumErrors-1-co" <callout arearefs="sd1_exam_2023_winter_sumErrors-1-co"
xml:id="sd1_exam_2023_winter_sumErrors-1"> 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> 0</code>.</para>
</callout> </callout>
<callout arearefs="sd1_exam_2023_winter_sumErrors-2-co" <callout arearefs="sd1_exam_2023_winter_sumErrors-2-co"
xml:id="sd1_exam_2023_winter_sumErrors-2"> xml:id="sd1_exam_2023_winter_sumErrors-2">
<para>Missing type i.e. <code language="java">int i = <para>Missing type i.e. <code language="java">int
0</code>;</para> i</code>.</para>
</callout> </callout>
<callout arearefs="sd1_exam_2023_winter_sumErrors-3-co" <callout arearefs="sd1_exam_2023_winter_sumErrors-3-co"
xml:id="sd1_exam_2023_winter_sumErrors-3"> xml:id="sd1_exam_2023_winter_sumErrors-3">
<para>Off by one error: Last array index being <code <para>Off by one error: Last array index being <code
language="java">values.length - 1</code> requires <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> </callout>
</calloutlist> </calloutlist>
</answer> </answer>
...@@ -174,33 +201,52 @@ public static int sum(int[] values) { ...@@ -174,33 +201,52 @@ public static int sum(int[] values) {
</question> </question>
<answer> <answer>
<para>Every expression of type byte is bounded by <code <para>Every variable of type byte is bounded by <code
language="java">Byte.MAX_VALUE</code>. The expression <code language="java">Byte.MAX_VALUE</code>. The boolean expression
language="java">b &lt;= Byte.MAX_VALUE</code> is thus always <code language="java">b &lt;= Byte.MAX_VALUE</code> is thus always
true.</para> true.</para>
<para>A closer look on the operator ++ acting on a byte variable <para>An even closer look reveals <code language="java">b++</code>
reveals a cycle passing between <code oscillating between its lower <code
language="java">Byte.MIN_VALUE</code> and <code language="java">Byte.MIN_VALUE</code> and upper <code
language="java">Byte.MAX_VALUE</code>:</para> language="java">Byte.MAX_VALUE</code> bound:</para>
<informaltable border="1"> <informaltable border="1">
<colgroup width="36%"/>
<colgroup width="12%"/>
<colgroup width="52%"/>
<tr> <tr>
<th>Code</th> <th>Code</th>
<th>Output</th> <th>Output</th>
<th>Explanation</th>
</tr> </tr>
<tr> <tr>
<td valign="top"><programlisting language="java">byte b = Byte.MAX_VALUE - 1; <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++);
System.out.println(b++);
...
System.out.println(b++);</programlisting></td> System.out.println(b++);</programlisting></td>
<td valign="top"><screen> 126 <td valign="top"><screen> 126
127 <emphasis role="red">127</emphasis>
-128 -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> </tr>
</informaltable> </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