Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
Dr. Martin Goik
GoikLectures
Commits
8db4d2e3
Commit
8db4d2e3
authored
Jun 24, 2020
by
Dr. Martin Goik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Leap year exercise frpm 'switch' to 'if' section
parent
1f2de764
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
97 additions
and
93 deletions
+97
-93
Doc/Sd1/statements.xml
Doc/Sd1/statements.xml
+91
-90
Doc/Sdi/apache.xml
Doc/Sdi/apache.xml
+6
-3
No files found.
Doc/Sd1/statements.xml
View file @
8db4d2e3
...
...
@@ -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"
>
...
...
Doc/Sdi/apache.xml
View file @
8db4d2e3
<?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>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment