Skip to content
Snippets Groups Projects
Commit 248e976b authored by Goik Martin's avatar Goik Martin
Browse files

Interest rate example German --> English

parent 49f0e5da
No related branches found
No related tags found
No related merge requests found
......@@ -1733,7 +1733,7 @@ public void writeSvg() {
<chapter xml:id="sd1CrabsEnhance2">
<title>Lecture 5 - A simple interest calculator</title>
<para>See compressed eclipse project giro.zip in <link
<para>See compressed eclipse project account.zip in <link
xlink:href="https://cloud.mi.hdm-stuttgart.de/owncloud/public.php?service=files&amp;t=df9f296af3298f96361a15a679390e59">subfolder
06</link>. This example illustrates the following concepts:</para>
......@@ -1750,8 +1750,8 @@ public void writeSvg() {
declaration</glossterm>
<glossdef>
<para><code>private double stand</code>, <code>public void
setStand(double stand)</code></para>
<para><code>private double balance</code>, <code>public void
setBalance(double balance)</code></para>
</glossdef>
</glossentry>
......@@ -1760,8 +1760,8 @@ public void writeSvg() {
declaration</glossterm>
<glossdef>
<para><code>private static double guthabenZinssatz</code>,
<code>private static double guthabenZinssatz(double
<para><code>private static double </code>interestRate,
<code>public static void setInterestRate(double
z)</code></para>
</glossdef>
</glossentry>
......@@ -1777,19 +1777,24 @@ public void writeSvg() {
<glossterm>Formal parameter names and variable scopes</glossterm>
<glossdef>
<programlisting language="java">public static void setZinssatz(double z) { // The scope of z is just the next block {...}
guthabenZinssatz = z; // The scope of guthabenZinssatz is class
// level.
}</programlisting>
<programlisting language="java"> /**
* Setting the interest rate common to all accounts.
*
* @param z
* the desired (global) interest rate.
*/
public static void setInterestRate(double z) { // Scope of variable "z" limited is just the next block {...},
interestRate = z; // in contrast interestRate has class scope.
}</programlisting>
<para>The formal variable's name <quote><code>z</code></quote> may
be <emphasis>consistently</emphasis> renamed to any other legal,
non-conflicting value like
<quote><code>myNewInvention</code></quote>:</para>
<quote><code>myFunnyVariableName</code></quote>:</para>
<programlisting language="java">public static void setZinssatz(double myNewInvention) {
guthabenZinssatz = myNewInvention;
}</programlisting>
<programlisting language="java"> public static void setInterestRate(double myFunnyVariableName) {
interestRate = myFunnyVariableName;
}</programlisting>
<para>Name shadowing conflicts can be resolved by using the keyword
<emphasis><code>this</code></emphasis> <coref
......@@ -1797,14 +1802,15 @@ public void writeSvg() {
<programlisting language="java">public class Konto {
...
private double stand; <emphasis role="bold">// variable "stand" being shadowed inside body of setStand(...)</emphasis>
private double balance; <emphasis role="bold">// variable "stand" being shadowed inside body of setStand(...)</emphasis>
...
public void setStand(double stand) {
if (stand &lt;= 10000) {
<emphasis role="bold">this</emphasis>.stand <co xml:id="sd1ListingThis"/> = stand; // "this" required to resolve name shadowing conflict
// by formal parameter name "double stand".
if (balance &lt;= 10000) {
<emphasis role="bold">this</emphasis>.balance <co
xml:id="sd1ListingThis"/> = balance; // "this" required to resolve name shadowing conflict
// by formal parameter name "double balance".
} else {
System.out.println("Wert" + stand + " ist groesser als " + 10000);
System.out.println("Balance" + balance + " exceeds " + 10000);
}
}
...
......@@ -1817,13 +1823,13 @@ public void writeSvg() {
attributes and methods</glossterm>
<glossdef>
<programlisting>public class Konto {
<programlisting>public class Account {
private static double // Common interestrate for all accounts being declared
guthabenZinssatz = 1.5; // once per class rather than per instance.
<emphasis role="bold">private</emphasis> static double // Visible for class methods only
interestRate = 1.5;
...
public void verzinsung() {
stand = stand * (1 + guthabenZinssatz / 100);
<emphasis role="bold">public</emphasis> void applyInterest() { // Externally visible
balance = balance * (1 + interestRate / 100);
}
...</programlisting>
......@@ -1838,22 +1844,24 @@ public void writeSvg() {
<glossdef>
<para>Example:</para>
<programlisting>public class Konto {
<programlisting>public class Account {
public Konto() { // Default constructor without any parameter
setStand(0);
public Account() { // Default Constructor without any parameter
setBalance(0);
}
...
public Konto(double stand) { // Non-default constructor defining an account's
setStand(stand); // initial value.
public Account(double balance) { // <emphasis role="bold">Overloaded</emphasis> non-default constructor creating an account
setBalance(balance); // with (possibly) non-zero balance.
}
...
public void verzinsung() { // Just one year.
stand = stand * (1 + guthabenZinssatz / 100);
public void applyInterest() { // Just one year
balance = balance *
(1 + interestRate / 100);
}
...
public void verzinsung(int jahre) { // Arbitrary number of years.
stand = stand * Math.pow((1 + guthabenZinssatz / 100), jahre) ; // raised to the power of jahre, see javadoc comment.
public void applyInterest(int years) { // <emphasis role="bold">Overloaded</emphasis> method allowing for different time periods.
balance = balance *
Math.pow((1 + interestRate / 100), years);
}
...
}</programlisting>
......@@ -1867,7 +1875,7 @@ public void writeSvg() {
<glossterm>Use of standard mathematical functions</glossterm>
<glossdef>
<programlisting>Math.pow((1 + guthabenZinssatz / 100), jahre)</programlisting>
<programlisting>Math.pow((1 + interestRate / 100), years)</programlisting>
<para>See <xref linkend="bibHorton2011"/>, chapter 2,
<quote>MATHEMATICAL FUNCTIONS AND CONSTANTS</quote>.</para>
......@@ -1893,6 +1901,52 @@ public void writeSvg() {
<section xml:id="sd1VariableExercises">
<title>Exercises</title>
<qandaset defaultlabel="qanda" xml:id="sd1QandaExtendInterest">
<title>Extending interest calculations.</title>
<qandadiv>
<qandaentry>
<question>
<para>Our current <code>Account</code> class does not handle
negative balances accordingly. Typically banks will charge a
different interest rate whenever an account is in debt i.e.
having a negative balance. In this case a second so called
default interest rate (being significantly higher) will be
applied.</para>
<para>Extend the current project by adding a new instance
variable <varname>defaultInterestRate</varname> along with
getter and setter methods. Then change the implementation of
<code>applyInterest()</code> and <code>applyInterest(int
years)</code> by using the correct interest value according to
the account's balance being positive or negative.</para>
<caution>
<para>Do not forget to change the javadoc comments
accordingly!</para>
</caution>
<para>An eclipse project archive file account.zip can be
imported from here.</para>
</question>
<answer>
<para>We introduce a new variable
<code>defaultInterestRate</code> to cover negative balance
values:</para>
<programlisting> private static double
interestRate = 1.5, // applied to positive balances
<emphasis role="bold">defaultInterestRate = 15.; // applied to negative balances</emphasis></programlisting>
<para>We need the appropriate getter and setter methods:</para>
<programlisting/>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
<qandaset defaultlabel="qanda" xml:id="sd1VariableComplexExpression">
<title>Programmers favourite expression</title>
......@@ -2076,6 +2130,13 @@ public class Fraction {
</qandadiv>
</qandaset>
</section>
<section xml:id="sd1ExerciseGreenCh3">
<title><productname>Greenfoot</productname></title>
<para>Finish all exercises being presented in chapter 3 of <xref
linkend="bibKoelling2010Ger"/>.</para>
</section>
</chapter>
<chapter xml:id="sd1L7">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment