Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
248e976b
Commit
248e976b
authored
11 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Interest rate example German --> English
parent
49f0e5da
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Sd1/swd1.xml
+94
-33
94 additions, 33 deletions
Sd1/swd1.xml
with
94 additions
and
33 deletions
Sd1/swd1.xml
+
94
−
33
View file @
248e976b
...
...
@@ -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&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
set
Stand
(double
stand
)
</code></para>
<para><code>
private double
balance
</code>
,
<code>
public void
set
Balance
(double
balance
)
</code></para>
</glossdef>
</glossentry>
...
...
@@ -1760,8 +1760,8 @@ public void writeSvg() {
declaration
</glossterm>
<glossdef>
<para><code>
private static double
guthabenZinssatz
</code>
,
<code>
p
rivate
static
double guthabenZinss
at
z
(double
<para><code>
private static double
</code>
interestRate
,
<code>
p
ublic
static
void setInterestR
at
e
(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>
my
NewInvention
</code></quote>
:
</para>
<quote><code>
my
FunnyVariableName
</code></quote>
:
</para>
<programlisting
language=
"java"
>
public static void set
Zinss
at
z
(double my
NewInvention
) {
guthabenZinss
at
z
= my
NewInvention;
}
</programlisting>
<programlisting
language=
"java"
>
public static void set
InterestR
at
e
(double my
FunnyVariableName
) {
interestR
at
e
= my
FunnyVariableName;
}
</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
<
= 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
<
= 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 al
s " + 10000);
System.out.println("
Balance" + balance + " exceed
s " + 10000);
}
}
...
...
...
@@ -1817,13 +1823,13 @@ public void writeSvg() {
attributes and methods
</glossterm>
<glossdef>
<programlisting>
public class
Ko
nt
o
{
<programlisting>
public class
Accou
nt {
private
static double //
Common interestrate for all accounts being declared
guthabenZinss
at
z
= 1.5;
// once per class rather than per instance.
<emphasis
role=
"bold"
>
private
</emphasis>
static double
//
Visible for class methods only
interestR
at
e
= 1.5;
...
public void verzinsung() {
stand = stand * (1 + guthabenZinss
at
z
/ 100);
<emphasis
role=
"bold"
>
public
</emphasis>
void applyInterest() { // Externally visible
balance = balance * (1 + interestR
at
e
/ 100);
}
...
</programlisting>
...
...
@@ -1838,22 +1844,24 @@ public void writeSvg() {
<glossdef>
<para>
Example:
</para>
<programlisting>
public class
Ko
nt
o
{
<programlisting>
public class
Accou
nt {
public
Ko
nt
o
() {
// Default
c
onstructor without any parameter
set
Stand(0);
public
Accou
nt() { // Default
C
onstructor without any parameter
set
Balance(0);
}
...
public
Ko
nt
o
(double
stand
) { //
N
on-default constructor
defin
ing an account
's
set
Stand(stand
); //
initial valu
e.
public
Accou
nt(double
balance
) {
//
<emphasis
role=
"bold"
>
Overloaded
</emphasis>
n
on-default constructor
creat
ing an account
set
Balance(balance
);
//
with (possibly) non-zero balanc
e.
}
...
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 +
guthabenZinss
at
z
/ 100),
jahre
)
</programlisting>
<programlisting>
Math.pow((1 +
interestR
at
e
/ 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"
>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment