From 8db4d2e3bb8bddfff3d4e3a042676b98fdf47dab Mon Sep 17 00:00:00 2001 From: "Dr. Martin Goik" <goik@hdm-stuttgart.de> Date: Wed, 24 Jun 2020 15:38:15 +0200 Subject: [PATCH] Leap year exercise frpm 'switch' to 'if' section --- Doc/Sd1/statements.xml | 181 +++++++++++++++++++++-------------------- Doc/Sdi/apache.xml | 9 +- 2 files changed, 97 insertions(+), 93 deletions(-) diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index 60fd75d6f..a17bb46b2 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -1488,6 +1488,97 @@ Decimal value 11 not yet implemented</screen> } }</programlisting> </figure> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaLeapYear"> + <title>Leap years</title> + + <qandadiv> + <qandaentry> + <question> + <para>We want to write an application telling whether a given + year is a leap year or not. The following dialogue may serve as + an example:</para> + + <screen>Enter a year:>1980 +Year 1980 is a leap year</screen> + + <screen>Enter a year:>1900 +Year 1900 is no leap year</screen> + + <para>You may reuse the user input handling code from the + previous examples.</para> + + <tip> + <para>Read about the <link + xlink:href="https://en.wikipedia.org/wiki/Leap_year#Algorithm">leap + year algorithm</link>.</para> + </tip> + </question> + + <answer> + <para>A first straightforward rule translation based solution + reads:</para> + + <programlisting language="java">package start; + +import java.util.Scanner; + +public class LeapYear { + + public static void main(String[] args) { + + try (final Scanner scan = new Scanner(System.in)) { + + 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"); + } + } + } +}</programlisting> + + <para>This solution contains two identical <code + language="java">println("Year " + year + " is a leap + year")</code> statements. Developers don't favour redundancies: + Rearranging and combining the first and third <code + language="java">if</code> branch into one resolves the + issue:</para> + + <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> + (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>Some enthusiasts prefer compact expressions at the expense + of readability (<quote>Geek syndrome</quote>) sometimes referred + to as <quote>syntactic sugar</quote>. The following code based + on the <code language="java" + xlink:href="https://www.geeksforgeeks.org/java-ternary-operator-with-examples">...? + ...: ...</code> operator is fully equivalent:</para> + + <programlisting language="java">... + +System.out.println("Year " + year + + (year % 400 == 0 || year % 4 == 0 && 0 != year % 100 ? " is a leap year" : " is no leap year")); +}</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> </section> </section> @@ -1955,96 +2046,6 @@ Midweek</screen> </qandaentry> </qandadiv> </qandaset> - - <qandaset defaultlabel="qanda" xml:id="sd1QandaLeapYear"> - <title>Leap years</title> - - <qandadiv> - <qandaentry> - <question> - <para>We want to write an application telling whether a given year - is a leap year or not. The following dialogue may serve as an - example:</para> - - <screen>Enter a year:>1980 -Year 1980 is a leap year</screen> - - <screen>Enter a year:>1900 -Year 1900 is no leap year</screen> - - <para>You may reuse the user input handling code from the previous - examples.</para> - - <tip> - <para>Read about the <link - xlink:href="https://en.wikipedia.org/wiki/Leap_year#Algorithm">leap - year algorithm</link>.</para> - </tip> - </question> - - <answer> - <para>A first straightforward rule translation based solution - reads:</para> - - <programlisting language="java">package start; - -import java.util.Scanner; - -public class LeapYear { - - public static void main(String[] args) { - - try (final Scanner scan = new Scanner(System.in)) { - - 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"); - } - } - } -}</programlisting> - - <para>This solution contains two identical <code - language="java">println("Year " + year + " is a leap year")</code> - statements. Developers don't favour redundancies: Rearranging and - combining the first and third <code language="java">if</code> - branch into one resolves the issue:</para> - - <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> - (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>Some enthusiasts prefer compact expressions at the expense - of readability (<quote>Geek syndrome</quote>) sometimes referred - to as <quote>syntactic sugar</quote>. The following code based on - the <code language="java" - xlink:href="https://www.geeksforgeeks.org/java-ternary-operator-with-examples">...? - ...: ...</code> operator is fully equivalent:</para> - - <programlisting language="java">... - -System.out.println("Year " + year + - (year % 400 == 0 || year % 4 == 0 && 0 != year % 100 ? " is a leap year" : " is no leap year")); -}</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> </section> <section xml:id="sd1_sect_loops"> diff --git a/Doc/Sdi/apache.xml b/Doc/Sdi/apache.xml index 0620d25df..6f3ecc7af 100644 --- a/Doc/Sdi/apache.xml +++ b/Doc/Sdi/apache.xml @@ -1,7 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> -<chapter version="5.1" xml:id="sdiApache" - annotations="slide" - xmlns="http://docbook.org/ns/docbook" +<chapter annotations="slide" version="5.1" xml:id="sdiApache" + xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes" xmlns:xi="http://www.w3.org/2001/XInclude" @@ -323,6 +322,10 @@ </listitem> </orderedlist> + <para>It fully suffices to get the <productname>firefox</productname> + browser working this way. <productname>Google-Chrome</productname> is + known for additional security restrictions.</para> + <para>The following docs may help you:</para> <tip> -- GitLab