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

Moving Circle/rectangle exercise to new chapter

parent 87d5dbb8
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,7 @@
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/ns/docbook">
<title>Statements</title>
<title>Arrays</title>
<section xml:id="sw1SectionArrays">
<title/>
......
<?xml version="1.0" encoding="UTF-8"?>
<chapter version="5.0" xml:id="sd1ClassesInstancesState"
xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/ns/docbook">
<title>-------------------------------- Classes, instances and internal
state (20.10)</title>
<section xml:id="sd1VariableExercises">
<title>Extending our interest calculator</title>
<qandaset defaultlabel="qanda" xml:id="sd1QandaExtendInterest">
<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 <command>Javadoc</command>
comments accordingly!</para>
</caution>
</question>
<answer>
<annotation role="make">
<para role="eclipse">Sd1/interest/V2</para>
</annotation>
<para>We introduce a new variable <code>defaultInterestRate</code>
to cover negative balance values:</para>
<programlisting language="java"> 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 in
<classname
xlink:href="Ref/api/P/interest/V2/de/hdm_stuttgart/mi/sd1/interest/Account.html">Account</classname>:</para>
<programlisting language="java"> /**
* @return
* the current default interest rate value.
*/
public static double <link
xlink:href="Ref/api/P/interest/V2/de/hdm_stuttgart/mi/sd1/interest/Account.html#getDefaultInterestRate--">getDefaultInterestRate()</link> {
return defaultInterestRate;
}
/**
* This interest rate will be applied to negative balances. In contrast
* {{@link #setInterestRate(double)} will handle positive balance values.
*
* @param defaultInterestRate
* the desired default interest rate value.
*/
public static void <link
xlink:href="Ref/api/P/interest/V2/de/hdm_stuttgart/mi/sd1/interest/Account.html#setDefaultInterestRate-double-">setDefaultInterestRate(double defaultInterestRate)</link> {
Account.defaultInterestRate = defaultInterestRate;
}</programlisting>
<para>The computed interest depends on positive or negative
balance values:</para>
<programlisting language="java"> public void applyInterest(int years) {
if (0 &lt; balance) {
balance = balance * Math.pow((1 + interestRate / 100), years) ;
} else if (balance &lt; 0){
balance = balance * Math.pow((1 + defaultInterestRate / 100), years) ;
}
}</programlisting>
<para>A complete solution including updated
<productname>Javadoc</productname> comments can be downloaded
<link
xlink:href="https://cloud.mi.hdm-stuttgart.de/owncloud/public.php?service=files&amp;t=577bc9091391524692b6d812a6d2737a">from
here</link>.</para>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
<qandaset defaultlabel="qanda" xml:id="sd1VariableComplexExpression">
<title>Programmers favourite expression</title>
<qandadiv>
<qandaentry>
<question>
<para>Consider the following code fragment:</para>
<programlisting language="java"> int a = 6,
b = 7,
c = -3,
result = 0;
result += ++a - b++ + --c;</programlisting>
<para>Rewrite this code by decomposing the last line into several
lines to make the code easier to understand.</para>
<para>Hint: After execution of your modified code all variable
must have identical values with respect to the original code. In
other words: Your modifications shall not alter the code's
behaviour in any way.</para>
</question>
<answer>
<para>Incrementing <code>++a</code> and decrementing
<code>--c</code> happens prior to adding / subtracting their
values to the variable <code>result</code> (prefix notation). The
increment operation <code>b--</code> in contrast happens after
being being subtracted from variable <code>result</code> (postfix
notation). The following code snippet is thus equivalent:</para>
<programlisting language="java"> int a = 6,
b = 7,
c = -3,
result = 0;
++a;
--c;
result += a - b + c; // or even: result = result + a - b + c;
b++;</programlisting>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
<qandaset defaultlabel="qanda" xml:id="sd1QandaIntOverflow">
<title>Integer value considerations.</title>
<qandadiv>
<qandaentry>
<question>
<para>Consider the following piece of code:</para>
<programlisting language="java">int a = ..., b = ...;
...// statements being omitted
int sum = a + b;</programlisting>
<para>Which representation related problem may arise here? May you
supply a solution?</para>
</question>
<answer>
<para>The sum of a and b may either exceed
<code>java.lang.Integer.MAX_VALUE</code> or in turn may be less
than <code>java.lang.Integer.MIN_VALUE</code>. To avoid this type
of overflow error our variable <code>sum</code> may be declared of
type long:</para>
<programlisting language="java">int a = ..., b = ...;
...// statements being omitted
long sum = a + b;</programlisting>
<para>Unfortunately this does not (yet) help at all: Since both
operands <code>a</code> and <code>b</code> are of type
<code>int</code> the expression <code>a + b</code> is also of type
int and will be evaluated as such. To circumvent this problem we
have to cast at least one operand to type <code>long</code> prior
to computing the sum. This works since the cast operator
<code>(long)</code> does have higher priority than the
<quote>+</quote> operator</para>
<programlisting language="java">int a = ..., b = ...;
...// statements being omitted
long sum = (long)a + b;</programlisting>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
<qandaset defaultlabel="qanda" xml:id="sde1QandaFraction">
<title>A class representing fractions</title>
<qandadiv>
<qandaentry>
<question>
<para>Implement a class representing fractions. You may find a
dummy implementation containing some (not yet working) sample
usage code being contained in a <code>main()</code> method. This
Maven archive also includes a <xref linkend="glo_Junit"/>
test.</para>
<annotation role="make">
<para role="eclipse">Sd1/fraction/V05</para>
</annotation>
</question>
<answer>
<annotation role="make">
<para role="eclipse">Sd1/fraction/V1</para>
</annotation>
<para>See implementation at <link
xlink:href="Ref/api/P/fraction/V1/de/hdm_stuttgart/mi/sd1/fraction/Fraction.html">Fraction</link>.</para>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
</chapter>
......@@ -7,7 +7,7 @@
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/ns/docbook">
<title>Statements</title>
<title>Core Classes</title>
<section xml:id="sw1SectionCoreClasses">
<title/>
......
......@@ -7,7 +7,7 @@
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/ns/docbook">
<title>Statements</title>
<title>Error Handling</title>
<section xml:id="sw1SectionErrorHandling">
<title/>
......
......@@ -7,7 +7,7 @@
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/ns/docbook">
<title>Statements</title>
<title>Inheritance</title>
<section xml:id="sw1SectionInheritance">
<title/>
......
......@@ -7,7 +7,7 @@
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/ns/docbook">
<title>Statements</title>
<title>Interfaces and Abstract Classes</title>
<section xml:id="sw1SectionInterfacesAbstractClasses">
<title/>
......
This diff is collapsed.
......@@ -7,7 +7,7 @@
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/ns/docbook">
<title>Statements</title>
<title>Working with Numbers</title>
<section xml:id="sw1SectionWorkingWithNumbers">
<title/>
......
......@@ -34,15 +34,21 @@
<xi:include href="Sd1/languageFundamentals.xml" xpointer="element(/1)"/>
<xi:include href="Sd1/statements.xml" xpointer="element(/1)"/>
<xi:include href="Sd1/objectsClasses.xml" xpointer="element(/1)"/>
<xi:include href="Sd1/coreClasses.xml" xpointer="element(/1)"/>
<xi:include href="Sd1/arrays.xml" xpointer="element(/1)"/>
<xi:include href="Sd1/inheritance.xml" xpointer="element(/1)"/>
<xi:include href="Sd1/errorHandling.xml" xpointer="element(/1)"/>
<xi:include href="Sd1/workingWithNumbers.xml" xpointer="element(/1)"/>
<xi:include href="Sd1/interfacesAbstractClasses.xml" xpointer="element(/1)"/>
<xi:include href="Sd1/class.xml" xpointer="element(/1)"/>
<xi:include href="Sd1/interfacesAbstractClasses.xml"
xpointer="element(/1)"/>
<xi:include href="Sd1/loop.xml" xpointer="element(/1)"/>
......
......@@ -3,13 +3,15 @@ package step1;
public class DriverCircle {
public static void main(String[] args) {
Circle c = new Circle(2.3);
final Circle c = new Circle(2.3);
System.out.println("Radius:" + c.getRadius());
System.out.println("Perimeter:" + c.getPerimeter());
System.out.println("Area:" + c.getArea());
// Changing the circle's radius to a different value
c.setRadius(4.7);
System.out.println("Radius:" + c.getRadius());
System.out.println("Perimeter:" + c.getPerimeter());
System.out.println("Area:" + c.getArea());
......
package step1;
public class DriverRctangle {
public class DriverRectangle {
public static void main(String[] args) {
Rectangle r = new Rectangle(8, 5);
final Rectangle r = new Rectangle(8, 5);
System.out.println("Perimeter:" + r.getPerimeter());
System.out.println("Area:" + r.getArea());
r.setWidth(4);
r.setHeight(7);
System.out.println("Perimeter:" + r.getPerimeter());
System.out.println("Area:" + r.getArea());
......
......@@ -58,4 +58,4 @@ public class Rectangle {
public void setHeight(double height) {
this.height = height;
}
}
}
\ No newline at end of file
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