diff --git a/Doc/Sd1/Ref/CoreClasses/Strings/newString.multi.svg b/Doc/Sd1/Ref/CoreClasses/Strings/newString.multi.svg index 339d82f7b627c2dc27269102138411391c9c393a..03e781dc02aef22bd018d5f04af03df4118369d1 100644 --- a/Doc/Sd1/Ref/CoreClasses/Strings/newString.multi.svg +++ b/Doc/Sd1/Ref/CoreClasses/Strings/newString.multi.svg @@ -24,16 +24,16 @@ borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" - inkscape:zoom="1.8200176" - inkscape:cx="248.80995" - inkscape:cy="129.99213" + inkscape:zoom="3.6400352" + inkscape:cx="340.54111" + inkscape:cy="250.3012" inkscape:document-units="mm" inkscape:current-layer="g683" showgrid="true" - inkscape:window-width="1600" - inkscape:window-height="1145" + inkscape:window-width="3200" + inkscape:window-height="1689" inkscape:window-x="0" - inkscape:window-y="27" + inkscape:window-y="55" inkscape:window-maximized="1" fit-margin-top="0" fit-margin-left="0" @@ -570,6 +570,66 @@ style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1" id="path5180-9-1" /> </marker> + <marker + inkscape:isstock="true" + style="overflow:visible" + id="marker5331-4" + refX="0" + refY="0" + orient="auto" + inkscape:stockid="DotM"> + <path + transform="matrix(0.4,0,0,0.4,2.96,0.4)" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + d="m -2.5,-1 c 0,2.76 -2.24,5 -5,5 -2.76,0 -5,-2.24 -5,-5 0,-2.76 2.24,-5 5,-5 2.76,0 5,2.24 5,5 z" + id="path5329-3" + inkscape:connector-curvature="0" /> + </marker> + <marker + inkscape:isstock="true" + style="overflow:visible" + id="marker5849-3" + refX="0" + refY="0" + orient="auto" + inkscape:stockid="Arrow2Mend"> + <path + inkscape:connector-curvature="0" + transform="scale(-0.6)" + d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1" + id="path5847-3" /> + </marker> + <marker + inkscape:isstock="true" + style="overflow:visible" + id="marker5331-4-7" + refX="0" + refY="0" + orient="auto" + inkscape:stockid="DotM"> + <path + transform="matrix(0.4,0,0,0.4,2.96,0.4)" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" + d="m -2.5,-1 c 0,2.76 -2.24,5 -5,5 -2.76,0 -5,-2.24 -5,-5 0,-2.76 2.24,-5 5,-5 2.76,0 5,2.24 5,5 z" + id="path5329-3-6" + inkscape:connector-curvature="0" /> + </marker> + <marker + inkscape:isstock="true" + style="overflow:visible" + id="marker5849-3-9" + refX="0" + refY="0" + orient="auto" + inkscape:stockid="Arrow2Mend"> + <path + inkscape:connector-curvature="0" + transform="scale(-0.6)" + d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z" + style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1" + id="path5847-3-1" /> + </marker> </defs> <metadata id="metadata5"> @@ -629,8 +689,8 @@ id="rect720" width="22.489582" height="5.2916665" - x="70.01358" - y="15.791656" /> + x="69.219841" + y="16.320822" /> <text id="text724" y="19.70046" @@ -646,7 +706,7 @@ sodipodi:nodetypes="cc" inkscape:connector-curvature="0" id="path726" - d="M 79.274012,18.437489 H 128.22192" + d="M 78.480258,18.966655 H 127.42817" style="display:inline;fill:none;stroke:#000000;stroke-width:0.52916682;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-start:url(#marker4980-6);marker-end:url(#marker5182-4)" /> <rect style="display:inline;fill:#00ff00;stroke:none;stroke-width:0.44722748;stroke-miterlimit:4;stroke-dasharray:1.34168253, 0.4472275;stroke-dashoffset:0;stroke-opacity:1" @@ -740,8 +800,8 @@ id="tspan671" sodipodi:role="line">Kate</tspan></text> <rect - y="15.791656" - x="70.412529" + y="16.320822" + x="69.219833" height="5.2916665" width="22.489582" id="rect675" @@ -759,7 +819,7 @@ style="stroke-width:0.26458332">s1</tspan></text> <path style="display:inline;fill:none;stroke:#000000;stroke-width:0.52916682;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-start:url(#marker4980-6);marker-end:url(#marker5182-4)" - d="M 79.672948,18.437489 H 128.62086" + d="M 78.480254,18.966655 H 127.42817" id="path681" inkscape:connector-curvature="0" sodipodi:nodetypes="cc" /> @@ -781,6 +841,28 @@ x="130.58456" id="tspan671-5" sodipodi:role="line">Kate</tspan></text> + <g + style="display:inline" + id="g2486" + transform="translate(6.1104156,-11.486383)"> + <path + sodipodi:nodetypes="cc" + inkscape:connector-curvature="0" + id="path2548-9" + d="m 145.924,31.246788 c 14.0992,4.622784 14.04791,4.388619 -0.50417,9.680285" + style="display:inline;fill:none;stroke:#000000;stroke-width:0.52916682;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-start:url(#marker5331-4-7);marker-end:url(#marker5849-3-9)" /> + <text + id="text2448-8" + y="41.975071" + x="151.63133" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:1.25;font-family:'DejaVu Sans Mono';-inkscape-font-specification:'DejaVu Sans Mono';letter-spacing:0px;word-spacing:0px;display:inline;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332" + xml:space="preserve"><tspan + style="stroke-width:0.26458332" + y="41.975071" + x="151.63133" + id="tspan2446-7" + sodipodi:role="line">copy</tspan></text> + </g> </g> <g inkscape:label="Layer 4" @@ -903,6 +985,23 @@ x="130.58456" id="tspan136-7" sodipodi:role="line">Kate</tspan></text> + <path + style="display:inline;fill:none;stroke:#000000;stroke-width:0.52916682;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-start:url(#marker5331-4);marker-end:url(#marker5849-3)" + d="m 151.21567,10.08012 c 14.0992,4.622784 14.04791,4.388619 -0.50417,9.680285" + id="path2548" + inkscape:connector-curvature="0" + sodipodi:nodetypes="cc" /> + <text + xml:space="preserve" + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333311px;line-height:1.25;font-family:'DejaVu Sans Mono';-inkscape-font-specification:'DejaVu Sans Mono';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332" + x="156.923" + y="20.808403" + id="text2448"><tspan + sodipodi:role="line" + id="tspan2446" + x="156.923" + y="20.808403" + style="stroke-width:0.26458332">copy</tspan></text> </g> <g inkscape:label="Layer 2" diff --git a/Doc/Sd1/coreClasses.xml b/Doc/Sd1/coreClasses.xml index 5f6f5153036f761a3cfd75385d7923d1f667699e..a4e54ec585143c3c5fc105e671956f6299bb900a 100644 --- a/Doc/Sd1/coreClasses.xml +++ b/Doc/Sd1/coreClasses.xml @@ -72,14 +72,52 @@ <figure xml:id="sd1_coreclasses_fig_stringCompare"> <title>Operator == and <methodname>equals()</methodname></title> - <programlisting language="java">String s1 = new String("Kate"); + <informaltable border="0"> + <tr> + <td valign="top"><programlisting language="java">String s1 = new String("Kate"); String s2 = new String("Kate"); -System.out.println("s1 == s2: " + (s1 == s2)); -System.out.println("s1.equals(s2): " + s1.equals(s2));</programlisting> +System.out.println("==: " + (s1 == s2)); +System.out.println("equals: " + s1.equals(s2));</programlisting></td> + + <td valign="top"><programlisting language="java">int a = 12; +int b = 12; + +System.out.println("==: " + (a == b)); +// No equals(...) equivalent for +// primitive types</programlisting></td> + </tr> + + <tr> + <td valign="top"><screen>==: false +equals: true</screen></td> + + <td valign="top"><screen>==: true</screen></td> + </tr> + </informaltable> + </figure> + + <figure xml:id="sd1_coreclasses_fig_operatorEqualVsMathOperator"> + <title>Remarks <code language="java">==</code> vs. <code + language="java">equals()</code></title> + + <itemizedlist> + <listitem> + <para>The <code language="java">==</code> operator acting on primitive + types compares values of expressions.</para> + </listitem> - <screen>s1 == s2: false -s1.equals(s2): true</screen> + <listitem> + <para>The <code language="java">==</code> operator acting on objects + compares for object identity.</para> + </listitem> + + <listitem> + <para>The <methodname + xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html#equals(java.lang.Object)">equals()</methodname> + method compares two object's values.</para> + </listitem> + </itemizedlist> </figure> <figure xml:id="sd1_coreclasses_fig_hashPrinciple"> @@ -1004,11 +1042,12 @@ 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><code language="java">a1</code> and <code + language="java">a2</code> thus likely contain an identical + object reference. Using the <methodname xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/String.html#equals(java.lang.Object)">equals()</methodname> - method for comparison.</para> + method for comparison indeed returns <code + language="java">true</code>.</para> </callout> <callout arearefs="answerCoStringOperatorEquality-2-co" diff --git a/Doc/Sd1/objectsClasses.xml b/Doc/Sd1/objectsClasses.xml index cf78695f985fd8eb1d4bebb545c2adb857c4ed8b..fcb37c0c269a31d3dc00acc0bc162531d233c1c9 100644 --- a/Doc/Sd1/objectsClasses.xml +++ b/Doc/Sd1/objectsClasses.xml @@ -203,11 +203,11 @@ r.width = 28; // Runtime error: NullPointerException (NPE)</programlisting> </figure> <figure xml:id="sd1_fig_checkObjectPresent"> - <title>References and <code language="java">null</code></title> + <title>Checking for object presence</title> <programlisting language="java">Rectangle r; -... +... // possible object assignment to variable r. if (null == r) { System.out.println("No rectangle on offer"); diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index 765612889855de14d6eaca2cecd09b7ea251c788..2f9362f1663b4bb3098e5846c756469e06484cd6 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -323,12 +323,8 @@ Found: <emphasis role="red">int</emphasis></screen> </question> <answer> - <para>The compiler complains about incompatible types: An <code - language="java">int</code> is being found but a <code - language="java">boolean</code> to be expected.</para> - - <para>Java provides two <emphasis>completely</emphasis> unrelated - operators:</para> + <para>Java provides two seemingly similar but + <emphasis>completely</emphasis> unrelated operators:</para> <glosslist> <glossentry> @@ -385,7 +381,8 @@ Found: <emphasis role="red">int</emphasis></screen> <glossdef> <para>This is the comparison operator in <xref linkend="glo_Java"/> matching the usual math semantics - comparing two objects for identity.</para> + comparing elementary types for equality and objects for + identity.</para> </glossdef> </glossentry> </glosslist> @@ -1080,7 +1077,8 @@ Sorry, invalid choice</screen> linkend="glo_Java"/> applications asking for user input simply use the following recipe to get started:</para> - <programlisting language="java">public static void main(String[] args) { + <programlisting language="java" + xml:id="sd1_listing_scannerBoilerplate">public static void main(String[] args) { try (final Scanner scan = new Scanner(System.in)) { // Creating a scanner for reading user input @@ -1161,11 +1159,18 @@ public class BarOrder { <screen>Enter a number:>9 IX</screen> - <para>If the user enters a value smaller than 1 or greater than - ten the following output is to be expected:</para> + <para>Regarding user input you may start from <xref + linkend="sd1_listing_scannerBoilerplate"/> again. If the user + enters a value smaller than one or greater than ten the following + output is to be expected:</para> <screen>Enter a number:>11 Decimal value 11 not yet implemented</screen> + + <tip> + <para>You may use a series of <code language="java">if () {...} + else if () {...} ...</code> statements.</para> + </tip> </question> <answer> @@ -1254,7 +1259,7 @@ Decimal value 11 not yet implemented</screen> </figure> <figure xml:id="sd1_fig_dayNumber2NameByIfElseif"> - <title>Implementing day's number to name</title> + <title>Implementing number to day's names</title> <programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { System.out.print("Enter a weekday number (1=Monday, 2=Tuesday,...) : "); @@ -1264,7 +1269,6 @@ Decimal value 11 not yet implemented</screen> System.out.println("Monday"); } else if (2 == number) { System.out.println("Tuesday"); - ... } else if (7 == number) { @@ -1576,7 +1580,7 @@ Unknown day name 'July'</screen> <para>You may read strings into your application using <classname xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html">Scanner</classname>.<methodname - xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html#next()">next()</methodname>;</para> + xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html#next()">next()</methodname>.</para> </listitem> </orderedlist> </tip> @@ -1662,7 +1666,9 @@ Midweek</screen> <tip> <para>Sometimes omitting <code language="java">break</code> - statements allowing for fall-through is your friend.</para> + statements allowing for <link + xlink:href="https://www.dummies.com/programming/java/how-to-use-fall-through-to-your-advantage-in-java-programming">fall-through</link> + is your friend.</para> </tip> </question> @@ -1741,7 +1747,7 @@ Year 1980 is a leap year</screen> Year 1900 is no leap year</screen> <para>You may reuse the user input handling code from the previous - example .</para> + examples.</para> <tip> <para>Read about the <link @@ -1800,6 +1806,19 @@ public class LeapYear { System.out.println("Year " + year + " is no leap year"); } }</programlisting> + + <para>Some enthusiasts prefer compact expressions at the expense + of readability (»Geek syndrome«). The following code is fully + equivalent albeit syntactically different:</para> + + <programlisting language="java">final Scanner scan = new Scanner(System.in); // Read user input +System.out.print("Enter a year:>"); +final int year = scan.nextInt(); + +System.out.println("Year " + year + + (year % 400 == 0 || year % 4 == 0 && 0 != year % 100 ? " is a leap year" : " is no leap year")); + +scan.close();</programlisting> </answer> </qandaentry> </qandadiv> @@ -2260,8 +2279,8 @@ for (int i = 0; i < limit; i += 2) { System.out.println(i); }</programlisting> - <para>Even the original code could have been modified in this - fashion:</para> + <para>The original code could have been modified in this fashion + as well:</para> <programlisting language="java">final int limit = 14; int i = 0; @@ -2326,8 +2345,8 @@ while (i < limit) { <tip> <orderedlist> <listitem> - <para>The following code snippet printing a triangle may - serve as a starting point:</para> + <para>Copy and execute the following code snippet printing + a triangle for the sake of a starting point:</para> <informaltable border="1"> <colgroup width="47%"/> @@ -2460,7 +2479,11 @@ for (int row = 0; row < numberOfRows; row++) { <co <para>Hint: You will need a second loop inside the <quote>outer</quote> loop for creating spaces using the - <code language="java">' '</code> character.</para> + <code language="java">' '</code> character. The number of + spaces depends on the line you are printing. In the + current example you'll need 6 spaces for the first line, 5 + spaces for the second and so on. Thus the inner loop + depends on the line number to be printed.</para> </listitem> </orderedlist> </tip> @@ -2584,8 +2607,8 @@ for (int row = 0; row < numberOfRows; row++) { <qandadiv> <qandaentry> <question> - <para>Producing the following ASCII art for configurable Xmas - tree sizes is more challenging :</para> + <para>The following ASCII art for configurable Xmas tree sizes + is more challenging :</para> <screen> \ / -->*<-- @@ -2698,7 +2721,7 @@ for (int row = 0; row < numberOfRows; row++) { <para>So far we have not yet introduced methods. In anticipation of <link linkend="sd1SectStaticMembers">upcoming lessons</link> we provide an alternate solution by introducing a method - <methodname>printIndented(...)</methodname> which prints a + <methodname>printIndented(...)</methodname> which prints a given string a configurable number of times being indented by a given number of whitespace characters using <package xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/System.html">System</package>.<parameter @@ -2869,8 +2892,7 @@ for (int row = 0; row < numberOfRows; row++) { <answer> <para>The table head my be copied from the code snippet. In - addition we require a loop for printing the table's - body:</para> + addition we require a loop printing the table's body:</para> <programlisting language="java">public static void main(String[] args) { @@ -3177,7 +3199,7 @@ for (int row = 0; row < numberOfRows; row++) { <qandaentry> <question> <para>The last square number table solution is only - appropriate for smaller amounts of data. Growing number of + appropriate for smaller amounts of data. Growing numbers of elements require rearranging values in blocks in order to limit wasting space:</para> @@ -3205,7 +3227,7 @@ for (int row = 0; row < numberOfRows; row++) { 18 | 324 38 | 1444 58 | 3364 78 | 6084 98 | 9604 19 | 361 39 | 1521 59 | 3481 79 | 6241 99 | 9801</screen> - <para>Building a table this way requires some + <para>Building a table this way requires additional parameters:</para> <itemizedlist> @@ -3283,7 +3305,21 @@ for (int row = 0; row < numberOfRows; row++) { xlink:href="/Sd1/Ref/Src/squareTable.html">squareTable.html</link>. Inspect this generated HTML 5 code and modify your console output from the previous exercise in order to produce - identical or similar HTML code.</para> + identical or similar HTML code. The expected outcome + is:</para> + + <screenshot> + <info> + <title>A <xref linkend="glo_HTML"/> square table till + 20*20.</title> + </info> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/squareTable.png"/> + </imageobject> + </mediaobject> + </screenshot> <para>Depending on your advance in HTML and <xref linkend="glo_CSS"/> you may want to postpone this exercise @@ -3321,9 +3357,8 @@ for (int row = 0; row < numberOfRows; row++) { <answer> <para>The following solution uses <xref linkend="glo_CSS"/> - style definitions in the generated HTML's header section which - are subsequently being referenced from the document's - body.</para> + style definitions in the generated HTML's header section being + referenced from the document's body.</para> <programlisting language="java"> public static void main(String[] args) { @@ -3384,22 +3419,6 @@ for (int row = 0; row < numberOfRows; row++) { System.out.print("" + " </table>\n" + " </body>\n" + "</html>\n"); }</programlisting> - - <para>Viewing the generated <xref linkend="glo_HTML"/> results - in:</para> - - <screenshot> - <info> - <title>A <xref linkend="glo_HTML"/> square table till - 20*20.</title> - </info> - - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/squareTable.png"/> - </imageobject> - </mediaobject> - </screenshot> </answer> </qandaentry> </qandadiv> @@ -3849,8 +3868,9 @@ for (int i = 1; i < 50; i++) { <code xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html#MAX_VALUE">Integer.MAX_VALUE</code> == 2147483647 giving rise to a (silent) arithmetic overflow. - But even declaring <code language="java">long product = - 1</code> does not help much:</para> + But even declaring our variable <code + language="java">product</code> of type <code + language="java">long</code> does not help much:</para> <screen>1! == 1 2! == 2 @@ -4407,8 +4427,8 @@ for (int i = 1; i < 50; i++) { <para>Your program will select an integer (pseudo) random number between zero and an exclusive configurable fixed upper - limit e.g. 10. The user will have an again configurable number - of tries to guess the number in question. Each time after + limit e.g. 10. The user will have a configurable number of + tries to guess the number in question. Each time after entering a number your program will respond with either of the following:</para> @@ -4471,10 +4491,6 @@ import java.util.Random; final int randomValue = new Random() // Selecting a pseudo random value .<link xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Random.html#nextInt()">nextInt(10)</link>; // between 0 and 10 (inclusive). - final Scanner scan = new Scanner(System.in); - System.out.print("Input a number:"); - final int value = scan.nextInt(); // Read user input - // ToDo: complete the implementation }</programlisting> </question> @@ -4506,7 +4522,6 @@ try (final Scanner scan = new Scanner(System.in)) { break; } } - if (numberWasFound) { System.out.println("Congratulations, you won!"); } else { @@ -4528,7 +4543,7 @@ try (final Scanner scan = new Scanner(System.in)) { <qandaentry> <question> <para>2520 is the smallest number that can be divided by each - of the numbers from 1 to 10 without any remainder.</para> + of the numbers from 2 to 10 without any remainder.</para> <para>What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?</para> @@ -5617,7 +5632,7 @@ try (final Scanner scan = new Scanner(System.in)) { </question> <answer> - <para>Since each variable does have a value of at least 1 the + <para>Since each variable value must be greater than zero the maximum possible value is 840 - 1 - 1 = 838.</para> <para>The <quote>last</quote> triple obeying both restrictions @@ -5670,8 +5685,8 @@ try (final Scanner scan = new Scanner(System.in)) { <para>We thus only need two nested loops for generating all possible triples: If the value of b is being incremented by 1 - then c has to be decremented by 1 as well and is thus fully - dependent.</para> + then c has to be decremented by 1 as well and is thus + completely determined.</para> <para>For convenience reasons we introduce a variable <code language="java">int sum = 840</code> representing our sum of