Commit 8db4d2e3 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Leap year exercise frpm 'switch' to 'if' section

parent 1f2de764
......@@ -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:&gt;1980
Year 1980 is a leap year</screen>
<screen>Enter a year:&gt;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:&gt;");
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 &amp;&amp; <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 &amp;&amp; <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 &amp;&amp; 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:&gt;1980
Year 1980 is a leap year</screen>
<screen>Enter a year:&gt;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:&gt;");
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 &amp;&amp; <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 &amp;&amp; <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 &amp;&amp; 0 != year % 100 ? " is a leap year" : " is no leap year"));
}</programlisting>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
<section xml:id="sd1_sect_loops">
......
<?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>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment