From de31c19f788a49470ed8501cbac64e15e178d72b Mon Sep 17 00:00:00 2001
From: Martin Goik <goik@hdm-stuttgart.de>
Date: Sat, 26 Apr 2014 16:33:23 +0200
Subject: [PATCH] Secon leap year variant, minor extension to MathTable

---
 .../hdm_stuttgart/de/sd1/leap/LeapYear.java   |  5 ++--
 .../de/sd1/leap/LeapYearCompact.java          | 24 +++++++++++++++++
 .../de/sd1/rounding/MathTable.java            | 26 ++++++++++---------
 Sd1/swd1.xml                                  | 24 +++++++++++++++++
 4 files changed, 65 insertions(+), 14 deletions(-)
 create mode 100644 Sd1/P/rounding/src/main/java/de/hdm_stuttgart/de/sd1/leap/LeapYearCompact.java

diff --git a/Sd1/P/rounding/src/main/java/de/hdm_stuttgart/de/sd1/leap/LeapYear.java b/Sd1/P/rounding/src/main/java/de/hdm_stuttgart/de/sd1/leap/LeapYear.java
index ed9ab7c84..bdaecebbc 100644
--- a/Sd1/P/rounding/src/main/java/de/hdm_stuttgart/de/sd1/leap/LeapYear.java
+++ b/Sd1/P/rounding/src/main/java/de/hdm_stuttgart/de/sd1/leap/LeapYear.java
@@ -16,9 +16,10 @@ public class LeapYear {
    * @return true if year is a leap year, false otherwise.
    */
   public static boolean isLeapYear(int year) {
-     if (year % 400 == 0 || 
-         year % 4 == 0 &&  0 != year % 200){
+     if (year % 400 == 0) {                           // Every 400 years we do have a leap year.
        return true;
+     } else if (year % 4 == 0 &&  0 != year % 200) {  // Every 4 years we do have a leap year unless the year
+       return true;                                   // in question is a multiple of 100.
      } else {
        return false;
      }
diff --git a/Sd1/P/rounding/src/main/java/de/hdm_stuttgart/de/sd1/leap/LeapYearCompact.java b/Sd1/P/rounding/src/main/java/de/hdm_stuttgart/de/sd1/leap/LeapYearCompact.java
new file mode 100644
index 000000000..4a9b8b1a8
--- /dev/null
+++ b/Sd1/P/rounding/src/main/java/de/hdm_stuttgart/de/sd1/leap/LeapYearCompact.java
@@ -0,0 +1,24 @@
+package de.hdm_stuttgart.de.sd1.leap;
+
+public class LeapYearCompact {
+
+  public static void main(String[] args) {
+    System.out.println("Is 1800 a leap year? " + isLeapYear(1800));
+    System.out.println("Is 2000 a leap year? " + isLeapYear(2000));
+    System.out.println("Is 2016 a leap year? " + isLeapYear(2016));
+  }
+
+  /**
+   * Characterizing a given year either as leap year or
+   * non- leap year
+   * 
+   * @param year The year in question.
+   * @return true if year is a leap year, false otherwise.
+   */
+  public static boolean isLeapYear(int year) {
+    return
+        year % 400 == 0 ||                  // Every 400 years we do have a leap year.
+        year % 4 == 0 &&  0 != year % 200;  // Every 4 years we do have a leap year unless the year
+                                            // in question is a multiple of 100.
+  }  
+}
diff --git a/Sd1/P/rounding/src/main/java/de/hdm_stuttgart/de/sd1/rounding/MathTable.java b/Sd1/P/rounding/src/main/java/de/hdm_stuttgart/de/sd1/rounding/MathTable.java
index c2b35fc8b..e993ffb0c 100644
--- a/Sd1/P/rounding/src/main/java/de/hdm_stuttgart/de/sd1/rounding/MathTable.java
+++ b/Sd1/P/rounding/src/main/java/de/hdm_stuttgart/de/sd1/rounding/MathTable.java
@@ -12,10 +12,12 @@ public class MathTable {
     System.out.println("----+------");
     for (int i = 0; i < 360; i += 5) {
       
-      final long precision = 1000,// Rounding three decimal places
-          sinValueRounded = Math.round(precision * 
-                    Math.sin(i * Math.PI / 180)), // Turning degrees to radians
-          digit = sinValueRounded / precision;
+      final double
+        sinValue = Math.sin(i * Math.PI / 180); // Turning degrees into radians
+      final long
+        precision = 1000,// Rounding to three decimal places
+        sinValueRounded = Math.round(precision *  sinValue), 
+        digits = sinValueRounded / precision;
       
       long fraction = sinValueRounded % precision;
       
@@ -27,22 +29,22 @@ public class MathTable {
       
       if (fraction < 0) {
         fraction *= -1;
-        System.out.print(i + " |-" + digit + ".");
-      } else if (-1 < digit){
-        System.out.print(i + " | " + digit + ".");
+        System.out.print(i + " |-" + digits + ".");
+      } else if (-1 < digits){
+        System.out.print(i + " | " + digits + ".");
       } else {
-        System.out.print(i + " |" + digit + ".");
+        System.out.print(i + " |"  + digits + ".");
       }
       
-      if (fraction < 9) { // Padding fraction with zeros
-        System.out.print("00"); // One digit, add two zeros
+      if (fraction < 9) { // Padding fraction with zeros if required
+        System.out.print("00"); // One digit, add two leading zeros
       } else if (fraction < 99) {
-        System.out.print("0"); // Two digits, add two zeros
+        System.out.print("0"); // Two digits, add one leading zero
       } 
       
       System.out.println(fraction);
       
-      if (0 < i && 0 == i % 20) {
+      if (0 < i && 0 == i % 20) { // Add extra seperator every four lines
         System.out.println("----+------");
       }
     }
diff --git a/Sd1/swd1.xml b/Sd1/swd1.xml
index 5e386d1f8..13764545d 100644
--- a/Sd1/swd1.xml
+++ b/Sd1/swd1.xml
@@ -2663,6 +2663,30 @@ Is 2016 a leap year? true</programlisting>
               </question>
 
               <answer>
+                <para>A first solution may be:</para>
+
+                <programlisting language="java">  <link
+                    xlink:href="Ref/api/P/rounding/src-html/de/hdm_stuttgart/de/sd1/leap/LeapYear.html">public static boolean isLeapYear(int year)</link> {
+     if (year % 400 == 0) {                           // Every 400 years we do have a leap year.
+       return true;
+     } else if (year % 4 == 0 &amp;&amp;  0 != year % 200) {  // Every 4 years we do have a leap year unless the year
+       return true;                                   // in question is a multiple of 100.
+     } else {
+       return false;
+     }
+  }</programlisting>
+
+                <para>This one is easy to read. Experienced programmers
+                however prefer compact code:</para>
+
+                <programlisting language="java">  <link
+                    xlink:href="Ref/api/P/rounding/src-html/de/hdm_stuttgart/de/sd1/leap/LeapYearCompact.html">public static boolean isLeapYear(int year)</link> {
+    return
+        year % 400 == 0 ||                  // Every 400 years we do have a leap year.
+        year % 4 == 0 &amp;&amp;  0 != year % 200;  // Every 4 years we do have a leap year unless the year
+                                            // in question is a multiple of 100.
+  }  </programlisting>
+
                 <para>See <link
                 xlink:href="Ref/api/P/rounding/src-html/de/hdm_stuttgart/de/sd1/leap/LeapYear.html">LeapYear</link>.</para>
               </answer>
-- 
GitLab