diff --git a/Doc/Sd1/Inheritance/inheritance.xml b/Doc/Sd1/Inheritance/inheritance.xml index 3b6bb206a37ae38665900b180d55a96c753a30ce..896748445bd80a94c9d206c34fd321011f8da6c8 100644 --- a/Doc/Sd1/Inheritance/inheritance.xml +++ b/Doc/Sd1/Inheritance/inheritance.xml @@ -431,26 +431,25 @@ public <link </figure> <qandaset defaultlabel="qanda" xml:id="sd1_qanda_stringEqualProblem"> - <title>Let me in, please!</title> + <title>Let me pass, please!</title> <qandadiv> <qandaentry> <question> <para>Consider the following snippet:</para> - <programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { - do { - System.out.print("Please enter password: "); - final String password = scan.nextLine(); - if (password == "secret") { - break; // Leave enclosing do ... while loop - } else { - System.out.println("Sorry, please try again"); - } - } while (true); + <programlisting language="java">final Scanner scan = new Scanner(System.in)); +do { + System.out.print("Please enter password: "); + final String password = scan.nextLine(); + if (password == "secret") { + break; // Leave enclosing do ... while loop + } else { + System.out.println("Sorry, please try again"); + } +} while (true); System.out.println("You made it!"); - // ... -}</programlisting> + // ...</programlisting> <para>Describe the above code's intended behaviour. Will it succeed? Execute the above code and provide a clue for correcting @@ -476,41 +475,42 @@ public <link xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html">String</classname> object in memory as well.</para> - <para>Unfortunately the usual <quote>==</quote> operator only - works as expected for the eight built in primitive <xref - linkend="glo_Java"/> types. With respect to class instances a - variable holds a reference to an object rather than the object - itself. Applying the <quote>==</quote> operator compares for - object identity rather than object equality.</para> + <para>Unfortunately the <quote>==</quote> operator only works as + expected for the eight built in primitive <xref + linkend="glo_Java"/> types. With respect to class instances + variables hold references to objects rather than to the objects' + values. The <quote>==</quote> operator compares for object + identity rather than for object equality.</para> <para>Two different <classname xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html">String</classname> - instances albeit being different object instances may off course - be equal with respect to their <quote>payload</quote> namely the - strings they both represent. Comparing for object equality rather - than for object identity in <xref linkend="glo_Java"/> requires + instances may off course be equal with respect to their + <quote>payload</quote> namely the values they both represent. + Comparing for object equality rather than for object identity in + <xref linkend="glo_Java"/> requires using the + <methodname>equals(...)</methodname> method instead. A + prerequisite for this to work requires the class authors overriding the <methodname xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#equals(java.lang.Object)">Object.equals(Object - o)</methodname> method. This override does exist in class - <classname + o)</methodname> method accordingly. This override does exist in + class <classname xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html">String</classname>. Within the given context we may thus simply use <methodname xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html#equals(java.lang.Object)">String.equals(Object o)</methodname>:</para> - <programlisting language="none">try (final Scanner scan = new Scanner(System.in)) { - do { - System.out.print("Please enter password: "); - final String password = scan.nextLine(); - <emphasis role="red">if (password.equals("secret"))</emphasis> { - break; // Leave enclosing do ... while loop - } else { - System.out.println("Sorry, please try again"); - } - } while (true); - System.out.println("You made it!"); - // ... -}</programlisting> + <programlisting language="none">final Scanner scan = new Scanner(System.in)); +do { + System.out.print("Please enter password: "); + final String password = scan.nextLine(); + <emphasis role="red">if (password.equals("secret"))</emphasis> { + break; // Leave enclosing do ... while loop + } else { + System.out.println("Sorry, please try again"); + } +} while (true); +System.out.println("You made it!"); +// ...</programlisting> </answer> </qandaentry> </qandadiv> diff --git a/Doc/Sd1/Ref/WorkingWithNumbers/P/src/main/java/de/hdm_stuttgart/sd1/EnterIntValue.java b/Doc/Sd1/Ref/WorkingWithNumbers/P/src/main/java/de/hdm_stuttgart/sd1/EnterIntValue.java index 60a125b6a816003fc66b6b0f34143f99b0cb6b3c..2b12b3ee7eddcc50c3b18adbf01ededa795ce6df 100644 --- a/Doc/Sd1/Ref/WorkingWithNumbers/P/src/main/java/de/hdm_stuttgart/sd1/EnterIntValue.java +++ b/Doc/Sd1/Ref/WorkingWithNumbers/P/src/main/java/de/hdm_stuttgart/sd1/EnterIntValue.java @@ -3,7 +3,6 @@ package de.hdm_stuttgart.sd1; import java.util.Scanner; public class EnterIntValue { - public static void main(String[] args) { String userInput = null; diff --git a/Doc/Sd1/Ref/WorkingWithNumbers/P/src/main/java/de/hdm_stuttgart/sd1/RandomExample.java b/Doc/Sd1/Ref/WorkingWithNumbers/P/src/main/java/de/hdm_stuttgart/sd1/RandomExample.java index d930a381fe783f8898319629bc27336353d39079..f5e66a135b08e29b6b8b3162897675f400c53f88 100644 --- a/Doc/Sd1/Ref/WorkingWithNumbers/P/src/main/java/de/hdm_stuttgart/sd1/RandomExample.java +++ b/Doc/Sd1/Ref/WorkingWithNumbers/P/src/main/java/de/hdm_stuttgart/sd1/RandomExample.java @@ -7,20 +7,14 @@ public class RandomExample { public static void main(String[] args) { - try(final Scanner scanner = new Scanner(System.in)) { System.out.print("Enter an integer seed:"); final long seed = scanner.nextLong(); - Random generator = new Random(seed); + final Random generator = new Random(seed); for (int i = 0; i < 10; i++) { System.out.print(generator.nextBoolean() + " "); } } - } - - - - } diff --git a/Doc/Sd1/Statements/statements.xml b/Doc/Sd1/Statements/statements.xml index c8e5ed8d441ee289dbb7551425c7c4e571b1d87a..3b5841559a0f762288f6dee8b718af37f9b98b12 100644 --- a/Doc/Sd1/Statements/statements.xml +++ b/Doc/Sd1/Statements/statements.xml @@ -851,19 +851,16 @@ else <tr> <td valign="top"><programlisting language="java">import java.util.Scanner; + public class App { public static void main(String[] args){ - try (final Scanner scan = - new Scanner(System.in)) { - - System.out.print("Enter a value:"); - - final int value = scan.nextInt(); - - System.out.println( - "You entered " + value); - } + final Scanner scan = + new Scanner(System.in)); + System.out.print("Enter a value:"); + final int value = scan.nextInt(); + System.out.println("You entered " + + value); } }</programlisting></td> @@ -1239,34 +1236,33 @@ Decimal value 11 not yet implemented</screen> </question> <answer> - <para><programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { - - System.out.print("Enter a number:>"); - final int number = scan.nextInt(); - - if (1 == number) { - System.out.println("I"); - } else if (2 == number) { - System.out.println("II"); - } else if (3 == number) { - System.out.println("III"); - } else if (4 == number) { - System.out.println("IV"); - } else if (5 == number) { - System.out.println("V"); - } else if (6 == number) { - System.out.println("VI"); - } else if (7 == number) { - System.out.println("VII"); - } else if (8 == number) { - System.out.println("VIII"); - } else if (9 == number) { - System.out.println("IX"); - } else if (10 == number) { - System.out.println("X"); - } else { - System.out.println("Decimal value " + number + " not yet implemented"); - } + <para><programlisting language="java">final Scanner scan = new Scanner(System.in)); + +System.out.print("Enter a number:>"); +final int number = scan.nextInt(); + +if (1 == number) { + System.out.println("I"); +} else if (2 == number) { + System.out.println("II"); +} else if (3 == number) { + System.out.println("III"); +} else if (4 == number) { + System.out.println("IV"); +} else if (5 == number) { + System.out.println("V"); +} else if (6 == number) { + System.out.println("VI"); +} else if (7 == number) { + System.out.println("VII"); +} else if (8 == number) { + System.out.println("VIII"); +} else if (9 == number) { + System.out.println("IX"); +} else if (10 == number) { + System.out.println("X"); +} else { + System.out.println("Decimal value " + number + " not yet implemented"); }</programlisting></para> </answer> </qandaentry> @@ -1335,22 +1331,21 @@ Decimal value 11 not yet implemented</screen> <figure xml:id="sd1_fig_dayNumber2NameByIfElseif"> <title>Numbers to day's names: The hard way</title> - <programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { - System.out.print("Enter a weekday number (1=Monday, 2=Tuesday,...) : "); + <programlisting language="java">final Scanner scan = new Scanner(System.in)); +System.out.print("Enter a weekday number (1=Monday, 2=Tuesday,...) : "); - final int number = scan.nextInt(); +final int number = scan.nextInt(); - if (1 == number) { - System.out.println("Monday"); - } else if (2 == number) { - System.out.println("Tuesday"); +if (1 == number) { + System.out.println("Monday"); +} else if (2 == number) { + System.out.println("Tuesday"); ... - } else if (7 == number) { - System.out.println("Sunday"); - } else { - System.out.println("Invalid number " + number); - } +} else if (7 == number) { + System.out.println("Sunday"); +} else { + System.out.println("Invalid number " + number); }</programlisting> </figure> @@ -1390,24 +1385,23 @@ import java.util.Scanner; public class LeapYear { - public static void main(String[] args) { + public static void main(String[] args) { - try (final Scanner scan = new Scanner(System.in)) { + final Scanner scan = new Scanner(System.in)); - System.out.print("Enter a year:>"); - final int year = scan.nextInt(); + System.out.print("Enter a year:>"); + final int year = scan.nextInt(); - if (0 == year % 400) { <emphasis role="bold"> // Every 400 years we do have a leap year.</emphasis> - System.out.println( - "Year " + year + " is a leap year"); - } else if (0 == year % 4 && <emphasis role="bold"> // Every 4 years we do have a leap year</emphasis> - 0 != year % 100) { <emphasis role="bold"> // unless year is a multiple of 100.</emphasis> - System.out.println("Year " + year + " is a leap year"); - } else { - System.out.println("Year " + year + " is no leap year"); - } + if (0 == year % 400) { <emphasis role="bold"> // Every 400 years we do have a leap year.</emphasis> + System.out.println( + "Year " + year + " is a leap year"); + } else if (0 == year % 4 && <emphasis role="bold"> // Every 4 years we do have a leap year</emphasis> + 0 != year % 100) { <emphasis role="bold"> // unless year is a multiple of 100.</emphasis> + System.out.println("Year " + year + " is a leap year"); + } else { + System.out.println("Year " + year + " is no leap year"); } - } + } }</programlisting> <para>This solution contains two identical <code @@ -1419,10 +1413,10 @@ public class LeapYear { <programlisting language="java">public static void main(String[] args) { ... - if (0 == year % 400 || <emphasis role="bold">// Every 400 years we do have a leap year.</emphasis> + if (0 == year % 400 || <emphasis role="bold">// Every 400 years we do have a leap year.</emphasis> (0 == year % 4 && <emphasis role="bold">// Every 4 years we do have a leap year</emphasis> 0 != year % 100)) { <emphasis role="bold">// unless year is a multiple of 100.</emphasis> - System.out.println("Year " + year + " is a leap year"); + System.out.println("Year " + year + " is a leap year"); } else { System.out.println("Year " + year + " is no leap year"); } @@ -1509,20 +1503,19 @@ Saturday</screen> <para><xref linkend="sd1_fig_dayNumber2NameByIfElseif"/> can be rewritten as:</para> - <programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { - System.out.print("Enter a weekday number (1=Monday, 2=Tuesday,...) : "); - final int number = scan.nextInt(); - switch(number) { - case 1: System.out.println("Monday"); - case 2: System.out.println("Tuesday"); - case 3: System.out.println("Wednesday"); - case 4: System.out.println("Thursday"); - case 5: System.out.println("Friday"); - case 6: System.out.println("Saturday"); - case 7: System.out.println("Sunday"); - - default: System.out.println("Invalid number " + number); - } + <programlisting language="java">final Scanner scan = new Scanner(System.in)); +System.out.print("Enter a weekday number (1=Monday, 2=Tuesday,...) : "); +final int number = scan.nextInt(); +switch(number) { + case 1: System.out.println("Monday"); + case 2: System.out.println("Tuesday"); + case 3: System.out.println("Wednesday"); + case 4: System.out.println("Thursday"); + case 5: System.out.println("Friday"); + case 6: System.out.println("Saturday"); + case 7: System.out.println("Sunday"); + + default: System.out.println("Invalid number " + number); }</programlisting> <para>Entering a day's number 5 yields:</para> @@ -1765,20 +1758,19 @@ Unknown day name 'July'</screen> </question> <answer> - <programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { - System.out.print("Enter a weekday (Monday to Sunday):>"); - final String day = scan.next(); - switch(day) { - case "Monday": System.out.println(1); break; - case "Tuesday": System.out.println(2); break; - case "Wednesday": System.out.println(3); break; - case "Thursday": System.out.println(4); break; - case "Friday": System.out.println(5); break; - case "Saturday": System.out.println(6); break; - case "Sunday": System.out.println(7); break; - default: System.out.println("Unknown day name '" + day + "'"); - break; - } + <programlisting language="java">final Scanner scan = new Scanner(System.in)); +System.out.print("Enter a weekday (Monday to Sunday):>"); +final String day = scan.next(); +switch(day) { + case "Monday": System.out.println(1); break; + case "Tuesday": System.out.println(2); break; + case "Wednesday": System.out.println(3); break; + case "Thursday": System.out.println(4); break; + case "Friday": System.out.println(5); break; + case "Saturday": System.out.println(6); break; + case "Sunday": System.out.println(7); break; + default: System.out.println("Unknown day name '" + day + "'"); + break; }</programlisting> </answer> </qandaentry> @@ -1851,23 +1843,22 @@ Midweek</screen> </question> <answer> - <programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { - System.out.print("Enter a weekday (Monday to Sunday):>"); - final String day = scan.next(); - switch(day) { - case "Monday": System.out.println("Start of work week"); break; + <programlisting language="java">final Scanner scan = new Scanner(System.in)); +System.out.print("Enter a weekday (Monday to Sunday):>"); +final String day = scan.next(); +switch(day) { + case "Monday": System.out.println("Start of work week"); break; - case "Tuesday": - case "Wednesday": - case "Thursday": System.out.println("Midweek"); break; + case "Tuesday": + case "Wednesday": + case "Thursday": System.out.println("Midweek"); break; - case "Friday": System.out.println("End of work week"); break; + case "Friday": System.out.println("End of work week"); break; - case "Saturday": - case "Sunday": System.out.println("Weekend"); break; + case "Saturday": + case "Sunday": System.out.println("Weekend"); break; - default: System.out.println("Unknown day name " + day); break; - } + default: System.out.println("Unknown day name " + day); break; }</programlisting> </answer> </qandaentry> @@ -1886,26 +1877,25 @@ Midweek</screen> </question> <answer> - <para><programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { - - System.out.print("Enter a number:>"); - final int number = scan.nextInt(); - - switch (number) { - case 1: System.out.println("I"); break; - case 2: System.out.println("II"); break; - case 3: System.out.println("III"); break; - case 4: System.out.println("IV"); break; - case 5: System.out.println("V"); break; - case 6: System.out.println("VI"); break; - case 7: System.out.println("VII"); break; - case 8: System.out.println("VIII"); break; - case 9: System.out.println("IX"); break; - case 10: System.out.println("X"); break; - - default:System.out.println("Decimal value " + number + " not yet implemented"); - break; - } + <para><programlisting language="java">final Scanner scan = new Scanner(System.in)); + +System.out.print("Enter a number:>"); +final int number = scan.nextInt(); + +switch (number) { + case 1: System.out.println("I"); break; + case 2: System.out.println("II"); break; + case 3: System.out.println("III"); break; + case 4: System.out.println("IV"); break; + case 5: System.out.println("V"); break; + case 6: System.out.println("VI"); break; + case 7: System.out.println("VII"); break; + case 8: System.out.println("VIII"); break; + case 9: System.out.println("IX"); break; + case 10: System.out.println("X"); break; + + default:System.out.println("Decimal value " + number + " not yet implemented"); + break; }</programlisting></para> </answer> </qandaentry> @@ -2183,20 +2173,19 @@ while (counter++ < 10) { <answer> <programlisting language="java">public static void main(String[] args) { - try (final Scanner scan = new Scanner(System.in)) { + final Scanner scan = new Scanner(System.in)); - System.out.print("Enter an integer value: "); - final int value = scan.nextInt(); + System.out.print("Enter an integer value: "); + final int value = scan.nextInt(); - long factorial = 1; - int i = 1; - - while (i++ < value) { - factorial *= i; - } + long factorial = 1; + int i = 1; - System.out.println(value + "! == " + factorial); + while (i++ < value) { + factorial *= i; } + + System.out.println(value + "! == " + factorial); }</programlisting> </answer> </qandaentry> @@ -2285,7 +2274,7 @@ Goodbye!</screen>A <code language="java">do ... while(...)</code> rather than a <code language="java">while(...)</code> loop is thus appropriate. It allows for entering the loop's body <emphasis role="bold">before</emphasis> any check is about to - happen:<programlisting language="java">try(final Scanner scanner = new Scanner(System.in)){ + happen:<programlisting language="java">final Scanner scanner = new Scanner(System.in)); int userInput; do { @@ -2456,21 +2445,20 @@ It's square is 1.9999999999999996</screen> </m:math> </inlineequation> respectively:</para> - <programlisting language="java">try (final Scanner scan = new Scanner(System.in)) { + <programlisting language="java">final Scanner scan = new Scanner(System.in)); - System.out.print("Enter a non-negative value: "); - final double a = scan.nextDouble(); +System.out.print("Enter a non-negative value: "); +final double a = scan.nextDouble(); - double x_next = a / 2, x_current; +double x_next = a / 2, x_current; - do { - x_current = x_next; // Save current approximation value - x_next = (x_current + a / x_current) / 2; // Calculate next series value - } while (x_next != x_current); // Did we get any closer? +do { + x_current = x_next; // Save current approximation value + x_next = (x_current + a / x_current) / 2; // Calculate next series value +} while (x_next != x_current); // Did we get any closer? - System.out.println("The square root of " + a + " is close to " + x_next); - System.out.println("It's square is " + x_next * x_next); -}</programlisting> +System.out.println("The square root of " + a + " is close to " + x_next); +System.out.println("It's square is " + x_next * x_next);</programlisting> </answer> </qandaentry> </qandadiv> @@ -5455,26 +5443,26 @@ System.out.println("Try to guess my secret number in between 0 and " System.out.println("You have " + numOfUserAttempts + " attempts"); -try (final Scanner scan = new Scanner(System.in)) { - boolean numberWasFound = false; +final Scanner scan = new Scanner(System.in)); +boolean numberWasFound = false; - for (int i = 0; i < numOfUserAttempts; i++) { - System.out.print("Input your guess:"); - final int userGuess = scan.nextInt(); - if (userGuess < randomValue) { - System.out.println("Number is too low"); - } else if (randomValue < userGuess) { - System.out.println("Number is too high"); - } else { - numberWasFound = true; - break; - } - } - if (numberWasFound) { - System.out.println("Congratulations, you won!"); - } else { - System.out.println("Game over, try another run?"); - } +for (int i = 0; i < numOfUserAttempts; i++) { + System.out.print("Input your guess:"); + final int userGuess = scan.nextInt(); + if (userGuess < randomValue) { + System.out.println("Number is too low"); + } else if (randomValue < userGuess) { + System.out.println("Number is too high"); + } else { + numberWasFound = true; + break; + } +} + +if (numberWasFound) { + System.out.println("Congratulations, you won!"); +} else { + System.out.println("Game over, try another run?"); }</programlisting> <para>In case you don't like <code>break</code> statements the