From ea1be0ef34d0c9e82d688b614f52db971a39b6b8 Mon Sep 17 00:00:00 2001
From: Martin Goik <goik@hdm-stuttgart.de>
Date: Sun, 13 Sep 2015 19:34:45 +0200
Subject: [PATCH] Simple interest exercise

---
 Doc/Sd1/languageFundamentals.xml | 109 ++++++++++++++++++++++++++++++-
 1 file changed, 107 insertions(+), 2 deletions(-)

diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml
index 62138dcb1..d31450d6a 100644
--- a/Doc/Sd1/languageFundamentals.xml
+++ b/Doc/Sd1/languageFundamentals.xml
@@ -655,7 +655,7 @@ System.out.println("Maximum short value:" + maximum);
     <title>Simple expressions</title>
 
     <qandaset defaultlabel="qanda" xml:id="sw1QandaSquareArea">
-      <title>Calculating the area of a square</title>
+      <title>Calculating square's area</title>
 
       <qandadiv>
         <qandaentry>
@@ -749,6 +749,111 @@ System.out.println("Maximum short value:" + maximum);
       </qandadiv>
     </qandaset>
 
+    <section xml:id="sd1SectInterestSimple">
+      <title>Interest calculation</title>
+
+      <qandaset defaultlabel="qanda" xml:id="sd1QandaInterestSimple">
+        <qandadiv>
+          <qandaentry>
+            <question>
+              <para>We want to calculate the compounded interest starting from
+              an initial capital, a given annual interest rate and a duration
+              of three years. Consider the following code fragment:</para>
+
+              <programlisting language="noneJava">  public static void main(String[] args) {
+
+    final double initialCapital = 223.12;
+    final double interestRate = 1.5;
+    
+    System.out.println("Initial capital:" + initialCapital);
+    System.out.println("Annual interest rate:" + interestRate);
+    
+    // TODO ...
+    
+    System.out.println("Capital after three years:" + ...);
+  }</programlisting>
+
+              <para>The expected output is:</para>
+
+              <programlisting language="noneJava">Initial capital:223.12
+Annual interest rate:1.5
+Capital after three years:233.31175902999993</programlisting>
+
+              <tip>
+                <para>In case you are unsure read about calculating the
+                compounded interest.</para>
+              </tip>
+            </question>
+
+            <answer>
+              <para>We obtain the compounded interest by multiplying the
+              initial capital by <inlineequation>
+                  <m:math display="inline">
+                    <m:mrow>
+                      <m:mn>1</m:mn>
+
+                      <m:mo>+</m:mo>
+
+                      <m:mrow>
+                        <m:mi>r</m:mi>
+
+                        <m:mo>/</m:mo>
+
+                        <m:mn>100</m:mn>
+                      </m:mrow>
+                    </m:mrow>
+                  </m:math>
+                </inlineequation> for each year where r represents the given
+              interest rate.</para>
+
+              <para>Since we have not yet introduced loops this multiplication
+              has to be repeated three times:</para>
+
+              <programlisting language="noneJava">  public static void main(String[] args) {
+
+    final double initialCapital = 223.12;
+    final double interestRate = 1.5;
+    
+    System.out.println("Initial capital:" + initialCapital);
+    System.out.println("Annual interest rate:" + interestRate);
+    
+    final double factor = 1. + interestRate/100.;
+    double capitalAtThreeYears = initialCapital;
+    
+    capitalAtThreeYears *= factor; // Year 1
+    capitalAtThreeYears *= factor; // Year 2
+    capitalAtThreeYears *= factor; // Year 3
+    
+    System.out.println("Capital after three years:" + capitalAtThreeYears);
+  }</programlisting>
+
+              <para>We might as well use a single arithmetic expression to
+              achieve the same result:</para>
+
+              <programlisting language="noneJava">  public static void main(String[] args) {
+
+    final double initialCapital = 223.12;
+    final double interestRate = 1.5;
+    
+    System.out.println("Initial capital:" + initialCapital);
+    System.out.println("Annual interest rate:" + interestRate);
+    
+    final double factor = 1. + interestRate/100.;
+    final double capitalAtThreeYears = 
+        initialCapital * factor * factor * factor;
+    
+    System.out.println("Capital after three years:" + capitalAtThreeYears);
+  }</programlisting>
+
+              <para>In <xref linkend="sd1InterestCalculator"/> we will present
+              a more elaborate solution based on loops and class
+              methods.</para>
+            </answer>
+          </qandaentry>
+        </qandadiv>
+      </qandaset>
+    </section>
+
     <section xml:id="sd1ExpressionStrangeThings">
       <title>Strange things happen</title>
 
@@ -772,7 +877,7 @@ System.out.println("New value=" + a);
 New value=-128</programlisting>
 
               <para>Explain this strange behaviour. Moreover you'll find the
-              following code yields a compile time error:</para>
+              following code snippet yields a compile time error:</para>
 
               <programlisting language="noneJava">byte a = 127;
 System.out.println("value=" + a);
-- 
GitLab