From 81702e5c252dbe483e26f2e6a70ad2a39aca5b01 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Fri, 12 Jan 2018 14:56:50 +0100 Subject: [PATCH] Deleting Car jump exercises, new String mask exercise --- Doc/Common/snippets.xml | 10 + Doc/Sd1/Ref/Fig/carSlope.fig | 72 -- Doc/Sd1/Ref/Fig/velocityDecompose.fig | 40 - Doc/Sd1/coreClasses.xml | 467 ++++++--- Doc/Sd1/objectsClasses.xml | 959 +++++------------- P/Sd1/CarJump/V1/.gitignore | 4 - P/Sd1/CarJump/V1/pom.xml | 48 - .../hdm_stuttgart/mi/sd1/carjump/Driver.java | 19 - .../mi/sd1/carjump/model/CarJump.java | 40 - .../CarJump/V1/src/main/resources/log4j2.xml | 21 - P/Sd1/CarJump/V2/.gitignore | 4 - P/Sd1/CarJump/V2/pom.xml | 48 - .../hdm_stuttgart/mi/sd1/carjump/Driver.java | 19 - .../mi/sd1/carjump/model/CarJump.java | 74 -- .../CarJump/V2/src/main/resources/log4j2.xml | 21 - .../mi/sd1/carjump/ModelTest.java | 22 - P/Sd1/CarJump/V5/.gitignore | 4 - P/Sd1/CarJump/V5/pom.xml | 48 - .../hdm_stuttgart/mi/sd1/carjump/Driver.java | 19 - .../mi/sd1/carjump/model/CarJump.java | 110 -- .../mi/sd1/carjump/ModelTest.java | 22 - P/Sd1/CoreClasses/StringMask/.gitignore | 54 + P/Sd1/CoreClasses/StringMask/pom.xml | 80 ++ .../java/de/hdm_stuttgart/mi/sd1/Mask.java | 55 + .../StringMask}/src/main/resources/log4j2.xml | 2 +- .../de/hdm_stuttgart/mi/sd1/MaskTest.java | 40 + P/pom.xml | 1 + 27 files changed, 785 insertions(+), 1518 deletions(-) delete mode 100644 Doc/Sd1/Ref/Fig/carSlope.fig delete mode 100644 Doc/Sd1/Ref/Fig/velocityDecompose.fig delete mode 100644 P/Sd1/CarJump/V1/.gitignore delete mode 100644 P/Sd1/CarJump/V1/pom.xml delete mode 100644 P/Sd1/CarJump/V1/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/Driver.java delete mode 100644 P/Sd1/CarJump/V1/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/model/CarJump.java delete mode 100644 P/Sd1/CarJump/V1/src/main/resources/log4j2.xml delete mode 100644 P/Sd1/CarJump/V2/.gitignore delete mode 100644 P/Sd1/CarJump/V2/pom.xml delete mode 100644 P/Sd1/CarJump/V2/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/Driver.java delete mode 100644 P/Sd1/CarJump/V2/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/model/CarJump.java delete mode 100644 P/Sd1/CarJump/V2/src/main/resources/log4j2.xml delete mode 100644 P/Sd1/CarJump/V2/src/test/java/de/hdm_stuttgart/mi/sd1/carjump/ModelTest.java delete mode 100644 P/Sd1/CarJump/V5/.gitignore delete mode 100644 P/Sd1/CarJump/V5/pom.xml delete mode 100644 P/Sd1/CarJump/V5/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/Driver.java delete mode 100644 P/Sd1/CarJump/V5/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/model/CarJump.java delete mode 100644 P/Sd1/CarJump/V5/src/test/java/de/hdm_stuttgart/mi/sd1/carjump/ModelTest.java create mode 100644 P/Sd1/CoreClasses/StringMask/.gitignore create mode 100644 P/Sd1/CoreClasses/StringMask/pom.xml create mode 100644 P/Sd1/CoreClasses/StringMask/src/main/java/de/hdm_stuttgart/mi/sd1/Mask.java rename P/Sd1/{CarJump/V5 => CoreClasses/StringMask}/src/main/resources/log4j2.xml (89%) create mode 100644 P/Sd1/CoreClasses/StringMask/src/test/java/de/hdm_stuttgart/mi/sd1/MaskTest.java diff --git a/Doc/Common/snippets.xml b/Doc/Common/snippets.xml index e7b0269f0..6359febed 100644 --- a/Doc/Common/snippets.xml +++ b/Doc/Common/snippets.xml @@ -79,6 +79,16 @@ </informaltable> </glossdef> </glossentry> + + <glossentry> + <glossterm>Annotation</glossterm> + + <glossdef> + <annotation role="make"> + <para role="eclipse">Sd1/Filepath</para> + </annotation> + </glossdef> + </glossentry> </glosslist> <qandaset defaultlabel="qanda" xml:id="qanda_"> diff --git a/Doc/Sd1/Ref/Fig/carSlope.fig b/Doc/Sd1/Ref/Fig/carSlope.fig deleted file mode 100644 index ad2d90c82..000000000 --- a/Doc/Sd1/Ref/Fig/carSlope.fig +++ /dev/null @@ -1,72 +0,0 @@ -#FIG 3.2 Produced by xfig version 3.2.5c -Landscape -Center -Metric -A4 -100.00 -Single --2 -1200 2 -5 1 0 1 0 7 50 -1 -1 4.000 0 0 0 0 492.188 5723.438 450 4950 990 5130 1215 5445 -5 1 0 1 0 7 50 -1 -1 4.000 0 0 0 0 2072.812 5526.562 2115 6300 1575 6120 1350 5805 -5 1 2 1 0 7 50 -1 -1 3.000 0 0 0 0 3416.500 7120.178 2912 6102 3434 5984 3921 6102 -5 1 0 1 0 7 50 -1 -1 4.000 0 0 0 0 2688.506 6264.902 2909 6110 2951 6204 2954 6311 -5 1 0 1 0 7 50 -1 -1 4.000 0 1 0 0 4132.494 6270.902 3912 6116 3870 6210 3867 6317 -5 1 0 1 0 7 50 -1 -1 0.000 0 0 1 0 3487.500 5557.500 3465 6615 3060 6525 2655 6210 - 2 1 1.00 60.00 120.00 -6 231 4805 520 4940 -6 250 4863 366 4940 -5 1 0 1 0 7 50 -1 -1 4.000 0 0 0 0 308.500 4920.500 270 4921 308 4882 347 4921 -1 3 0 1 0 7 50 -1 -1 4.000 1 0.0000 308 4921 19 19 308 4921 308 4940 --6 -6 404 4863 520 4940 -5 1 0 1 0 7 50 -1 -1 4.000 0 0 0 0 462.500 4920.500 424 4921 462 4882 501 4921 -1 3 0 1 0 7 50 -1 -1 4.000 1 0.0000 462 4921 19 19 462 4921 462 4940 --6 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 2 - 347 4921 424 4921 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 4 - 270 4921 231 4921 231 4863 308 4863 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 7 - 308 4863 327 4805 404 4805 462 4863 520 4863 520 4921 - 501 4921 --6 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 2 - 1215 5445 1350 5805 -2 1 1 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 2 - 1935 6300 450 6300 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 1 1 2 - 2 1 1.00 60.00 120.00 - 2 1 1.00 60.00 120.00 - 495 4905 495 6345 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 2 - 270 4950 450 4950 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 3 - 2113 6302 2385 6302 2912 6102 -2 1 1 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 2 - 2430 6308 4403 6305 -2 1 1 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 2 - 2914 6094 2239 6094 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 1 0 2 - 2 1 1.00 60.00 120.00 - 2340 5741 2340 6101 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 1 0 2 - 2 1 1.00 60.00 120.00 - 2340 6685 2340 6280 -2 1 1 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 2 - 2925 6120 2925 5445 -2 1 1 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 2 - 3915 6120 3915 5445 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 1 1 2 - 2 1 1.00 60.00 120.00 - 2 1 1.00 60.00 120.00 - 2925 5490 3915 5490 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 3 - 4731 6302 4449 6301 3922 6101 -4 0 0 50 -1 0 12 0.0000 6 180 525 540 5670 $h_1$\001 -4 0 0 50 -1 0 12 0.0000 6 180 525 2205 6255 $h_2$\001 -4 0 0 50 -1 0 12 0.0000 6 165 315 3240 5445 $d$\001 -4 0 0 50 -1 0 12 0.0000 6 195 720 3960 6300 $\\alpha$\001 -4 0 0 50 -1 0 12 0.0000 6 195 720 2790 6300 $\\alpha$\001 -4 0 0 50 -1 0 12 0.0000 4 180 435 3510 6660 slope\001 -4 0 0 50 -1 0 12 0.0000 4 135 1065 -630 6345 Ground level\001 diff --git a/Doc/Sd1/Ref/Fig/velocityDecompose.fig b/Doc/Sd1/Ref/Fig/velocityDecompose.fig deleted file mode 100644 index 1ad72443e..000000000 --- a/Doc/Sd1/Ref/Fig/velocityDecompose.fig +++ /dev/null @@ -1,40 +0,0 @@ -#FIG 3.2 Produced by xfig version 3.2.5c -Landscape -Center -Metric -A4 -100.00 -Single --2 -1200 2 -5 1 2 1 0 7 50 -1 -1 3.000 0 0 0 0 3416.500 7120.178 2912 6102 3434 5984 3921 6102 -5 1 0 1 0 7 50 -1 -1 4.000 0 0 0 0 2688.506 6264.902 2909 6110 2951 6204 2954 6311 -6 2070 6120 2385 6300 -5 1 0 1 0 7 50 -1 -1 4.000 0 0 0 0 2307.500 6270.500 2269 6271 2307 6232 2346 6271 -5 1 0 1 0 7 50 -1 -1 4.000 0 0 0 0 2153.500 6270.500 2115 6271 2153 6232 2192 6271 -1 3 0 1 0 7 50 -1 -1 4.000 1 0.0000 2153 6271 19 19 2153 6271 2153 6290 -1 3 0 1 0 7 50 -1 -1 4.000 1 0.0000 2307 6271 19 19 2307 6271 2307 6290 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 2 - 2192 6271 2269 6271 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 4 - 2115 6271 2076 6271 2076 6213 2153 6213 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 7 - 2153 6213 2172 6155 2249 6155 2307 6213 2365 6213 2365 6271 - 2346 6271 --6 -2 1 0 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 3 - 2113 6302 2385 6302 2912 6102 -2 1 1 1 0 7 50 -1 -1 4.000 0 0 -1 0 0 2 - 2430 6308 4403 6305 -2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 2 - 2 1 1.00 60.00 120.00 - 2925 6075 4365 5580 -2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 2 - 2 1 1.00 60.00 120.00 - 2925 6075 4320 6075 -2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 2 - 2 1 1.00 60.00 120.00 - 4320 6075 4320 5580 -4 0 0 50 -1 0 12 0.0000 6 195 720 2790 6300 $\\alpha$\001 -4 0 0 50 -1 0 12 0.0000 6 180 525 3690 6255 $v_x$\001 -4 0 0 50 -1 0 12 0.0000 6 195 525 4410 5895 $v_y$\001 diff --git a/Doc/Sd1/coreClasses.xml b/Doc/Sd1/coreClasses.xml index a6192f534..c3e9db4af 100644 --- a/Doc/Sd1/coreClasses.xml +++ b/Doc/Sd1/coreClasses.xml @@ -192,9 +192,9 @@ s1.equals(s2): true</screen> </classname><methodname>equals(...)</methodname> and <methodname>hashCode()</methodname></title> - <para>If <methodname>a.equals(b)</methodname> ==> <code language="java">a.hashCode() == - b.hashCode()</code>. <emphasis role="red">The reverse does not - apply!</emphasis></para> + <para>If <methodname>a.equals(b)</methodname> ==> <code + language="java">a.hashCode() == b.hashCode()</code>. <emphasis + role="red">The reverse does not apply!</emphasis></para> <para>Consequence: <methodname>equals()</methodname> and <methodname>hashCode()</methodname> must be <emphasis role="red">redefined @@ -208,8 +208,8 @@ s1.equals(s2): true</screen> <qandadiv> <qandaentry> <question> - <para>This exercise aims at a better understanding of - <code language="java">System.out.<link + <para>This exercise aims at a better understanding of <code + language="java">System.out.<link xlink:href="https://docs.oracle.com/javase/9/docs/api/java/io/PrintStream.html#format-java.lang.String-java.lang.Object...-">format()</link></code> already being used in <xref linkend="sd1QandaSquareNumberTableFormatted"/> and other @@ -249,7 +249,8 @@ s1.equals(s2): true</screen> method</link> providing an additional locale argument does indeed exist.</para> - <para>According to <code language="java">System.out.<methodname + <para>According to <code + language="java">System.out.<methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/io/PrintStream.html#format-java.lang.String-java.lang.Object...-">format(...)</methodname></code> the first argument must be of type <code language="java" xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html">String</code>. @@ -263,10 +264,11 @@ s1.equals(s2): true</screen> </listitem> <listitem> - <para>There is a one to one correspondence between each - <code language="java">%...</code> format string and its corresponding - argument: If n further arguments exist the format string must - contain n <quote>%</quote> format specifiers.</para> + <para>There is a one to one correspondence between each <code + language="java">%...</code> format string and its + corresponding argument: If n further arguments exist the + format string must contain n <quote>%</quote> format + specifiers.</para> </listitem> </orderedlist> @@ -325,8 +327,8 @@ java.util.IllegalFormatConversionException: <emphasis role="bold">d != java.lang <question> <para>In Exercise <xref linkend="sw1QandaCircleAreaFinal"/> you calculated a given circle's area protecting variables against - accidental redefinition using the <code language="java">final</code> - modifier:</para> + accidental redefinition using the <code + language="java">final</code> modifier:</para> <programlisting language="java">public static void main(String[] args) { @@ -347,7 +349,8 @@ java.util.IllegalFormatConversionException: <emphasis role="bold">d != java.lang predefines constants in <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html">java.lang.Math</classname> class. Read its documentation to rewrite your code thereby - replacing your own variable <code language="java">pi</code>'s definition .</para> + replacing your own variable <code language="java">pi</code>'s + definition .</para> <tip> <para>You may want to read the <quote>Static Members</quote> and @@ -417,8 +420,9 @@ public final class Math { <para>This accounts for using the expression <varname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html#field.summary">Math.PI</varname>.</para> - <para>The careful reader may have expected an <code language="java">import</code> - statement in order to use the <classname + <para>The careful reader may have expected an <code + language="java">import</code> statement in order to use the + <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html">Math</classname> class:</para> @@ -449,12 +453,14 @@ public class CircleAreaCalculator { </qandaset> </section> - <section xml:id="sw1SectAdvancedExternalStringExercise"> - <title>External String exercises</title> + <section xml:id="sd1_coreClasses_sect_qandaString"> + <title>String exercises</title> <qandaset defaultlabel="qanda" xml:id="sw1QandaExternalAdvancedString"> <qandadiv> <qandaentry> + <title>Strings on CodingBat</title> + <question> <para>Solve all remaining exercises from the <link xlink:href="http://codingbat.com/java/String-1">String-1</link> @@ -473,12 +479,142 @@ public class CircleAreaCalculator { </qandaentry> </qandadiv> </qandaset> - </section> - <section xml:id="sd1_sect_emoticons"> - <title>UTF-8 and emoticons</title> + <qandaset defaultlabel="qanda" xml:id="sd1coreClasses_qanda_stringMask"> + <title>Masking strings</title> + + <qandadiv> + <qandaentry> + <question> + <para>Whenever you enter sensible information like billing details + you don't want others sneaking these details. On the other hand + you still want to recognize <abbrev>e.g.</abbrev> the account in + question.</para> + + <para>One technique solving this conflict is masking. Considering + an <acronym>IBAN</acronym> bank account number we have two + possibilities:</para> + + <glosslist> + <glossentry> + <glossterm>Full disclosure:</glossterm> + + <glossdef> + <screen>IBAN: DE09300606010006778453</screen> + + <itemizedlist> + <listitem> + <para>Advantage: Full control concerning + <abbrev>e.g.</abbrev> correctness of entered + value.</para> + </listitem> + + <listitem> + <para>Disadvantage: Possibility of tapping by + criminals.</para> + </listitem> + </itemizedlist> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Masking:</glossterm> + + <glossdef> + <screen>IBAN: ##################8453</screen> + + <itemizedlist> + <listitem> + <para>Advantage: Protection against felons.</para> + </listitem> + + <listitem> + <para>Disadvantage: Limited distinguishability.</para> + </listitem> + </itemizedlist> + </glossdef> + </glossentry> + </glosslist> + + <para>Implement a corresponding method:</para> + + <programlisting language="java">public class Mask { + + static private final int VISIBLE_POSITIONS = 4; + + /** + * <p>Mask all but 4 positions of a given string by '#' characters. Examples:</p> + * + * <ul> + * <li> "You" --> "You" </li> + * <li> "Secret" --> "##cret" </li> + * <li> "Ultimate test" --> "#########test" </li> + * </ul> + * + * @param s The input string to be masked. + * @return The masked string or null in case of null input string + */ + public static String mask(final String s) { + ... + return ... ; + } +}</programlisting> + + <para>The following unit tests provide basic testing:</para> + + <programlisting language="java">public class MaskTest { + /** + * null input + */ + @Test + public void testNullInput() { + Assert.assertEquals( null, Mask.mask(null) ); + } + + /** + * 4 or less characters should be left untouched. + */ + @Test + public void testBelowLimit() { + Assert.assertEquals( "", Mask.mask("") ); + Assert.assertEquals( "1", Mask.mask("1") ); + Assert.assertEquals( "12", Mask.mask("12") ); + Assert.assertEquals( "123", Mask.mask("123") ); + Assert.assertEquals( "1234", Mask.mask("1234") ); + } + /** + * More than 4 characters require masking. + */ + @Test + public void testAboveLimit() { + Assert.assertEquals( "#2345", Mask.mask("12345") ); + Assert.assertEquals( "##3456", Mask.mask("123456") ); + Assert.assertEquals( "###4567", Mask.mask("1234567") ); + Assert.assertEquals( "####5678", Mask.mask("12345678") ); + Assert.assertEquals( "#####6789", Mask.mask("123456789") ); + } +}</programlisting> + </question> + + <answer> + <annotation role="make"> + <para role="eclipse">Sd1/CoreClasses/StringMask</para> + </annotation> + + <para>Notice the presence of an alternate implementation + <methodname>mask2(String s)</methodname> employing yet not covered + <link linkend="sw1ChapterArrays">arrays</link> being based on the + <methodname + xlink:href="https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#fill-char:A-char-">Arrays.fill + (...)</methodname> library method.</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="sd1coreClasses_qanda_emoticons"> + <title>UTF-8 and emoticons</title> - <qandaset defaultlabel="qanda" xml:id="sd1_qanda_emoticons"> <qandadiv> <qandaentry> <question> @@ -524,12 +660,10 @@ public class CircleAreaCalculator { </qandaentry> </qandadiv> </qandaset> - </section> - - <section xml:id="sw1StringDigitProduct"> - <title>Analyzing strings</title> <qandaset defaultlabel="qanda" xml:id="sw1QandaDigitStringLargestProduct"> + <title>Analyzing strings</title> + <qandadiv> <qandaentry> <question> @@ -593,7 +727,8 @@ public class CircleAreaCalculator { <listitem> <para>The <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html">String</classname> - class does contain a method returning the <code language="java" + class does contain a method returning the <code + language="java" xlink:href="https://docs.oracle.com/javase/tutorial/java/data/characters.html">char</code> value at a given index. Read the documentation to find it.</para> @@ -698,22 +833,25 @@ public class CircleAreaCalculator { }</programlisting> <para>Unfortunately this method sometimes returns weird results: - The argument <code language="java">"5397536978179"</code> for example returns - -1594459696. This negative value is due to an arithmetic overflow: - The product 5 × 3 × 9 × 7 × 5 × 3 × 6 × 9 × 7 × 8 × 1 × 7 × 9 is - actually 2,700,507,600. This exceeds <code language="java" + The argument <code language="java">"5397536978179"</code> for + example returns -1594459696. This negative value is due to an + arithmetic overflow: The product 5 × 3 × 9 × 7 × 5 × 3 × 6 × 9 × 7 + × 8 × 1 × 7 × 9 is actually 2,700,507,600. This exceeds <code + language="java" xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html#MAX_VALUE">MAX_VALUE</code> of 2,147,483,647 <link xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">being - representable by a 4 byte <code language="java">int</code> variable</link>.</para> + representable by a 4 byte <code language="java">int</code> + variable</link>.</para> - <para>Luckily a <code language="java">long</code> variable will be able to hold - positive values <link + <para>Luckily a <code language="java">long</code> variable will be + able to hold positive values <link xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">up to 9,223,372,036,854,775,807</link>. This is much larger than our <quote>worst case</quote> 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 × 9 = 2,541,865,828,329 value. We thus change our data type - from <code language="java">int</code> to <code language="java">long</code>:</para> + from <code language="java">int</code> to <code + language="java">long</code>:</para> <programlisting language="java"> private static <emphasis role="bold">long</emphasis> getDigitProduct(final String digitWord) { @@ -724,8 +862,8 @@ public class CircleAreaCalculator { return product; }</programlisting> - <para>Assembling these pieces leaves us with the following - <code language="java">main(...)</code> method:</para> + <para>Assembling these pieces leaves us with the following <code + language="java">main(...)</code> method:</para> <programlisting language="java"> public static void main(String[] args) { @@ -771,21 +909,18 @@ public class CircleAreaCalculator { <para>The largest product of 13 successive digits stems from the substring <code>"5576689664895</code>" <coref linkend="sd1CoMaxProductSequence"/> starting with the last three - digits in the fourth definition line of <code language="java">String input = - ...</code>.</para> + digits in the fourth definition line of <code + language="java">String input = ...</code>.</para> <screen>The substring '5576689664895' yields the largest product value 23514624000.</screen> </answer> </qandaentry> </qandadiv> </qandaset> - </section> - - <section xml:id="pitfallsUsingOperatorEquals"> - <title>Pitfalls using the operator <quote>==</quote></title> <qandaset defaultlabel="qanda" xml:id="qandaStringOperatorEquals"> - <title>String instances and equality</title> + <title>Pitfalls using <quote>==</quote>: Equality of + <classname>String</classname> instances</title> <qandadiv> <qandaentry> @@ -835,18 +970,19 @@ b1.equals(b2): true </programlisting> <calloutlist> <callout arearefs="answerCoStringOperatorEquality-1-co" xml:id="answerCoStringOperatorEquality-1"> - <para>The string literal <code language="java">"TestA"</code> is being - instantiated only once: The <xref linkend="glo_Java"/> - compiler implementation allocates only one instance of class - String per string literal.</para> + <para>The string literal <code language="java">"TestA"</code> + is being instantiated only once: The <xref + linkend="glo_Java"/> compiler implementation allocates only + one instance of class String per string literal.</para> <para>The string literal "TestA" (even if being defined within different classes) always represents the same object. Thus the - two distinct variables <code language="java">a1</code> and <code language="java">a2</code> are - being assigned an identical reference pointing to this unique - instance. As per definition the operator == compares two - object references for equality and thus returns true. You - might as well write:</para> + two distinct variables <code language="java">a1</code> and + <code language="java">a2</code> are being assigned an + identical reference pointing to this unique instance. As per + definition the operator == compares two object references for + equality and thus returns true. You might as well + write:</para> <programlisting language="java">System.out.println(<emphasis role="bold">"TestA" == "TestA"</emphasis>);</programlisting> @@ -868,8 +1004,9 @@ b1.equals(b2): true </programlisting> <screen>Hashcode a1 == 366712642, Hashcode a2 == 366712642</screen> - <para>So <code language="java">a1</code> and <code language="java">a2</code> indeed point to - the same object resulting in true when using the <methodname + <para>So <code language="java">a1</code> and <code + language="java">a2</code> indeed point to the same object + resulting in true when using the <methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#equals-java.lang.Object-">equals()</methodname> method for comparison.</para> </callout> @@ -879,9 +1016,10 @@ b1.equals(b2): true </programlisting> <para>Every call to the <link xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#String-java.lang.String-">String constructor</link> will create a new string instance. Thus - <code language="java">b1</code> and <code language="java">b2</code> will hold two distinct - references pointing to different String instances albeit these - two instances contain identical values. Following the above + <code language="java">b1</code> and <code + language="java">b2</code> will hold two distinct references + pointing to different String instances albeit these two + instances contain identical values. Following the above reasoning we may execute:</para> <programlisting language="java">System.out.println("Hashcode b1 == " + System.identityHashCode(b1) + @@ -893,8 +1031,8 @@ b1.equals(b2): true </programlisting> <screen>Hashcode b1 == 1829164700, Hashcode b2 == 2018699554</screen> <para>Comparing these two reference values for equality the - <quote>==</quote> operator thus returns - <code language="java">false</code>.</para> + <quote>==</quote> operator thus returns <code + language="java">false</code>.</para> <para>The <methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#equals-java.lang.Object-">equals()</methodname> @@ -906,12 +1044,10 @@ b1.equals(b2): true </programlisting> </qandaentry> </qandadiv> </qandaset> - </section> - - <section xml:id="sd1SectStringWeirdo"> - <title>Weird, weirder, weirdest!</title> <qandaset defaultlabel="qanda" xml:id="sd1QandaStringWeirdo"> + <title>Weird, weirder, weirdest!</title> + <qandadiv> <qandaentry> <question> @@ -973,18 +1109,18 @@ b1.equals(b2): true </programlisting> </question> <answer> - <para>Having <code language="java">name = "An" + "ton"</code> in place we - get:</para> + <para>Having <code language="java">name = "An" + "ton"</code> in + place we get:</para> <screen>Content:Anton <emphasis role="bold">Instances are equal</emphasis></screen> <para>The Java compiler performs a clever optimization: The - expression <code language="java">"An" + "ton"</code> is being evaluated at compile - time. Its value is identical to the string literal - <code language="java">"Anton"</code> and thus both variables - <varname>name</varname> and <varname>reference</varname> will be - initialized to a common <classname + expression <code language="java">"An" + "ton"</code> is being + evaluated at compile time. Its value is identical to the string + literal <code language="java">"Anton"</code> and thus both + variables <varname>name</varname> and <varname>reference</varname> + will be initialized to a common <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html">String</classname> instance:</para> @@ -996,15 +1132,15 @@ b1.equals(b2): true </programlisting> </mediaobject> </informalfigure> - <para>When starting from <code language="java">name = "An".concat("ton")</code> - the result is different:</para> + <para>When starting from <code language="java">name = + "An".concat("ton")</code> the result is different:</para> <screen>Content:Anton <emphasis role="bold">Instances are not equal</emphasis></screen> <para>This time the Java compiler is unable to dynamically resolve - the expression <code language="java">"An".concat("ton")</code> at compile time. - Instead the method <methodname + the expression <code language="java">"An".concat("ton")</code> at + compile time. Instead the method <methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#concat-java.lang.String-">concat(...)</methodname>will be called at runtime resulting in a second <classname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html">String</classname> @@ -1034,11 +1170,12 @@ b1.equals(b2): true </programlisting> <question> <para>Regarding relative and absolute path specifications you may want to read the first three paragraphs of section <quote>File - Systems and Paths</quote> in chapter 16.</para> + Systems and Paths</quote> in chapter 16 of <xref + linkend="bib_Kurniawan2015"/>.</para> <para>Depending on your operating system of choice absolute - (=fully qualified) filenames are being represented in different - ways:</para> + (=fully qualified) filenames are being represented + differently:</para> <glosslist> <glossentry> @@ -1174,7 +1311,7 @@ File extension: html</screen> <glossentry> <glossterm>Drive letter string to character - conversion</glossterm> + normalization</glossterm> <glossdef> <para>"C:" → <code>'C'</code></para> @@ -1191,98 +1328,103 @@ File extension: html</screen> </glossentry> </glosslist> - <tip> - <orderedlist> - <listitem> - <para>You will have to deal with absolute and relative (e.g. - <filename>../../Desktop/var.png</filename>) filename - specifications.</para> - </listitem> + <para>Hints:</para> - <listitem> - <para>You will have to deal with files having no extensions - like e.g. <filename>/usr/bin/firefox</filename>. In this - case the <code>extension</code> attribute is expected to be - <code language="java">null</code>.</para> - </listitem> + <orderedlist> + <listitem> + <para>You will have to deal with absolute and relative (e.g. + <filename>../../Desktop/var.png</filename>) filename + specifications.</para> + </listitem> - <listitem> - <para>For the sake of limiting complexity you do not have to - consider filenames starting with a period (like - <filename>/home/heinz/.bashrc</filename>) or containing - multiple periods like - <filename>../arrow.right.png</filename>.</para> - </listitem> + <listitem> + <para>You will have to deal with files having no extensions + like e.g. <filename>/usr/bin/firefox</filename>. In this case + the <code>extension</code> attribute is expected to be <code + language="java">null</code> rather than <code + language="java">""</code>.</para> + </listitem> - <listitem> - <para>To distinguish the above mentioned system defaults for - filename path conventions read the - <classname>java.lang.System</classname> section of chapter 5 - <link - xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/System.html">along - with its API</link>. The book's table of system properties - will enable your implementation to work both on Windows and - non-Windows systems accordingly.</para> - </listitem> + <listitem> + <para>For the sake of limiting complexity do not consider + filenames starting with a period (like + <filename>/home/heinz/.bashrc</filename>) or containing + multiple periods like + <filename>../arrow.right.png</filename>.</para> + </listitem> - <listitem> - <para>You will have to parse filenames like - <code>"/usr/share/openvpn/hdm.conf"</code>. Exercise 7 from - the <quote>Quiz</quote> section of chapter 5 shows an - example dissecting a string containing tokens being - separated by space characters using the <classname - xlink:href="https://docs.oracle.com/javase/9/docs/api/java/util/StringTokenizer.html">java.util.StringTokenizer</classname> - class. Read its documentation and learn how to specify - string delimiters other than default space <code language="java">' '</code>. - It'll allow you to dissect paths using separators '/' (Unix) - or '\' (Windows) to break paths into components.</para> - - <para>Likewise you may split filenames into basename / - extension by using <code>'.'</code> as separator.</para> - </listitem> + <listitem> + <para>To distinguish the above mentioned system defaults for + filename path conventions read the + <classname>java.lang.System</classname> section of chapter 5 + <link + xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/System.html">along + with its API</link>. The table of system properties in <xref + linkend="bib_Kurniawan2015"/> will enable your implementation + to work both on Windows and non-Windows systems + accordingly.</para> + </listitem> - <listitem> - <para>Assembling path components may be effected by either - using a <classname - xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/StringBuffer.html">StringBuffer</classname> - or a <classname - xlink:href="https://docs.oracle.com/javase/9/docs/api/java/util/StringJoiner.html">StringJoiner</classname> - instance. Mind the distinction between relative and absolute - paths!</para> - </listitem> + <listitem> + <para>You will have to parse filenames like + <code>"/usr/share/openvpn/hdm.conf"</code>. Exercise 7 from + the <quote>Quiz</quote> section of chapter 5 in <xref + linkend="bib_Kurniawan2015"/> shows an example dissecting a + string containing tokens being separated by space characters + using the <classname + xlink:href="https://docs.oracle.com/javase/9/docs/api/java/util/StringTokenizer.html">java.util.StringTokenizer</classname> + class. Read its documentation and learn how to specify string + delimiters other than default space <code language="java">' + '</code>. It'll allow you to dissect paths using separators + '/' (Unix) or '\' (Windows) to break paths into + components.</para> + + <para>Likewise you may split filenames into basename / + extension by using <code>'.'</code> as separator.</para> + </listitem> - <listitem> - <para>The methods <methodname - xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#indexOf-int-">indexOf(...)</methodname> - and <methodname - xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#substring-int-int-">substring(...)</methodname> - come in handy when dissecting filenames into basename and - extension.</para> - </listitem> + <listitem> + <para>Assembling path components may be effected by either + using a <classname + xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/StringBuffer.html">StringBuffer</classname> + or a <classname + xlink:href="https://docs.oracle.com/javase/9/docs/api/java/util/StringJoiner.html">StringJoiner</classname> + instance. Mind the distinction between relative and absolute + paths!</para> + </listitem> - <listitem> - <para>The backslash <quote>\</quote> must be escaped when - being part of a <xref linkend="glo_Java"/> String - literal:</para> + <listitem> + <para>The methods <methodname + xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#indexOf-int-">indexOf(...)</methodname> + and <methodname + xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#substring-int-int-">substring(...)</methodname> + come in handy when dissecting filenames into basename and + extension.</para> + </listitem> - <screen> String path = "C:\\programms\\openvpn\\conf\\hdm.ovpn";</screen> - </listitem> + <listitem> + <para>The backslash <quote>\</quote> must be escaped when + being part of a <xref linkend="glo_Java"/> String + literal:</para> - <listitem> - <para>Unit tests may be written for Windows and non-Windows - systems separately. Read <uri - xlink:href="https://www.thekua.com/atwork/2008/09/running-tests-on-a-specific-os-under-junit4">https://www.thekua.com/atwork/2008/09/running-tests-on-a-specific-os-under-junit4</uri> - how to use <methodname>Assume.assumeFalse()</methodname> / - <methodname>Assume.assumeTrue(...)</methodname> to separate - Windows from Unix tests.</para> - </listitem> + <screen> String path = "C:\\programms\\openvpn\\conf\\hdm.ovpn";</screen> + </listitem> - <listitem> - <para>Test your implementation both on Windows and Unix-type - platforms. The following tests may be used as a starting - point:</para> + <listitem> + <para>Unit tests may be written for Windows and non-Windows + systems separately. Read <uri + xlink:href="https://www.thekua.com/atwork/2008/09/running-tests-on-a-specific-os-under-junit4">https://www.thekua.com/atwork/2008/09/running-tests-on-a-specific-os-under-junit4</uri> + how to use <methodname>Assume.assumeFalse()</methodname> / + <methodname>Assume.assumeTrue(...)</methodname> to separate + Windows from Unix tests.</para> + </listitem> - <programlisting language="java">public class MetaInfoUnixTest { + <listitem> + <para>Test your implementation both on Windows and Unix-type + platforms. The following tests may be used as a starting + point:</para> + + <programlisting language="java">public class MetaInfoUnixTest { static private final boolean isWindows = System.getProperty("os.name").startsWith("Windows"); @@ -1340,7 +1482,7 @@ File extension: html</screen> } }</programlisting> - <programlisting language="java">public class MetaInfoWindowsTest { + <programlisting language="java">public class MetaInfoWindowsTest { static private final boolean isWindows = System.getProperty("os.name").startsWith("Windows"); @@ -1400,9 +1542,8 @@ File extension: html</screen> Assert.assertEquals("gif", fmi.extension); } }</programlisting> - </listitem> - </orderedlist> - </tip> + </listitem> + </orderedlist> </question> <answer> diff --git a/Doc/Sd1/objectsClasses.xml b/Doc/Sd1/objectsClasses.xml index 7759c6a63..8c1599dac 100644 --- a/Doc/Sd1/objectsClasses.xml +++ b/Doc/Sd1/objectsClasses.xml @@ -236,8 +236,9 @@ if (null == r) { <listitem> <para>Provide access restrictions.</para> - <para><code language="java">public</code>, <code language="java">private</code> and - <code language="java">protected</code> modifier</para> + <para><code language="java">public</code>, <code + language="java">private</code> and <code + language="java">protected</code> modifier</para> </listitem> <listitem> @@ -357,7 +358,8 @@ public class Q {<lineannotation>Class def</lineannotation> <callout arearefs="sd1_callout_importVsQualifying-4.2-co" xml:id="sd1_callout_importVsQualifying-4.2"> - <para>Unqualified class use due to <code language="java">import</code>.</para> + <para>Unqualified class use due to <code + language="java">import</code>.</para> </callout> </calloutlist></td> </tr> @@ -584,8 +586,8 @@ public class Main { <callout arearefs="sd1_callout_createStringAnswer1-2-co" xml:id="sd1_callout_createStringAnswer1-2"> - <para>Due to the <code language="java">import</code> statement this is - actually + <para>Due to the <code language="java">import</code> + statement this is actually <package>de.hdm_stuttgart.mi.sd1.tooling</package>.<classname>String</classname>.</para> </callout> </calloutlist> @@ -765,7 +767,8 @@ height=44 </screen></td> <para>Optional <link xlink:href="https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html#accesscontrol-levels">access control</link> modifier either of <code language="java">public</code>, - <code language="java">protected</code> or <code language="java">private</code>.</para> + <code language="java">protected</code> or <code + language="java">private</code>.</para> </callout> <callout arearefs="sd1_callout_methodSyntax-2-co" @@ -782,8 +785,9 @@ height=44 </screen></td> </glossentry> <glossentry> - <glossterm>A data type <abbrev>e.g.</abbrev> <code language="java">int</code>, - <code language="java">double</code>, ...</glossterm> + <glossterm>A data type <abbrev>e.g.</abbrev> <code + language="java">int</code>, <code language="java">double</code>, + ...</glossterm> <glossdef> <para>The method will return a value of the given type to its @@ -978,10 +982,11 @@ public class Rectangle { </question> <answer> - <para>First we define two instance variables <code language="java">width</code> and - <code language="java">height</code> representing a - <classname>Rectangle</classname>'s corresponding two parameters - <code language="java">width</code> and <code language="java">height</code>:</para> + <para>First we define two instance variables <code + language="java">width</code> and <code language="java">height</code> + representing a <classname>Rectangle</classname>'s corresponding two + parameters <code language="java">width</code> and <code + language="java">height</code>:</para> <programlisting language="java">public class Rectangle { @@ -1039,8 +1044,8 @@ public class Rectangle { <glossdef> <para>The method's formal parameter <quote>height</quote> shadows the instance variable's name being defined at class - level. We need the <quote>this</quote> keyword in - <code language="java">this.height = height</code> to resolve the + level. We need the <quote>this</quote> keyword in <code + language="java">this.height = height</code> to resolve the ambiguity.</para> </glossdef> </glossentry> @@ -1198,8 +1203,9 @@ public class Circle { </question> <answer> - <para>We define an instance variable <code language="java">radius</code> inside - our class <classname>Circle</classname>:</para> + <para>We define an instance variable <code + language="java">radius</code> inside our class + <classname>Circle</classname>:</para> <programlisting language="java">public class Circle { @@ -1224,11 +1230,12 @@ public class Circle { <emphasis role="bold">this.</emphasis>radius = radius; }</programlisting> - <para>This requires the usage of the <code language="java">this</code> keyword to - distinguish the formal parameter in <methodname>setRadius(double - radius)</methodname> from the instance variable previously being - defined within our class <classname>Circle</classname>. In other - words: We have to resolve a name shadowing conflict.</para> + <para>This requires the usage of the <code + language="java">this</code> keyword to distinguish the formal + parameter in <methodname>setRadius(double radius)</methodname> + from the instance variable previously being defined within our + class <classname>Circle</classname>. In other words: We have to + resolve a name shadowing conflict.</para> <para>The rest of the implementation is (quite) straightforward. A complete class reads:</para> @@ -1373,9 +1380,9 @@ public class Circle { linkend="glo_SVG"/> code being written to standard output. Use <methodname>System.out.println(...)</methodname> calls to create the desired <xref linkend="glo_SVG"/> output. You - may need <code language="java">\"</code> to escape double quotes as in the - subsequent example or use single attribute quotes - instead:</para> + may need <code language="java">\"</code> to escape double + quotes as in the subsequent example or use single attribute + quotes instead:</para> <programlisting language="java">System.out.println("<rect width=\"20\"" ...</programlisting> </glossdef> @@ -1819,13 +1826,15 @@ public class Gamma { <orderedlist> <listitem> - <para>Explain the underlying idea of the term - <code language="java">(secondsSince_2000_01_01 / 60) % 60</code>.</para> + <para>Explain the underlying idea of the term <code + language="java">(secondsSince_2000_01_01 / 60) % + 60</code>.</para> </listitem> <listitem> - <para>Explain the <code language="java">(int)</code> cast's necessity. Why - is there no overflow error about to happen?</para> + <para>Explain the <code language="java">(int)</code> cast's + necessity. Why is there no overflow error about to + happen?</para> </listitem> </orderedlist> </question> @@ -1847,12 +1856,13 @@ public class Gamma { </listitem> <listitem> - <para>The expression <code language="java">(secondsSince_2000_01_01 / 60) % - 60</code> is of type long due to the presence of a - <code language="java">long</code> variable. However the <quote>%</quote> - operator guarantees the result to be in the integer range 0 - ... 59 which may thus be safely narrowed to an - <code language="java">int</code> value.</para> + <para>The expression <code + language="java">(secondsSince_2000_01_01 / 60) % 60</code> + is of type long due to the presence of a <code + language="java">long</code> variable. However the + <quote>%</quote> operator guarantees the result to be in the + integer range 0 ... 59 which may thus be safely narrowed to + an <code language="java">int</code> value.</para> </listitem> </orderedlist> </answer> @@ -2323,8 +2333,8 @@ void main(void) { <para>In <xref linkend="glo_Java"/> method signatures allow for uniquely addressing a method within a given class e.g.:</para> - <para>The method named <code language="java">print</code> having an int argument - followed by a double:</para> + <para>The method named <code language="java">print</code> having an + int argument followed by a double:</para> <programlisting language="java">print(int, double)</programlisting> </figure> @@ -2479,8 +2489,8 @@ a = 33;</programlisting></td> <glossterm>Non-empty argument list</glossterm> <glossdef> - <para>Non-default constructor, e.g. <code language="java">obj = new - MyClass("xyz")</code>.</para> + <para>Non-default constructor, e.g. <code language="java">obj = + new MyClass("xyz")</code>.</para> </glossdef> </glossentry> </glosslist> @@ -2558,8 +2568,8 @@ Rectangle individual = new Rectangle(2, 7); // 2 x 7 </programlisting></td> <informaltable border="1"> <tr> - <th colspan="2">Equivalent: <code language="java">Rectangle r = new - Rectangle();</code></th> + <th colspan="2">Equivalent: <code language="java">Rectangle r = + new Rectangle();</code></th> </tr> <tr> @@ -2730,9 +2740,10 @@ Salary:30000.00</pre> <para>Currently both classes (hopefully!) reside in the same package <package>company</package>. We will assume the two - attributes age and salary yet have no <code language="java">public</code>, - <code language="java">private</code> or <code language="java">protected</code> access modifiers - :</para> + attributes age and salary yet have no <code + language="java">public</code>, <code + language="java">private</code> or <code + language="java">protected</code> access modifiers :</para> <programlisting language="java">package company; @@ -2783,8 +2794,9 @@ public class Driver { <tip> <para>Read the section on <code language="java">public</code>, - <code language="java">private</code>, <code language="java">protected</code> and default - access level.</para> + <code language="java">private</code>, <code + language="java">protected</code> and default access + level.</para> </tip> </question> @@ -2792,8 +2804,8 @@ public class Driver { <para>Moving the <classname>Employee</classname> class from its <package>company</package> package to <package>model</package> changes just one line of code in - <filename>Employee.java</filename> namely its - <code language="java">package</code> declaration. It also adds an + <filename>Employee.java</filename> namely its <code + language="java">package</code> declaration. It also adds an <code language="java">import</code> statement</para> <informaltable border="1"> @@ -2898,8 +2910,8 @@ public class Driver { (<quote>quick and dirty</quote>)</glossterm> <glossdef> - <para>Raising access level from default to - <code language="java">public</code> thereby violating the <link + <para>Raising access level from default to <code + language="java">public</code> thereby violating the <link xlink:href="https://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)">principle of encapsulation</link>:</para> @@ -2912,8 +2924,8 @@ public class Driver { <glossentry> <glossterm>Providing a so called setter method in our - <classname>Employee</classname> class of access type - <code language="java">public</code></glossterm> + <classname>Employee</classname> class of access type <code + language="java">public</code></glossterm> <glossdef> <informaltable border="1"> @@ -3008,11 +3020,13 @@ public class Driver { }</programlisting> <para>This compiles correctly but yields an unexpected result: - The constructor variables <code language="java">int age</code> and <code language="java">int - salary</code> will be assigned to themselves as being indicated - by a corresponding compiler warning. More important their values - don't make it to the <code language="java">age</code> and <code language="java">salary</code> - attributes being defined on class level.</para> + The constructor variables <code language="java">int age</code> + and <code language="java">int salary</code> will be assigned to + themselves as being indicated by a corresponding compiler + warning. More important their values don't make it to the <code + language="java">age</code> and <code + language="java">salary</code> attributes being defined on class + level.</para> <para>Apparently we are missing something. Explain these two compiler warnings. How can the underlying conflict be @@ -3028,9 +3042,9 @@ public class Driver { <answer> <para>When choosing <methodname>public Employee(int age, double - salary)</methodname> we have two sets of variables - (<code language="java">age</code>, <code language="java">salary</code>) in two different - conflicting scopes:</para> + salary)</methodname> we have two sets of variables (<code + language="java">age</code>, <code language="java">salary</code>) + in two different conflicting scopes:</para> <glosslist> <glossentry> @@ -3058,15 +3072,18 @@ public class Driver { <para>Within the constructor's method body the parameter list scope will take precedence over class scope. Thus the assignment - <code language="java">age = age</code> will assign the constructor's argument - <code language="java">age</code> to itself rather than assigning it to the - instance variable <code language="java">age</code> being defined within class + <code language="java">age = age</code> will assign the + constructor's argument <code language="java">age</code> to + itself rather than assigning it to the instance variable <code + language="java">age</code> being defined within class scope.</para> <para>We may explicitly resolve this scope conflict in our - favour by qualifying the instance variables <code language="java">age</code> and - <code language="java">salary</code> using their respective scope being - represented by the <code language="java">this</code> keyword:</para> + favour by qualifying the instance variables <code + language="java">age</code> and <code + language="java">salary</code> using their respective scope being + represented by the <code language="java">this</code> + keyword:</para> <programlisting language="java"> public Employee(int age, double salary) { // The "this" keyword refers to class scope @@ -3166,8 +3183,8 @@ public class Driver { <calloutlist> <callout arearefs="sd1_callout_clubStaticMembercount-1-co" xml:id="sd1_callout_clubStaticMembercount-1"> - <para>The keyword <code language="java">static</code> defines <code language="java">memberCount</code> - as a <emphasis + <para>The keyword <code language="java">static</code> defines <code + language="java">memberCount</code> as a <emphasis xlink:href="https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html">class variable</emphasis> keeping track of the club's overall member count.</para> @@ -3175,8 +3192,9 @@ public class Driver { <callout arearefs="sd1_callout_clubStaticMembercount-2-co" xml:id="sd1_callout_clubStaticMembercount-2"> - <para><code language="java">memberNumber</code> and <code language="java">name</code> being defined as - instance variables.</para> + <para><code language="java">memberNumber</code> and <code + language="java">name</code> being defined as instance + variables.</para> </callout> <callout arearefs="sd1_callout_clubStaticMembercount-3-co" @@ -3243,8 +3261,8 @@ public class Driver { <callout arearefs="sd1_callout_clubMembershipAccessHeadcount-1-co" xml:id="sd1_callout_clubMembershipAccessHeadcount-1"> <para><methodname>getMemberCount()</methodname> being defined as - <emphasis role="bold">class method</emphasis> by virtue of the - <code language="java">static</code> keyword.</para> + <emphasis role="bold">class method</emphasis> by virtue of the <code + language="java">static</code> keyword.</para> </callout> <callout arearefs="sd1_callout_clubMembershipAccessHeadcount-2-co" @@ -3354,7 +3372,8 @@ Club's member count:3</screen> <para>Is it possible to implement <methodname>getDetails()</methodname> from <xref linkend="sd1_fig_staticMemberConstructorImplement"/> as a - class method by adding <code language="java">static</code>?</para> + class method by adding <code + language="java">static</code>?</para> </listitem> <listitem> @@ -3368,7 +3387,8 @@ Club's member count:3</screen> <answer> <orderedlist> <listitem> - <para>Adding the <code language="java">static</code> modifier yields:</para> + <para>Adding the <code language="java">static</code> modifier + yields:</para> <programlisting language="none">public class ClubMember { ... @@ -3425,9 +3445,9 @@ model/ClubMember.java:20: error: non-static variable memberNumber System.out.println(karen.getDetails()); System.out.println("Club's headcount:" + <emphasis role="red">john.</emphasis>getMemberCount());</programlisting> - <para>This is actually a regression with respect to the - <code language="java">static</code> class method variant since the number of - club members does not depend on individual member + <para>This is actually a regression with respect to the <code + language="java">static</code> class method variant since the + number of club members does not depend on individual member instances.</para> </listitem> </orderedlist> @@ -3544,8 +3564,9 @@ public class <link </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1QandaRewriteLoop"> - <title>A method for printing square numbers using <code language="java">for</code>, - <code language="java">while</code> and <code language="java">do</code> ... <code language="java">while</code></title> + <title>A method for printing square numbers using <code + language="java">for</code>, <code language="java">while</code> and <code + language="java">do</code> ... <code language="java">while</code></title> <qandadiv> <qandaentry> @@ -3583,7 +3604,8 @@ public class <link <orderedlist> <listitem> - <para><code language="java">while (...) {...}</code> loop:</para> + <para><code language="java">while (...) {...}</code> + loop:</para> <programlisting language="java"> ... public static void while_squareNumbers(final int limit) { @@ -3597,7 +3619,8 @@ public class <link </listitem> <listitem> - <para><code language="java">do {...} while(...)</code> loop:</para> + <para><code language="java">do {...} while(...)</code> + loop:</para> <programlisting language="java"> ... public static void while_squareNumbers(final int limit) { @@ -3612,8 +3635,8 @@ public class <link </listitem> </orderedlist> - <para>Caveat: The <code language="java">do {...} while(...)</code> part is a - little bit tricky. Read the method's documentation + <para>Caveat: The <code language="java">do {...} while(...)</code> + part is a little bit tricky. Read the method's documentation <emphasis><emphasis role="bold">precisely</emphasis></emphasis> to avoid a common pitfall.</para> @@ -3639,8 +3662,8 @@ public class <link System.out.println("Finished computing square numbers"); } ...</programlisting> - <para>Its tempting to implement a <code language="java">do ... while</code> in a - similar fashion:</para> + <para>Its tempting to implement a <code language="java">do ... + while</code> in a similar fashion:</para> <programlisting language="java">public class LoopExample { ... @@ -3674,14 +3697,16 @@ public class <link System.out.println("Finished computing square numbers"); }</programlisting> - <para>This required if-clause reminds us that a <code language="java">do {...} - while (...)</code> is an ill-suited choice here in comparison to a - <code language="java">while(...){...}</code> or a <code language="java">for(...){...}</code> - loop.</para> + <para>This required if-clause reminds us that a <code + language="java">do {...} while (...)</code> is an ill-suited + choice here in comparison to a <code + language="java">while(...){...}</code> or a <code + language="java">for(...){...}</code> loop.</para> - <para>Actually a <code language="java">for(...){...} loop is the best choice here - since the number of iterations is known in advance, the increment - is constant and it allows for initialization</code>.</para> + <para>Actually a <code language="java">for(...){...} loop is the + best choice here since the number of iterations is known in + advance, the increment is constant and it allows for + initialization</code>.</para> </answer> </qandaentry> </qandadiv> @@ -3829,8 +3854,9 @@ public class <link declaration</glossterm> <glossdef> - <para><code language="java">private double balance</code>, <code language="java">public - void setBalance(double balance)</code></para> + <para><code language="java">private double balance</code>, + <code language="java">public void setBalance(double + balance)</code></para> </glossdef> </glossentry> @@ -3839,9 +3865,9 @@ public class <link declaration</glossterm> <glossdef> - <para><code language="java">private static double </code>interestRate, - <code language="java">public static void setInterestRate(double - z)</code></para> + <para><code language="java">private static double + </code>interestRate, <code language="java">public static + void setInterestRate(double z)</code></para> </glossdef> </glossentry> </glosslist> @@ -3867,17 +3893,19 @@ public class <link interestRate = z; // in contrast interestRate has } // class scope.</programlisting> - <para>The formal variable's name <quote><code language="java">z</code></quote> - may be <emphasis>consistently</emphasis> renamed to any other - legal, non-conflicting value like - <quote><code language="java">myFunnyVariableName</code></quote>:</para> + <para>The formal variable's name <quote><code + language="java">z</code></quote> may be + <emphasis>consistently</emphasis> renamed to any other legal, + non-conflicting value like <quote><code + language="java">myFunnyVariableName</code></quote>:</para> <programlisting language="java"> public static void setInterestRate(double myFunnyVariableName) { interestRate = myFunnyVariableName; }</programlisting> <para>Alternatively name shadowing conflicts may be resolved by - using the keyword <emphasis><code language="java">this</code></emphasis> <coref + using the keyword <emphasis><code + language="java">this</code></emphasis> <coref linkend="sd1ListingThis"/>:</para> <programlisting language="java">public class Konto { @@ -3972,19 +4000,20 @@ public class <link <qandadiv> <qandaentry> <question> - <para>Our current <classname>Account</classname> class does not handle - negative balances accordingly. Typically banks will charge a - different interest rate whenever an account is in debt i.e. - having a negative balance. In this case a second so called - default interest rate (being significantly higher) will be - applied.</para> + <para>Our current <classname>Account</classname> class does + not handle negative balances accordingly. Typically banks will + charge a different interest rate whenever an account is in + debt i.e. having a negative balance. In this case a second so + called default interest rate (being significantly higher) will + be applied.</para> <para>Extend the current project by adding a new instance variable <varname>defaultInterestRate</varname> along with getter and setter methods. Then change the implementation of - <code language="java">applyInterest()</code> and <code language="java">applyInterest(int - years)</code> by using the correct interest value according to - the account's balance being positive or negative.</para> + <code language="java">applyInterest()</code> and <code + language="java">applyInterest(int years)</code> by using the + correct interest value according to the account's balance + being positive or negative.</para> <caution> <para>Do not forget to change the <command>Javadoc</command> @@ -3997,9 +4026,9 @@ public class <link <para role="eclipse">Sd1/interest/V2</para> </annotation> - <para>We introduce a new variable - <code language="java">defaultInterestRate</code> to cover negative balance - values:</para> + <para>We introduce a new variable <code + language="java">defaultInterestRate</code> to cover negative + balance values:</para> <programlisting language="java"> private static double interestRate = 1.5, // applied to positive balances @@ -4064,24 +4093,28 @@ int sum = a + b;</programlisting> </question> <answer> - <para>The sum of a and b may either exceed - <code language="java">java.lang.Integer.MAX_VALUE</code> or in turn may be - less than <code language="java">java.lang.Integer.MIN_VALUE</code>. To avoid - this type of overflow error our variable <code language="java">sum</code> may - be declared of type long:</para> + <para>The sum of a and b may either exceed <code + language="java">java.lang.Integer.MAX_VALUE</code> or in turn + may be less than <code + language="java">java.lang.Integer.MIN_VALUE</code>. To avoid + this type of overflow error our variable <code + language="java">sum</code> may be declared of type + long:</para> <programlisting language="java">int a = ..., b = ...; ...// statements being omitted long sum = a + b;</programlisting> <para>Unfortunately this does not (yet) help at all: Since - both operands <code language="java">a</code> and <code language="java">b</code> are of type - <code language="java">int</code> the expression <code language="java">a + b</code> is also of - type int and will be evaluated as such. To circumvent this - problem we have to cast at least one operand to type - <code language="java">long</code> prior to computing the sum. This works since - the cast operator <code language="java">(long)</code> does have higher - priority than the <quote>+</quote> operator</para> + both operands <code language="java">a</code> and <code + language="java">b</code> are of type <code + language="java">int</code> the expression <code + language="java">a + b</code> is also of type int and will be + evaluated as such. To circumvent this problem we have to cast + at least one operand to type <code language="java">long</code> + prior to computing the sum. This works since the cast operator + <code language="java">(long)</code> does have higher priority + than the <quote>+</quote> operator</para> <programlisting language="java">int a = ..., b = ...; ...// statements being omitted @@ -4101,8 +4134,8 @@ long sum = (long)a + b;</programlisting> <question> <para>Implement a class representing fractions. You may find a dummy implementation containing some (not yet working) sample - usage code being contained in a <code language="java">main()</code> - method.</para> + usage code being contained in a <code + language="java">main()</code> method.</para> <annotation role="make"> <para role="eclipse">Sd1/fraction/V05</para> @@ -4168,13 +4201,14 @@ long sum = (long)a + b;</programlisting> </question> <answer> - <para>Incrementing <code language="java">++a</code> and decrementing - <code language="java">--c</code> happens prior to adding / subtracting their - values to the variable <code language="java">result</code> (prefix notation). - The increment operation <code language="java">b--</code> in contrast happens - after being being subtracted from variable <code language="java">result</code> - (postfix notation). The following code snippet is thus - equivalent:</para> + <para>Incrementing <code language="java">++a</code> and + decrementing <code language="java">--c</code> happens prior to + adding / subtracting their values to the variable <code + language="java">result</code> (prefix notation). The increment + operation <code language="java">b--</code> in contrast happens + after being being subtracted from variable <code + language="java">result</code> (postfix notation). The + following code snippet is thus equivalent:</para> <programlisting language="java"> int a = 6, b = 7, @@ -4392,9 +4426,9 @@ long sum = (long)a + b;</programlisting> numerator *= i; }</programlisting> - <para>In this case our numerator variable of type - <code language="java">long</code> will be set to a value 22! (factorial). - Consider a simple demo code snippet among with its + <para>In this case our numerator variable of type <code + language="java">long</code> will be set to a value 22! + (factorial). Consider a simple demo code snippet among with its result:</para> <informaltable border="1"> @@ -4677,9 +4711,9 @@ Largest long value:9223372036854775807</screen></td> <qandaentry> <question> <para>We recall <xref linkend="sd1SectFraction"/>. So far no one - demanded cancelling fractions. Yet calling <code language="java">new - Fraction(4,8)</code> will create an instance internally being - represented by <inlineequation> + demanded cancelling fractions. Yet calling <code + language="java">new Fraction(4,8)</code> will create an instance + internally being represented by <inlineequation> <m:math display="inline"> <m:mfrac> <m:mi>4</m:mi> @@ -4712,10 +4746,10 @@ Largest long value:9223372036854775807</screen></td> return ??; }</programlisting> - <para>With respect to fractions one or both parameters - <code language="java">a</code> and <code language="java">b</code> <coref - linkend="sd1ListEuclidNeg"/> may be zero or negative. So we do - have several special cases to handle:</para> + <para>With respect to fractions one or both parameters <code + language="java">a</code> and <code language="java">b</code> + <coref linkend="sd1ListEuclidNeg"/> may be zero or negative. So + we do have several special cases to handle:</para> <glosslist> <glossentry> @@ -4974,11 +5008,11 @@ Largest long value:9223372036854775807</screen></td> </m:mrow> </m:math> </inlineequation>. Our simple implementation proposal would - call <code language="java">new Fraction(12, 14)</code> only to discover a - <acronym>GCD</acronym> value of 4. Having larger argument values - this might cause an unnecessary overflow. Moreover the - <acronym>GCD</acronym> calculation will take longer than - needed.</para> + call <code language="java">new Fraction(12, 14)</code> only to + discover a <acronym>GCD</acronym> value of 4. Having larger + argument values this might cause an unnecessary overflow. + Moreover the <acronym>GCD</acronym> calculation will take longer + than needed.</para> <para>We may instead transform the term in question by exchanging the numerators like <inlineequation> @@ -5000,9 +5034,9 @@ Largest long value:9223372036854775807</screen></td> </m:mrow> </m:math> </inlineequation> to enable cancelling <emphasis - role="bold">prior</emphasis> to multiplying. Now the call - <code language="java">new Fraction(4,2)</code> will construct the representation - <inlineequation> + role="bold">prior</emphasis> to multiplying. Now the call <code + language="java">new Fraction(4,2)</code> will construct the + representation <inlineequation> <m:math display="inline"> <m:mfrac> <m:mi>2</m:mi> @@ -5207,8 +5241,9 @@ public class Math { <orderedlist> <listitem> <para>The method's signature looks slightly weird: It does - expect an argument of type <code language="java">int</code> but returns a - <code language="java">long</code> value. Explain the underlying + expect an argument of type <code + language="java">int</code> but returns a <code + language="java">long</code> value. Explain the underlying ratio.</para> </listitem> @@ -5236,10 +5271,10 @@ public class Math { <orderedlist> <listitem> <para>Returning a long is sensible since even small - argument values in return yield large factorials. - <code language="java">long</code> is the best (largest) choice among the - <xref linkend="glo_Java"/> built-in integer types. - Consider the following example code:</para> + argument values in return yield large factorials. <code + language="java">long</code> is the best (largest) choice + among the <xref linkend="glo_Java"/> built-in integer + types. Consider the following example code:</para> <programlisting language="java"> public static void main(String[] args) { @@ -5278,22 +5313,23 @@ public class Math { <m:mo>!</m:mo> </m:mrow> </m:math> - </inlineequation> we already see <code language="java">long</code> - overflow related errors. Thus allowing for even larger - <code language="java">long</code> arguments instead of <code language="java">int</code> - does not make sense at all.</para> + </inlineequation> we already see <code + language="java">long</code> overflow related errors. Thus + allowing for even larger <code language="java">long</code> + arguments instead of <code language="java">int</code> does + not make sense at all.</para> <para>Since <quote>21</quote> is pretty small we might - favour <code language="java">short</code> (or even <code language="java">char</code>) as - argument type:</para> + favour <code language="java">short</code> (or even <code + language="java">char</code>) as argument type:</para> <programlisting language="java">static public long factorial(short n) { ... }</programlisting> <para>This however is a bad idea: Even simple expressions would be flagged as compile time errors since both integer literals and arithmetic expressions in <xref - linkend="glo_Java"/> evaluate to the data type - <code language="java">int</code>:</para> + linkend="glo_Java"/> evaluate to the data type <code + language="java">int</code>:</para> <programlisting language="java">// Compile time error: // The method factorial(short) in the type @@ -6294,9 +6330,10 @@ Ratio Recursive / Loop: 65554</screen></td> }</programlisting> </figure> - <para>Compare your results using <code language="java">seriesLimit=8</code> - terms and the corresponding values from the professional - implementation <link + <para>Compare your results using <code + language="java">seriesLimit=8</code> terms and the + corresponding values from the professional implementation + <link xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html#exp-double">java.lang.Math.exp</link> by calculating <inlineequation> <m:math display="inline"> @@ -8136,8 +8173,9 @@ sin(4 * PI)=4518.2187229323445, difference=4518.2187229323445</screen> <para>Hint: The standard function <methodname xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Math.html#floor-double-">Math.rint(double)</methodname> - could be helpful: It may turn e.g. 4.47 (<code language="java">double</code>) - to 4 (<code language="java">long</code>).</para> + could be helpful: It may turn e.g. 4.47 (<code + language="java">double</code>) to 4 (<code + language="java">long</code>).</para> </question> <answer> @@ -8201,10 +8239,13 @@ sin(4 * PI)=4518.2187229323445, difference=4518.2187229323445</screen> <glossdef> <para>Holding data within own memory location.</para> - <para>Eight value types in <xref linkend="glo_Java"/>: - <code language="java">byte</code>, <code language="java">short</code>, <code language="java">int</code>, - <code language="java">long</code>, <code language="java">float</code>, <code language="java">double</code>, - <code language="java">char</code> and <code language="java">boolean</code>.</para> + <para>Eight value types in <xref linkend="glo_Java"/>: <code + language="java">byte</code>, <code language="java">short</code>, + <code language="java">int</code>, <code + language="java">long</code>, <code language="java">float</code>, + <code language="java">double</code>, <code + language="java">char</code> and <code + language="java">boolean</code>.</para> </glossdef> </glossentry> @@ -8561,8 +8602,8 @@ After printDuplicateValue: <emphasis role="red">6</emphasis></screen></td> <glossterm>Instance scope</glossterm> <glossdef> - <para>Object lifetime: <code language="java">new ...()</code> until being garbage - collected.</para> + <para>Object lifetime: <code language="java">new ...()</code> + until being garbage collected.</para> </glossdef> </glossentry> @@ -8570,8 +8611,8 @@ After printDuplicateValue: <emphasis role="red">6</emphasis></screen></td> <glossterm>Method scope</glossterm> <glossdef> - <para>During method call until (possibly implicit) - <code language="java">return</code>.</para> + <para>During method call until (possibly implicit) <code + language="java">return</code>.</para> </glossdef> </glossentry> </glosslist> @@ -8649,8 +8690,8 @@ After printDuplicateValue: <emphasis role="red">6</emphasis></screen></td> <tr> <td valign="top"><emphasis role="bold">empty</emphasis></td> - <td valign="top"><para><code language="java">true</code> if and only if the stack is - empty. Read only.</para></td> + <td valign="top"><para><code language="java">true</code> if and only + if the stack is empty. Read only.</para></td> </tr> </informaltable> </figure> @@ -9068,8 +9109,8 @@ Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 </figure> <figure xml:id="sd1_fig_equalsDoubleCaveat"> - <title>Caution comparing <code language="java">float</code> / - <code language="java">double</code></title> + <title>Caution comparing <code language="java">float</code> / <code + language="java">double</code></title> <mediaobject> <imageobject> @@ -9347,9 +9388,10 @@ Elapsed time: 1169805 nanoseconds</screen> Expected :0 Actual :10</screen> - <para>We forgot to deal with negative <code language="java">limit</code> values. - Our sum is supposed to start with 0 so negative <code language="java">limit</code> - values should yield 0 like in our loop based solution:</para> + <para>We forgot to deal with negative <code + language="java">limit</code> values. Our sum is supposed to start + with 0 so negative <code language="java">limit</code> values + should yield 0 like in our loop based solution:</para> <programlisting language="java">public static long getSum(int limit) { if (limit < 0) { @@ -9367,10 +9409,12 @@ Actual :10</screen> Expected :2147450880 Actual :-32768</screen> - <para>This actually is a showstopper for large <code language="java">limit</code> - values: The algebraic value of <code language="java">limit * (limit + 1) / - 2</code> might still fit into an <code language="java">int</code>. But <code language="java">limit - * (limit + 1)</code> itself not yet divided by 2 may exceed <code language="java" + <para>This actually is a showstopper for large <code + language="java">limit</code> values: The algebraic value of <code + language="java">limit * (limit + 1) / 2</code> might still fit + into an <code language="java">int</code>. But <code + language="java">limit * (limit + 1)</code> itself not yet divided + by 2 may exceed <code language="java" xlink:href="https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html#MAX_VALUE">Integer.MAX_VALUE</code>. Since the multiplication happens prior to dividing by 2 we see this overflow error happen.</para> @@ -9387,12 +9431,13 @@ Actual :-32768</screen> } }</programlisting> - <para>This is only correct if <code language="java">limit</code> is even. - Otherwise division by 2 leaves us with a remainder of 1.</para> + <para>This is only correct if <code language="java">limit</code> + is even. Otherwise division by 2 leaves us with a remainder of + 1.</para> - <para>However if <code language="java">limit</code> is uneven the second factor - <code language="java">limit + 1</code> will be even. This observation leads us to - the final solution:</para> + <para>However if <code language="java">limit</code> is uneven the + second factor <code language="java">limit + 1</code> will be even. + This observation leads us to the final solution:</para> <programlisting language="java">public static long getSum(int limit) { if (limit < 0) { @@ -9418,528 +9463,4 @@ Elapsed time: 25422 nanoseconds</screen> </qandadiv> </qandaset> </section> - - <section xml:id="sw1ChapterObjectsClassesCannon"> - <title>Simulating a car jump</title> - - <para>We want to model a <link - xlink:href="https://www.sciencechannel.com/tv-shows/outrageous-acts-of-science/videos/insane-car-jump">car - jump</link> to be used in a computer game. <link - xlink:href="https://www.sciencechannel.com/tv-shows/outrageous-acts-of-science/videos/insane-car-jump">Watch - the video</link> to understand the subsequent explanations</para> - - <section xml:id="sd1SectCarJumpBasic"> - <title>A basic model, rolling downwards</title> - - <qandaset defaultlabel="qanda" xml:id="sd1QandaCarJumpBasic"> - <qandadiv> - <qandaentry> - <question> - <para>Our Java model will be implemented in a class - <classname>CarJump</classname>. We start by two simple - assumptions:</para> - - <itemizedlist> - <listitem> - <para>Initially our car will gain speed just just rolling - down the slope.</para> - </listitem> - - <listitem> - <para>The car's motor will remain idle and in particular - will not provide any additional thrust.</para> - </listitem> - - <listitem> - <para>Friction and (airborne) drag shall be - neglected.</para> - </listitem> - </itemizedlist> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/carSlope.fig"/> - </imageobject> - </mediaobject> - - <para>Watch <link - xlink:href="/Sd1/Ref/Fig/carSlope.pdf">carJump.pdf</link> for - parameter details of the above image. In this exercise we start - by just considering the car's velocity when getting airborne. To - get an implementation sketch you may want to import the - subsequently described Maven skeleton and continue its - implementation.</para> - - <annotation role="make"> - <para role="eclipse">Sd1/CarJump/V1</para> - </annotation> - - <para>Do not forget to define suitable unit tests like:</para> - - <programlisting language="java">package ... -import org.junit.Assert; -import org.junit.Test; - -import de.hdm_stuttgart.mi.sd1.carjump.model.CarJump; - -/** - * Unit test for car jump model. - */ -public class ModelTest { - /** - * Checking approximate velocity value. - */ - @Test - public void testAirborneVelocity() { - - final CarJump jump = new CarJump(20, 4); - - Assert.assertEquals(17.71, jump.getSlopeSpeed(), 1.E-2); - } -}</programlisting> - - <tip> - <orderedlist> - <listitem> - <para>This exercise requires some physics knowledge. Since - our car starts from height <inlineequation> - <m:math display="inline"> - <m:msub> - <m:mi>h</m:mi> - - <m:mn>1</m:mn> - </m:msub> - </m:math> - </inlineequation> and gets airborne at <inlineequation> - <m:math display="inline"> - <m:msub> - <m:mi>h</m:mi> - - <m:mn>2</m:mn> - </m:msub> - </m:math> - </inlineequation> we can easily calculate the amount of - potential energy. According to the well known formula - relating a bodies velocity and kinetic energy this allows - for calculating the car's velocity.</para> - </listitem> - - <listitem> - <para>The project skeleton does not yet provide instance - variables describing the model's internal state. - Furthermore the desired velocity is depending on these - model parameters.</para> - </listitem> - </orderedlist> - </tip> - </question> - - <answer> - <para>With <inlineequation> - <m:math display="inline"> - <m:mi>g</m:mi> - </m:math> - </inlineequation> being the gravity acceleration constant of - <inlineequation> - <m:math display="inline"> - <m:mrow> - <m:mi>9.81</m:mi> - - <m:mo>â¢</m:mo> - - <m:mfrac> - <m:mi>m</m:mi> - - <m:msup> - <m:mi>s</m:mi> - - <m:mi>2</m:mi> - </m:msup> - </m:mfrac> - </m:mrow> - </m:math> - </inlineequation> we have:</para> - - <glosslist> - <glossentry> - <glossterm>The car's (difference in) potential Energy when - rolling from <inlineequation> - <m:math display="inline"> - <m:msub> - <m:mi>h</m:mi> - - <m:mn>1</m:mn> - </m:msub> - </m:math> - </inlineequation> down to <inlineequation> - <m:math display="inline"> - <m:msub> - <m:mi>h</m:mi> - - <m:mn>2</m:mn> - </m:msub> - </m:math> - </inlineequation> :</glossterm> - - <glossdef> - <informalequation> - <m:math display="block"> - <m:mrow> - <m:msub> - <m:mi>E</m:mi> - - <m:mi>p</m:mi> - </m:msub> - - <m:mo>=</m:mo> - - <m:mrow> - <m:mi>m</m:mi> - - <m:mo>â¢</m:mo> - - <m:mi>g</m:mi> - - <m:mo>â¢</m:mo> - - <m:mrow> - <m:mo>(</m:mo> - - <m:mrow> - <m:msub> - <m:mi>h</m:mi> - - <m:mn>1</m:mn> - </m:msub> - - <m:mo>-</m:mo> - - <m:msub> - <m:mi>h</m:mi> - - <m:mn>2</m:mn> - </m:msub> - </m:mrow> - - <m:mo>)</m:mo> - </m:mrow> - </m:mrow> - </m:mrow> - </m:math> - </informalequation> - </glossdef> - </glossentry> - - <glossentry> - <glossterm>The car's kinetic Energy due to its speed - <inlineequation> - <m:math display="inline"> - <m:mi>v</m:mi> - </m:math> - </inlineequation> when leaving the ascending - slope:</glossterm> - - <glossdef> - <informalequation> - <m:math display="block"> - <m:mrow> - <m:msub> - <m:mi>E</m:mi> - - <m:mi>k</m:mi> - </m:msub> - - <m:mo>=</m:mo> - - <m:mrow> - <m:mfrac> - <m:mi>1</m:mi> - - <m:mi>2</m:mi> - </m:mfrac> - - <m:mo>â¢</m:mo> - - <m:mi>m</m:mi> - - <m:mo>â¢</m:mo> - - <m:msup> - <m:mi>v</m:mi> - - <m:mi>2</m:mi> - </m:msup> - </m:mrow> - </m:mrow> - </m:math> - </informalequation> - </glossdef> - </glossentry> - - <glossentry> - <glossterm>The car's potential and kinetic energy amounts - are equal:</glossterm> - - <glossdef> - <informalequation> - <m:math display="inline"> - <m:mrow> - <m:msub> - <m:mi>E</m:mi> - - <m:mi>p</m:mi> - </m:msub> - - <m:mo>=</m:mo> - - <m:msub> - <m:mi>E</m:mi> - - <m:mi>k</m:mi> - </m:msub> - </m:mrow> - </m:math> - </informalequation> - </glossdef> - </glossentry> - </glosslist> - - <para>We thus conclude:</para> - - <informalequation> - <m:math display="inline"> - <m:mrow> - <m:mi>v</m:mi> - - <m:mo>=</m:mo> - - <m:msqrt> - <m:mrow> - <m:mi>2</m:mi> - - <m:mo>â¢</m:mo> - - <m:mi>g</m:mi> - - <m:mo>â¢</m:mo> - - <m:mrow> - <m:mo>(</m:mo> - - <m:mrow> - <m:msub> - <m:mi>h</m:mi> - - <m:mn>1</m:mn> - </m:msub> - - <m:mo>-</m:mo> - - <m:msub> - <m:mi>h</m:mi> - - <m:mn>2</m:mn> - </m:msub> - </m:mrow> - - <m:mo>)</m:mo> - </m:mrow> - </m:mrow> - </m:msqrt> - </m:mrow> - </m:math> - </informalequation> - - <para>This leads to the following implementation:</para> - - <annotation role="make"> - <para role="eclipse">Sd1/CarJump/V2</para> - </annotation> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - - <section xml:id="sd1SectCarJumpGettingAirborne"> - <title>Getting airborne</title> - - <qandaset defaultlabel="qanda" xml:id="sd1QandaCarJumpGettingAirborne"> - <qandadiv> - <qandaentry> - <question> - <para>Our game developer asks for the following (derived) - parameters:</para> - - <itemizedlist> - <listitem> - <para>The car's horizontal distance <inlineequation> - <m:math display="inline"> - <m:mi>d</m:mi> - </m:math> - </inlineequation> (see previous exercise pdf - figure).</para> - </listitem> - - <listitem> - <para>The car's maximum height above ground during its - flight.</para> - </listitem> - </itemizedlist> - - <tip> - <para>The car will travel the ascending slope at an angle of - <inlineequation> - <m:math display="inline"> - <m:mi>α</m:mi> - </m:math> - </inlineequation> given in ordinary degrees ranging from 0 - to 90. You can decompose the car's velocity into its - horizontal and vertical components (See pdf for - details):</para> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/velocityDecompose.fig"/> - </imageobject> - </mediaobject> - - <para>The car's horizontal velocity component will remain - constant during flight time. The vertical component will give - rise to an <quote>inverse</quote> free fall until reaching - maximum height. Afterwards the car will enter a free fall - until (hopefully!) landing on the second slope.</para> - </tip> - </question> - - <answer> - <para>Decomposing the car's velocity into its horizontal and - vertical components requires trigonometric functions:</para> - - <informalequation> - <m:math display="inline"> - <m:mtable> - <m:mtr> - <m:mtd> - <m:mrow> - <m:msub> - <m:mi>v</m:mi> - - <m:mi>x</m:mi> - </m:msub> - - <m:mo>=</m:mo> - - <m:mrow> - <m:mi>v</m:mi> - - <m:mo>â¢</m:mo> - - <m:mrow> - <m:mi>cos</m:mi> - - <m:mo>â¡</m:mo> - - <m:mi>α</m:mi> - </m:mrow> - </m:mrow> - </m:mrow> - </m:mtd> - </m:mtr> - - <m:mtr> - <m:mtd> - <m:mrow> - <m:msub> - <m:mi>v</m:mi> - - <m:mi>y</m:mi> - </m:msub> - - <m:mo>=</m:mo> - - <m:mrow> - <m:mi>v</m:mi> - - <m:mo>â¢</m:mo> - - <m:mrow> - <m:mi>sin</m:mi> - - <m:mo>â¡</m:mo> - - <m:mi>α</m:mi> - </m:mrow> - </m:mrow> - </m:mrow> - </m:mtd> - </m:mtr> - </m:mtable> - </m:math> - </informalequation> - - <para>A bodies' velocity in free fall relates to time by:</para> - - <informalequation> - <m:math display="block"> - <m:mrow> - <m:mi>v</m:mi> - - <m:mo>=</m:mo> - - <m:mrow> - <m:mi>g</m:mi> - - <m:mo>â¢</m:mo> - - <m:mi>t</m:mi> - </m:mrow> - </m:mrow> - </m:math> - </informalequation> - - <para>During a given time a free falling bodies' traveling - distance will be:</para> - - <informalequation> - <m:math display="block"> - <m:mrow> - <m:mi>s</m:mi> - - <m:mo>=</m:mo> - - <m:mrow> - <m:mfrac> - <m:mi>1</m:mi> - - <m:mi>2</m:mi> - </m:mfrac> - - <m:mo>â¢</m:mo> - - <m:mi>g</m:mi> - - <m:mo>â¢</m:mo> - - <m:msup> - <m:mi>t</m:mi> - - <m:mi>2</m:mi> - </m:msup> - </m:mrow> - </m:mrow> - </m:math> - </informalequation> - - <para>Assembling these equations leads to:</para> - - <annotation role="make"> - <para role="eclipse">Sd1/CarJump/V5</para> - </annotation> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> - </section> </chapter> diff --git a/P/Sd1/CarJump/V1/.gitignore b/P/Sd1/CarJump/V1/.gitignore deleted file mode 100644 index a1c3ab4d0..000000000 --- a/P/Sd1/CarJump/V1/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -/target/ -/.settings/ -.classpath -.project diff --git a/P/Sd1/CarJump/V1/pom.xml b/P/Sd1/CarJump/V1/pom.xml deleted file mode 100644 index 378058a78..000000000 --- a/P/Sd1/CarJump/V1/pom.xml +++ /dev/null @@ -1,48 +0,0 @@ -<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>carjump</artifactId> - <version>1.0</version> - <packaging>jar</packaging> - - <name>carjump</name> - <url>https://freedocs.mi.hdm-stuttgart.de</url> - - <properties> - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - </properties> - - <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.9.1</version> - </dependency> - - </dependencies> - - <build> - <plugins> - - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>3.7.0</version> - <configuration> - <source>1.8</source> - <target>1.8</target> - </configuration> - </plugin> - - </plugins> - </build> - -</project> diff --git a/P/Sd1/CarJump/V1/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/Driver.java b/P/Sd1/CarJump/V1/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/Driver.java deleted file mode 100644 index 1cf16433f..000000000 --- a/P/Sd1/CarJump/V1/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/Driver.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.hdm_stuttgart.mi.sd1.carjump; - -import de.hdm_stuttgart.mi.sd1.carjump.model.CarJump; - - -/** - * Testing the model manually. - */ -public class Driver { - - /** - * @param args Unused - */ - public static void main( String[] args ) { - final CarJump jump = new CarJump(20, 4); - - System.out.format("True air speed at airborne time: %3d meters per second.%n", Math.round(jump.getSlopeSpeed())); - } -} diff --git a/P/Sd1/CarJump/V1/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/model/CarJump.java b/P/Sd1/CarJump/V1/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/model/CarJump.java deleted file mode 100644 index 1c00c7716..000000000 --- a/P/Sd1/CarJump/V1/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/model/CarJump.java +++ /dev/null @@ -1,40 +0,0 @@ -package de.hdm_stuttgart.mi.sd1.carjump.model; - -/** - * Model of a car jump. - * - */ -public class CarJump { - - /** - * A model of a car gaining speed and then getting airborne for a jump between - * two slopes. - * - * @param accelHeight See {@link #setAccelHeight(double)} - * @param slopeHeight See {@link #setSlopeHeight(double)} - */ - public CarJump(final double accelHeight, final double slopeHeight) { - // TODO: Implement me! - } - - /** - * @return The car's speed when leaving the ascending slope. - */ - public double getSlopeSpeed() { - return 0; // TODO: Implement me correctly! - } - - /** - * @param accelHeight The height measured in meters at which the car will start at the very beginning of its journey. - */ - public void setAccelHeight(double accelHeight) { - // TODO: Implement me! - } - - /** - * @param slopeHeight The slope's tip height above ground level measured in meters. - */ - public void setSlopeHeight(double slopeHeight) { - // TODO: Implement me! - } -} \ No newline at end of file diff --git a/P/Sd1/CarJump/V1/src/main/resources/log4j2.xml b/P/Sd1/CarJump/V1/src/main/resources/log4j2.xml deleted file mode 100644 index 67f853751..000000000 --- a/P/Sd1/CarJump/V1/src/main/resources/log4j2.xml +++ /dev/null @@ -1,21 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<Configuration> - <Appenders> - <File name="A1" fileName="A1.log" append="false"> - <PatternLayout pattern="%t %-5p %c{2} - %m%n"/> - </File> - <Console name="STDOUT" target="SYSTEM_OUT"> - <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> - </Console> - </Appenders> - <Loggers> - - <!-- You my want to define class or package level per-logger rules --> - <Logger name="de.hdm_stuttgart.mi.sd1.carjump.App" level="debug"> - <AppenderRef ref="A1"/> - </Logger> - <Root level="info"> - <AppenderRef ref="STDOUT"/> - </Root> - </Loggers> -</Configuration> \ No newline at end of file diff --git a/P/Sd1/CarJump/V2/.gitignore b/P/Sd1/CarJump/V2/.gitignore deleted file mode 100644 index a1c3ab4d0..000000000 --- a/P/Sd1/CarJump/V2/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -/target/ -/.settings/ -.classpath -.project diff --git a/P/Sd1/CarJump/V2/pom.xml b/P/Sd1/CarJump/V2/pom.xml deleted file mode 100644 index a30be7f4f..000000000 --- a/P/Sd1/CarJump/V2/pom.xml +++ /dev/null @@ -1,48 +0,0 @@ -<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>carjump</artifactId> - <version>2.0</version> - <packaging>jar</packaging> - - <name>carjump</name> - <url>https://freedocs.mi.hdm-stuttgart.de</url> - - <properties> - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - </properties> - - <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.9.1</version> - </dependency> - - </dependencies> - - <build> - <plugins> - - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>3.7.0</version> - <configuration> - <source>1.8</source> - <target>1.8</target> - </configuration> - </plugin> - - </plugins> - </build> - -</project> diff --git a/P/Sd1/CarJump/V2/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/Driver.java b/P/Sd1/CarJump/V2/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/Driver.java deleted file mode 100644 index 52dae66fa..000000000 --- a/P/Sd1/CarJump/V2/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/Driver.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.hdm_stuttgart.mi.sd1.carjump; - -import de.hdm_stuttgart.mi.sd1.carjump.model.CarJump; - - -/** - * - */ -public class Driver { - - /** - * @param args Unused - */ - public static void main( String[] args ) { - final CarJump jump = new CarJump(20, 4); - - System.out.format("True air speed at airborne time: %5.2f meters per second.%n", Math.round(10 *jump.getSlopeSpeed()) / 10.); - } -} diff --git a/P/Sd1/CarJump/V2/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/model/CarJump.java b/P/Sd1/CarJump/V2/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/model/CarJump.java deleted file mode 100644 index 5b00fdb07..000000000 --- a/P/Sd1/CarJump/V2/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/model/CarJump.java +++ /dev/null @@ -1,74 +0,0 @@ -package de.hdm_stuttgart.mi.sd1.carjump.model; - -/** - * Model of a car jump. - * - */ -public class CarJump { - - // Physical constant describing a fall's acceleration - // due to gravity. - private static final double gravitationalAcceleration = 9.81; - - // Model parameters - private double - accelHeight, - slopeHeight; - - // derived parameters - double - slopeSpeed; - - /** - * Internal method to be called at end of object construction - * and after changing any model parameter. Re-calculates - * derived parameters, see above. - */ - private void calculateDerivedParameters() { - slopeSpeed = Math.sqrt(2 * gravitationalAcceleration * (accelHeight - slopeHeight)); - } - - /** - * A model of a car gaining speed and then getting airborne for a jump between - * two slopes. - * - * @param accelHeight See {@link #setAccelHeight(double)} - * @param slopeHeight See {@link #setSlopeHeight(double)} - */ - public CarJump(final double accelHeight, final double slopeHeight) { - // We assign attribute values since calling i.e. setAccelHeight(...) would trigger the invocation - // of calculateDerivedParameters(). But this invocation must be deferred till our object - // has been fully initialized. - // - this.accelHeight = accelHeight; - this.slopeHeight = slopeHeight; - calculateDerivedParameters(); - } - - /** - * @return The car's speed when leaving the ascending slope. - */ - public double getSlopeSpeed() { - return slopeSpeed; - } - - /** - * @param accelHeight The height measured in meters at which the car will start at the very beginning of its journey. - */ - public void setAccelHeight(double accelHeight) { - if (accelHeight != this.accelHeight) { // Only assign and re-calculate if required - this.accelHeight = accelHeight; - calculateDerivedParameters(); - } - } - - /** - * @param slopeHeight The slope's tip height above ground level measured in meters. - */ - public void setSlopeHeight(double slopeHeight) { - if (slopeHeight != this.slopeHeight) {// Only assign and re-calculate if required - this.slopeHeight = slopeHeight; - calculateDerivedParameters(); - } - } -} \ No newline at end of file diff --git a/P/Sd1/CarJump/V2/src/main/resources/log4j2.xml b/P/Sd1/CarJump/V2/src/main/resources/log4j2.xml deleted file mode 100644 index 67f853751..000000000 --- a/P/Sd1/CarJump/V2/src/main/resources/log4j2.xml +++ /dev/null @@ -1,21 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<Configuration> - <Appenders> - <File name="A1" fileName="A1.log" append="false"> - <PatternLayout pattern="%t %-5p %c{2} - %m%n"/> - </File> - <Console name="STDOUT" target="SYSTEM_OUT"> - <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> - </Console> - </Appenders> - <Loggers> - - <!-- You my want to define class or package level per-logger rules --> - <Logger name="de.hdm_stuttgart.mi.sd1.carjump.App" level="debug"> - <AppenderRef ref="A1"/> - </Logger> - <Root level="info"> - <AppenderRef ref="STDOUT"/> - </Root> - </Loggers> -</Configuration> \ No newline at end of file diff --git a/P/Sd1/CarJump/V2/src/test/java/de/hdm_stuttgart/mi/sd1/carjump/ModelTest.java b/P/Sd1/CarJump/V2/src/test/java/de/hdm_stuttgart/mi/sd1/carjump/ModelTest.java deleted file mode 100644 index 661d3a30a..000000000 --- a/P/Sd1/CarJump/V2/src/test/java/de/hdm_stuttgart/mi/sd1/carjump/ModelTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.hdm_stuttgart.mi.sd1.carjump; - -import org.junit.Assert; -import org.junit.Test; - -import de.hdm_stuttgart.mi.sd1.carjump.model.CarJump; - -/** - * Unit test for car jump model. - */ -public class ModelTest { - /** - * Checking approximate velocity value. - */ - @Test - public void testAirborneVelocity() { - - final CarJump jump = new CarJump(20, 4); - - Assert.assertEquals(17.71, jump.getSlopeSpeed(), 1.E-2); - } -} diff --git a/P/Sd1/CarJump/V5/.gitignore b/P/Sd1/CarJump/V5/.gitignore deleted file mode 100644 index a1c3ab4d0..000000000 --- a/P/Sd1/CarJump/V5/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -/target/ -/.settings/ -.classpath -.project diff --git a/P/Sd1/CarJump/V5/pom.xml b/P/Sd1/CarJump/V5/pom.xml deleted file mode 100644 index e9b0187b0..000000000 --- a/P/Sd1/CarJump/V5/pom.xml +++ /dev/null @@ -1,48 +0,0 @@ -<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>carjump</artifactId> - <version>5.0</version> - <packaging>jar</packaging> - - <name>carjump</name> - <url>https://freedocs.mi.hdm-stuttgart.de</url> - - <properties> - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - </properties> - - <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.9.1</version> - </dependency> - - </dependencies> - - <build> - <plugins> - - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>3.7.0</version> - <configuration> - <source>1.8</source> - <target>1.8</target> - </configuration> - </plugin> - - </plugins> - </build> - -</project> diff --git a/P/Sd1/CarJump/V5/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/Driver.java b/P/Sd1/CarJump/V5/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/Driver.java deleted file mode 100644 index ecb167435..000000000 --- a/P/Sd1/CarJump/V5/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/Driver.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.hdm_stuttgart.mi.sd1.carjump; - -import de.hdm_stuttgart.mi.sd1.carjump.model.CarJump; - -/** - * Manually testing car jump model. - */ -public class Driver { - - /** - * @param args Unused - */ - public static void main( String[] args ) { - final CarJump jump = new CarJump(20, 45, 4); - - System.out.format("Jump distance %3d meters.%n", Math.round(jump.getJumpDistance())); - System.out.format("Jump height: %d meters. %n", Math.round(jump.getJumpHeight())); - } -} diff --git a/P/Sd1/CarJump/V5/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/model/CarJump.java b/P/Sd1/CarJump/V5/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/model/CarJump.java deleted file mode 100644 index e9c90e81d..000000000 --- a/P/Sd1/CarJump/V5/src/main/java/de/hdm_stuttgart/mi/sd1/carjump/model/CarJump.java +++ /dev/null @@ -1,110 +0,0 @@ -package de.hdm_stuttgart.mi.sd1.carjump.model; - -public class CarJump { - - // Physical constant describing a fall's acceleration - // due to gravity. - private static final double gravitationalAcceleration = 9.81; - - // Model parameters - private double - accelHeight, - slopeAngle, - slopeHeight; - - // derived parameters - double - slopeSpeed, - jumpHeight, - jumpDistance; - - /** - * Internal method to be called at end of object construction - * and after changing any model parameter. Re-calculates - * derived parameters, see above. - */ - private void calculateDerivedParameters() { - - slopeSpeed = Math.sqrt(2 * gravitationalAcceleration * (accelHeight - slopeHeight)); - - final double slopeAngleInRadians = slopeAngle / 180 * Math.PI; - - jumpDistance = Math.sin(2 * slopeAngleInRadians) * slopeSpeed * slopeSpeed / - gravitationalAcceleration; - - final double sine = Math.sin(slopeAngleInRadians); - jumpHeight = slopeHeight + sine * sine * slopeSpeed * slopeSpeed / 2 / gravitationalAcceleration; - } - - /** - * A model of a car gaining speed and then getting airborne for a jump between - * two slopes. - * - * @param accelHeight See {@link #setAccelHeight(double)} - * @param slopeAngle See {@link #setSlopeAngle(double)} - * @param slopeHeight See {@link #setSlopeHeight(double)} - */ - public CarJump(final double accelHeight, final double slopeAngle, final double slopeHeight) { - // We assign attribute values since calling i.e. setAccelHeight(...) would trigger the invocation - // of calculateDerivedParameters(). But this invocation must be deferred till our object - // has been fully initialized. - // - this.accelHeight = accelHeight; - this.slopeAngle = slopeAngle; - this.slopeHeight = slopeHeight; - calculateDerivedParameters(); - } - - /** - * @return The car's maximum height above ground level while being - * airborne. - */ - public double getJumpHeight() { - return jumpHeight; - } - - /** - * @return - */ - public double getJumpDistance() { - return jumpDistance; - } - - /** - * @return - */ - public double getSlopeSpeed() { - return slopeSpeed; - } - - /** - * @param accelHeight The height measured in meters at which the car will start at the very beginning of its journey. - */ - public void setAccelHeight(double accelHeight) { - if (accelHeight != this.accelHeight) { // Only assign and re-calculate if required - this.accelHeight = accelHeight; - calculateDerivedParameters(); - } - } - - /** - * @param slopeAngle The slope's angle measured in degrees (0-360). - */ - public void setSlopeAngle(double slopeAngle) { - if (slopeAngle != this.slopeAngle) { // Only assign and re-calculate if required - this.slopeAngle = slopeAngle; - calculateDerivedParameters(); - } - } - - - /** - * @param slopeHeight The slope's tip height above ground level measured in meters. - */ - public void setSlopeHeight(double slopeHeight) { - if (slopeHeight != this.slopeHeight) {// Only assign and re-calculate if required - this.slopeHeight = slopeHeight; - calculateDerivedParameters(); - } - } -} \ No newline at end of file diff --git a/P/Sd1/CarJump/V5/src/test/java/de/hdm_stuttgart/mi/sd1/carjump/ModelTest.java b/P/Sd1/CarJump/V5/src/test/java/de/hdm_stuttgart/mi/sd1/carjump/ModelTest.java deleted file mode 100644 index 7414e7409..000000000 --- a/P/Sd1/CarJump/V5/src/test/java/de/hdm_stuttgart/mi/sd1/carjump/ModelTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.hdm_stuttgart.mi.sd1.carjump; - -import org.junit.Assert; -import org.junit.Test; - -import de.hdm_stuttgart.mi.sd1.carjump.model.CarJump; - -/** - * Unit test for car jump model. - */ -public class ModelTest { - /** - * Checking approximate velocity value. - */ - @Test - public void testAirborneVelocity() { - - final CarJump jump = new CarJump(20, 45, 4); - - Assert.assertEquals(17.71, jump.getSlopeSpeed(), 1.E-2); - } -} diff --git a/P/Sd1/CoreClasses/StringMask/.gitignore b/P/Sd1/CoreClasses/StringMask/.gitignore new file mode 100644 index 000000000..1a547d140 --- /dev/null +++ b/P/Sd1/CoreClasses/StringMask/.gitignore @@ -0,0 +1,54 @@ +/target/ +/.settings/ +.classpath +.project + +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties \ No newline at end of file diff --git a/P/Sd1/CoreClasses/StringMask/pom.xml b/P/Sd1/CoreClasses/StringMask/pom.xml new file mode 100644 index 000000000..ec09c3937 --- /dev/null +++ b/P/Sd1/CoreClasses/StringMask/pom.xml @@ -0,0 +1,80 @@ +<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>stringmask</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> + + <name>stringmask</name> + + <url>https://freedocs.mi.hdm-stuttgart.de/sd1SectToolsOfTheTrade2.html</url> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <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.9.1</version> + </dependency> + + </dependencies> + + <build> + <plugins> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.7.0</version> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>2.10.4</version> + <configuration /> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>3.1.0</version> + <configuration> + <transformers> + <transformer + implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> + <manifestEntries> + <Main-Class>de.hdm_stuttgart.mi.sd1.Mask</Main-Class> + </manifestEntries> + </transformer> + </transformers> + </configuration> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + </execution> + </executions> + </plugin> + + </plugins> + </build> +</project> diff --git a/P/Sd1/CoreClasses/StringMask/src/main/java/de/hdm_stuttgart/mi/sd1/Mask.java b/P/Sd1/CoreClasses/StringMask/src/main/java/de/hdm_stuttgart/mi/sd1/Mask.java new file mode 100644 index 000000000..99814ef5b --- /dev/null +++ b/P/Sd1/CoreClasses/StringMask/src/main/java/de/hdm_stuttgart/mi/sd1/Mask.java @@ -0,0 +1,55 @@ +package de.hdm_stuttgart.mi.sd1; + +import java.util.Arrays; + +public class Mask { + + /** + * Number of character positions being visible when masking a string. See {@link #mask(String)} + * for details. + */ + static public final int VISIBLE_POSITIONS = 4; + + /** + * <p>Mask all but {@link #VISIBLE_POSITIONS} positions of a given string by '#' characters. Examples:</p> + * + * <ul> + * <li> "You" --> "You" </li> + * <li> "Secret" --> "##cret" </li> + * <li> "Ultimate test" --> "#########test" </li> + * </ul> + * + * @param s The input string to be masked. + * @return The masked string or null in case of null input string + */ + public static String mask(final String s) { + if (null == s || s.length() <= VISIBLE_POSITIONS) { + return s; + } else { + final int maskLength = s.length() - VISIBLE_POSITIONS; + + final StringBuffer result = new StringBuffer(); + for (int i = 0; i < maskLength; i++) { + result.append('#'); + } + return result.append(s.substring(maskLength)).toString(); + } + } + + /** + * Alternate implementation using yet not covered array topic. + * + * @param s The input string to be masked. + * @return The masked string or null in case of null input string + */ + public static String mask2(final String s) { + if (null == s || s.length() <= VISIBLE_POSITIONS) { + return s; + } else { + final int maskLength = s.length() - VISIBLE_POSITIONS; + final char[] mask = new char[maskLength]; + Arrays.fill(mask, '#'); + return new String(mask).concat(s.substring(maskLength)); + } + } +} diff --git a/P/Sd1/CarJump/V5/src/main/resources/log4j2.xml b/P/Sd1/CoreClasses/StringMask/src/main/resources/log4j2.xml similarity index 89% rename from P/Sd1/CarJump/V5/src/main/resources/log4j2.xml rename to P/Sd1/CoreClasses/StringMask/src/main/resources/log4j2.xml index 67f853751..271655702 100644 --- a/P/Sd1/CarJump/V5/src/main/resources/log4j2.xml +++ b/P/Sd1/CoreClasses/StringMask/src/main/resources/log4j2.xml @@ -11,7 +11,7 @@ <Loggers> <!-- You my want to define class or package level per-logger rules --> - <Logger name="de.hdm_stuttgart.mi.sd1.carjump.App" level="debug"> + <Logger name="de.hdm_stuttgart.mi.sd1.Mask" level="debug"> <AppenderRef ref="A1"/> </Logger> <Root level="info"> diff --git a/P/Sd1/CoreClasses/StringMask/src/test/java/de/hdm_stuttgart/mi/sd1/MaskTest.java b/P/Sd1/CoreClasses/StringMask/src/test/java/de/hdm_stuttgart/mi/sd1/MaskTest.java new file mode 100644 index 000000000..03dd1993c --- /dev/null +++ b/P/Sd1/CoreClasses/StringMask/src/test/java/de/hdm_stuttgart/mi/sd1/MaskTest.java @@ -0,0 +1,40 @@ +package de.hdm_stuttgart.mi.sd1; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit test for simple Mask. + */ +public class MaskTest { + /** + * null input + */ + @Test + public void testNullInput() { + Assert.assertEquals( null, Mask.mask(null) ); + } + + /** + * 4 or less characters should be left untouched. + */ + @Test + public void testBelowLimit() { + Assert.assertEquals( "", Mask.mask("") ); + Assert.assertEquals( "1", Mask.mask("1") ); + Assert.assertEquals( "12", Mask.mask("12") ); + Assert.assertEquals( "123", Mask.mask("123") ); + Assert.assertEquals( "1234", Mask.mask("1234") ); + } + /** + * More than 4 characters require masking. + */ + @Test + public void testAboveLimit() { + Assert.assertEquals( "#2345", Mask.mask("12345") ); + Assert.assertEquals( "##3456", Mask.mask("123456") ); + Assert.assertEquals( "###4567", Mask.mask("1234567") ); + Assert.assertEquals( "####5678", Mask.mask("12345678") ); + Assert.assertEquals( "#####6789", Mask.mask("123456789") ); + } +} \ No newline at end of file diff --git a/P/pom.xml b/P/pom.xml index 96767e5b1..d99f137b9 100644 --- a/P/pom.xml +++ b/P/pom.xml @@ -38,6 +38,7 @@ <module>Sd1/Array/arraycalcExercise</module> <module>Sd1/Array/integerStoreStat</module> <module>Sd1/Array/marking</module> + <module>Sd1/CoreClasses/StringMask</module> <module>Sd1/Euler/11/largestproductingrid</module> <module>Sd1/Wc/readFile</module> <module>Sd1/Wc/wc</module> -- GitLab