From 62a70d9881d9d27a944b5e6a2aaaa0b3ba055cfd Mon Sep 17 00:00:00 2001
From: Martin Goik <goik@hdm-stuttgart.de>
Date: Mon, 14 Sep 2015 20:46:04 +0200
Subject: [PATCH] time conversion exercise

---
 Doc/Sd1/languageFundamentals.xml | 235 ++++++++++++++++++++++---------
 1 file changed, 170 insertions(+), 65 deletions(-)

diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml
index 7e9028c6c..a122198a5 100644
--- a/Doc/Sd1/languageFundamentals.xml
+++ b/Doc/Sd1/languageFundamentals.xml
@@ -583,8 +583,8 @@ System.out.println("Maximum short value:" + maximum);
             </question>
 
             <answer>
-              <para>Since <code>short</code> variables are being represented
-              by to bytes their range is <inlineequation>
+              <para>Since variables of type <code>short</code> are being
+              represented by to bytes their range is <inlineequation>
                   <m:math display="inline">
                     <m:mrow>
                       <m:mo>[</m:mo>
@@ -632,7 +632,7 @@ System.out.println("Maximum short value:" + maximum);
 
               <para>On the other hand the <code>int</code> literal
               <code>0B10000000_00000000</code> evaluates to +32768 and can
-              thus not be assigned to a short at all not to mention its non-
+              thus not be assigned to a short at all. Not to mention its non-
               <link
               xlink:href="http://en.wikipedia.org/wiki/Two's_complement#firstHeading">Two's
               complement</link> representation. In order to avoid this error
@@ -651,49 +651,50 @@ System.out.println("Maximum short value:" + maximum);
     </section>
   </section>
 
-  <section xml:id="sd1SimpleExpressions">
+  <section xml:id="sw1SectSquareArea">
     <title>Simple expressions</title>
 
-    <qandaset defaultlabel="qanda" xml:id="sw1QandaSquareArea">
-      <title>Calculating square's area</title>
+    <section xml:id="sd1SquareArea">
+      <title>Calculating a square's area</title>
 
-      <qandadiv>
-        <qandaentry>
-          <question>
-            <para>The area <inlineequation>
-                <m:math display="inline">
-                  <m:mi>a</m:mi>
-                </m:math>
-              </inlineequation> of a given square having radius
-            <inlineequation>
-                <m:math display="inline">
-                  <m:mi>r</m:mi>
-                </m:math>
-              </inlineequation> is being obtained by <inlineequation>
-                <m:math display="inline">
-                  <m:mrow>
+      <qandaset defaultlabel="qanda" xml:id="sw1QandaSquareArea">
+        <qandadiv>
+          <qandaentry>
+            <question>
+              <para>The area <inlineequation>
+                  <m:math display="inline">
                     <m:mi>a</m:mi>
+                  </m:math>
+                </inlineequation> of a given square having radius
+              <inlineequation>
+                  <m:math display="inline">
+                    <m:mi>r</m:mi>
+                  </m:math>
+                </inlineequation> is being obtained by <inlineequation>
+                  <m:math display="inline">
+                    <m:mrow>
+                      <m:mi>a</m:mi>
 
-                    <m:mo>=</m:mo>
+                      <m:mo>=</m:mo>
 
-                    <m:mrow>
-                      <m:mi>Ï€</m:mi>
+                      <m:mrow>
+                        <m:mi>Ï€</m:mi>
 
-                      <m:mo>×</m:mo>
+                        <m:mo>×</m:mo>
 
-                      <m:msup>
-                        <m:mi>r</m:mi>
+                        <m:msup>
+                          <m:mi>r</m:mi>
 
-                        <m:mi>2</m:mi>
-                      </m:msup>
+                          <m:mi>2</m:mi>
+                        </m:msup>
+                      </m:mrow>
                     </m:mrow>
-                  </m:mrow>
-                </m:math>
-              </inlineequation>. Complete the following code to calculate the
-            result and write it to standard output using
-            <code>System.out.println(...)</code>:</para>
+                  </m:math>
+                </inlineequation>. Complete the following code to calculate
+              the result and write it to standard output using
+              <code>System.out.println(...)</code>:</para>
 
-            <programlisting language="noneJava">public static void main(String[] args) {
+              <programlisting language="noneJava">public static void main(String[] args) {
       
    double radius = 2.31;         // A square having a radius (given e.g. in mm).
    double pi = 3.1415926;  // Constant relating a square's radius, perimeter and area.
@@ -701,39 +702,39 @@ System.out.println("Maximum short value:" + maximum);
    // TODO: Write the circle's area to standard output     
 }</programlisting>
 
-            <tip>
-              <para>You may want to read the <link
-              xlink:href="http://proquest.safaribooksonline.com/9780992133047/toc1_html_4">overview
-              section</link> on statements.</para>
-            </tip>
-          </question>
+              <tip>
+                <para>You may want to read the <link
+                xlink:href="http://proquest.safaribooksonline.com/9780992133047/toc1_html_4">overview
+                section</link> on statements.</para>
+              </tip>
+            </question>
 
-          <answer>
-            <glosslist>
-              <glossentry>
-                <glossterm>Using a mere expression</glossterm>
+            <answer>
+              <glosslist>
+                <glossentry>
+                  <glossterm>Using a mere expression</glossterm>
 
-                <glossdef>
-                  <programlisting language="noneJava">public static void main(String[] args) {
+                  <glossdef>
+                    <programlisting language="noneJava">public static void main(String[] args) {
       
    double radius = 2.31;   // A square having a radius (given e.g. in mm).
    double pi = 3.1415926;  // Constant relating a square's radius, perimeter and area.
       
    System.out.println(pi * radius * radius);
 }</programlisting>
-                </glossdef>
-              </glossentry>
+                  </glossdef>
+                </glossentry>
 
-              <glossentry>
-                <glossterm>Using a variable</glossterm>
+                <glossentry>
+                  <glossterm>Using a variable</glossterm>
 
-                <glossdef>
-                  <para>Instead of immediately using the expression as an
-                  argument to <code>System.out.println(...)</code> we may
-                  assign its value to a variable prior to output
-                  creation:</para>
+                  <glossdef>
+                    <para>Instead of immediately using the expression as an
+                    argument to <code>System.out.println(...)</code> we may
+                    assign its value to a variable prior to output
+                    creation:</para>
 
-                  <programlisting language="noneJava">public static void main(String[] args) {
+                    <programlisting language="noneJava">public static void main(String[] args) {
       
    double radius = 2.31;   // A square having a radius (given e.g. in mm).
    double pi = 3.1415926;  // Constant relating a square's radius, perimeter and area.
@@ -741,13 +742,117 @@ System.out.println("Maximum short value:" + maximum);
    double area = pi * radius * radius;
    System.out.println(area);
 }</programlisting>
-                </glossdef>
-              </glossentry>
-            </glosslist>
-          </answer>
-        </qandaentry>
-      </qandadiv>
-    </qandaset>
+                  </glossdef>
+                </glossentry>
+              </glosslist>
+            </answer>
+          </qandaentry>
+        </qandadiv>
+      </qandaset>
+    </section>
+
+    <section xml:id="sd1SectTimeUnits">
+      <title>Time unit conversion</title>
+
+      <qandaset defaultlabel="qanda" xml:id="sd1QandaTimeUnits">
+        <qandadiv>
+          <qandaentry>
+            <question>
+              <para>This exercise consists of two parts:</para>
+
+              <orderedlist>
+                <listitem>
+                  <para>Write an application converting a (seconds, minutes
+                  ,hours) time specification to seconds:</para>
+
+                  <programlisting language="noneJava">  public static void main(String[] args) {
+
+    final int
+      seconds = 31,
+      minutes = 16,
+      hours = 4;
+    
+    int timeInSeconds ...
+    
+    System.out.println("Time in seconds:" + timeInSeconds);
+  }</programlisting>
+
+                  <para>The expected output reads:</para>
+
+                  <programlisting language="noneJava">Time in seconds:15391</programlisting>
+                </listitem>
+
+                <listitem>
+                  <para>Reverse the previous part of this exercise: Convert a
+                  time specification in seconds to (seconds, minutes ,hours)
+                  as in:</para>
+
+                  <programlisting language="noneJava">  public static void main(String[] args) {
+
+    final int timeInSeconds = 15391;
+    
+    ...
+    
+    System.out.println("Hours:" +  hours);
+    System.out.println("Minutes:" +  minutes);
+    System.out.println("Seconds:" +  seconds);
+  }</programlisting>
+
+                  <para>The expected output reads:</para>
+
+                  <programlisting language="noneJava">Hours:4
+Minutes:16
+Seconds:31</programlisting>
+
+                  <tip>
+                    <para>Consider the <link
+                    xlink:href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html">remainder
+                    operator <quote>%</quote></link> (modulo operator).</para>
+                  </tip>
+                </listitem>
+              </orderedlist>
+            </question>
+
+            <answer>
+              <orderedlist>
+                <listitem>
+                  <para>A straightforward solution reads:</para>
+
+                  <para><programlisting language="noneJava">  public static void main(String[] args) {
+
+    final int
+      seconds = 31,
+      minutes = 16,
+      hours = 4;
+    
+    final int timeInSeconds = seconds + 60 * (minutes + 60 * hours);
+    
+    System.out.println("Time in seconds:" + timeInSeconds);
+  }</programlisting></para>
+                </listitem>
+
+                <listitem>
+                  <programlisting language="noneJava">  public static void main(String[] args) {
+
+    final int timeInSeconds = 15391;
+    
+    final int minutesRemaining = timeInSeconds / 60;
+    final int seconds = timeInSeconds % 60;
+    
+    final int hours = minutesRemaining / 60;
+    final int minutes = minutesRemaining % 60;
+    
+    System.out.println("Hours:" +  hours);
+    System.out.println("Minutes:" +  minutes);
+    System.out.println("Seconds:" +  seconds);
+  }</programlisting>
+                </listitem>
+              </orderedlist>
+            </answer>
+          </qandaentry>
+        </qandadiv>
+      </qandaset>
+    </section>
 
     <section xml:id="sd1SectInterestSimple">
       <title>Interest calculation</title>
@@ -942,7 +1047,7 @@ System.out.println("New value=" + a);</programlisting>
 
               <para>If the expression <code>a + b</code> was of data type
               <code>byte</code> an arithmetic overflow as in the subsequent
-              code example would occur: </para>
+              code example would occur:</para>
 
               <programlisting language="none">    byte a = 120, b = 10;
     
-- 
GitLab