diff --git a/Doc/Sd1/LanguageFundamentals/languageFundamentals.xml b/Doc/Sd1/LanguageFundamentals/languageFundamentals.xml index 89e528411f48eeb3896a11b4658084fca186fc1c..602fae1a6faa9be093e5c4827e315f374a0b05d2 100644 --- a/Doc/Sd1/LanguageFundamentals/languageFundamentals.xml +++ b/Doc/Sd1/LanguageFundamentals/languageFundamentals.xml @@ -7215,19 +7215,73 @@ age = age + 2; <emphasis role="red">// Error: Incompatible types.</emphasis> age += 200;</programlisting> + <para>So why is <code language="java">age += 2</code> perfectly + legal on contrary to <code language="java">age = age + 2</code> + ?</para> + <tip> - <para>Consider <xref - linkend="sd1_fig_noBinaryPlusByteByte"/>.</para> + <para>Consider <xref linkend="sd1_fig_noBinaryPlusByteByte"/> + and <link + xlink:href="https://docs.oracle.com/javase/specs/jls/se19/html/jls-15.html#jls-15.26.2">Compound + Assignment Operators</link>.</para> </tip> </question> <answer> - <para>According to <xref + <para>The<link + xlink:href="https://docs.oracle.com/javase/specs/jls/se19/html/index.html"> + Java® Language Specification SE 19 Edition</link> offers a + definition in its <link + xlink:href="https://docs.oracle.com/javase/specs/jls/se19/html/jls-15.html#jls-15.26.2">Compound + Assignment Operators</link> section:</para> + + <blockquote> + <para>A compound assignment expression of the form E1 op= E2 + is equivalent to E1 = (T) ((E1) op (E2)), where T is the type + of E1, except that E1 is evaluated only once</para> + </blockquote> + + <para>We provide an example illustrating this rather condensed + statement:</para> + + <programlisting language="none">double d = 4.5; +byte i = 3; + + <emphasis role="red">i</emphasis> <emphasis role="red">+</emphasis>= <emphasis + role="red">d</emphasis> ; +// <emphasis role="red">E1</emphasis> <emphasis role="red">op</emphasis>= <emphasis + role="red">E2</emphasis></programlisting> + + <para>We thus link:</para> + + <itemizedlist> + <listitem> + <para><code>i</code> to <code>E1</code></para> + </listitem> + + <listitem> + <para>the <code>+</code> operator to <code>op</code></para> + </listitem> + + <listitem> + <para><code>d</code> to <code>E2</code></para> + </listitem> + </itemizedlist> + + <para> Our variable <code language="java">i</code> aka + <code>E1</code> is of type <code language="java">int</code>. T + being <code>E1</code>'s type the expression<code> E1 = (T) ((E1) + op (E2))</code> hence translates to:</para> + + <programlisting language="java">i = (int)(i + d);</programlisting> + + <para>Back to our original example: According to <xref linkend="sd1_fig_noBinaryPlusByteByte"/> the arithmetic <code language="java">+</code> operator acting any two non-long values will always return a result of type <code language="java">int</code>. The expression <code - language="java">age + 2</code> is thus of type <code + language="java">age + 2</code> (The literal 2 is of type <code + language="java">int</code> anyway) is thus of type <code language="java">int</code> and therefore requires a cast when being assigned to our variable <code language="java">age</code> of type <code language="java">byte</code>:</para> @@ -7241,7 +7295,7 @@ age = (byte)(age + 2);</programlisting> defined as <code language="java">final</code> the value of the expression <code language="java">age + 2</code> is not being known at compile time. Adding <code - language="java">final</code> allows for compile time + language="java">final</code> would allow for compile time evaluation and thus assignment to a second variable of type <code language="java">byte</code> without requiring a cast:</para> @@ -7251,10 +7305,9 @@ age = (byte)(age + 2);</programlisting> byte gettingOlder = age + 2; // o.K. without cast: age + 2 can be evaluated at compile time.</programlisting> </note> - <para>On contrary the operator <code language="java">+=</code> - will accept any right hand integer value (even of type <code - language="java">long</code>!) thereby possibly cycling through - the range of byte values e.g.:</para> + <para>On contrary regarding the <code language="java">age += + 2</code> expression the equivalent is age = (byte)(age + + 2):</para> <informaltable border="1"> <tr> diff --git a/Klausuren/Sd1/2021summer/Exam/src/main/java/de/hdm_stuttgart/mi/sd1/task2/LengthUnitCalculator.java b/Klausuren/Sd1/2021summer/Exam/src/main/java/de/hdm_stuttgart/mi/sd1/task2/LengthUnitCalculator.java index 2654b2b5e5555dda0f7e0939efcc85c37f015584..74edb4f15734745a4b37dee283210ffa0a46c2c1 100644 --- a/Klausuren/Sd1/2021summer/Exam/src/main/java/de/hdm_stuttgart/mi/sd1/task2/LengthUnitCalculator.java +++ b/Klausuren/Sd1/2021summer/Exam/src/main/java/de/hdm_stuttgart/mi/sd1/task2/LengthUnitCalculator.java @@ -113,5 +113,20 @@ package de.hdm_stuttgart.mi.sd1.task2; * </section> */ public class LengthUnitCalculator { - // TODO: implement me + + double length; + Unit unit = null; + void setLength(final double length) { + this.length = length; + } + + void setUnit(final Unit unit ) { + if (null != unit) { + + } + + this.unit = unit; + } + + } diff --git a/Klausuren/Sd1/2021summer/Exam/src/main/java/de/hdm_stuttgart/mi/sd1/task2/Unit.java b/Klausuren/Sd1/2021summer/Exam/src/main/java/de/hdm_stuttgart/mi/sd1/task2/Unit.java index d9be29c19886457567190eb4aa60a01aa4cfe9cd..f21b5af7d8616c56e873d7db139866c6a474cd53 100644 --- a/Klausuren/Sd1/2021summer/Exam/src/main/java/de/hdm_stuttgart/mi/sd1/task2/Unit.java +++ b/Klausuren/Sd1/2021summer/Exam/src/main/java/de/hdm_stuttgart/mi/sd1/task2/Unit.java @@ -7,18 +7,25 @@ public enum Unit { /** * <p>SI metric length 1 meter (m)</p> */ - METER, + METER(1.0), /** * <p>Inch: Imperial length defined as 0.0254m</p> */ - INCH, + INCH(0.0254), /** * <p>Imperial mile defined as 1609.344m</p> */ - MILE_IMPERIAL, + MILE_IMPERIAL(1609.344), /** * <p>Nautical mile defined as 1852m.</p> */ - MILE_NAUTICAL + MILE_NAUTICAL(1852.); + + Unit(final double lengthInMeter) { + this.lengthInMeter = lengthInMeter; + } + + + final private double lengthInMeter; }