diff --git a/Doc/Sd1/arrays.xml b/Doc/Sd1/arrays.xml index 692508c2eafd7721fd166adc04277d12ccf17652..5f77350d179fdbf82cacb13c5569ff9230a2768a 100644 --- a/Doc/Sd1/arrays.xml +++ b/Doc/Sd1/arrays.xml @@ -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/> diff --git a/Doc/Sd1/class.xml b/Doc/Sd1/class.xml deleted file mode 100644 index 96e7f86d7823d17a79153c2ed6b217e006080649..0000000000000000000000000000000000000000 --- a/Doc/Sd1/class.xml +++ /dev/null @@ -1,215 +0,0 @@ -<?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 < balance) { - balance = balance * Math.pow((1 + interestRate / 100), years) ; - } else if (balance < 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&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> diff --git a/Doc/Sd1/coreClasses.xml b/Doc/Sd1/coreClasses.xml index aa7f46e0393b60518459cb41d77520102ab86420..fe85c3bab44567b80e13bb789928a4fb23931197 100644 --- a/Doc/Sd1/coreClasses.xml +++ b/Doc/Sd1/coreClasses.xml @@ -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/> diff --git a/Doc/Sd1/errorHandling.xml b/Doc/Sd1/errorHandling.xml index 565be73157eb0caa27d054c0712bdbe37c93b029..765765eaf54447772c64d26dc047b0d6f1dd7d00 100644 --- a/Doc/Sd1/errorHandling.xml +++ b/Doc/Sd1/errorHandling.xml @@ -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/> diff --git a/Doc/Sd1/inheritance.xml b/Doc/Sd1/inheritance.xml index bb1b5aa731f814cfc88ff40c6b7079d4b8d56c99..b505d67daf2aace0feb1854af908e99e29c997d9 100644 --- a/Doc/Sd1/inheritance.xml +++ b/Doc/Sd1/inheritance.xml @@ -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/> diff --git a/Doc/Sd1/interfacesAbstractClasses.xml b/Doc/Sd1/interfacesAbstractClasses.xml index d3e40dbde2d2805255265f94c45a77319634079e..c5f64de79f7f60ad1d05812d5d06a418f79cc2d6 100644 --- a/Doc/Sd1/interfacesAbstractClasses.xml +++ b/Doc/Sd1/interfacesAbstractClasses.xml @@ -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/> diff --git a/Doc/Sd1/objectsClasses.xml b/Doc/Sd1/objectsClasses.xml index 8c5414605a2e8371b466ed5b217c53de2e8ceeeb..cb6ff0408ee65c5f570579e243ce9249bc0fc740 100644 --- a/Doc/Sd1/objectsClasses.xml +++ b/Doc/Sd1/objectsClasses.xml @@ -7,11 +7,959 @@ 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>Objects and Classes</title> - <section xml:id="sw1SectionObjectsClasses"> - <title/> + <section xml:id="sd1CrabsEnhancePrepare"> + <title>Preparations</title> - <para/> + <itemizedlist> + <listitem> + <para>Read ...</para> + + <itemizedlist> + <listitem> + <para>static vs. non-static methods and fields.</para> + </listitem> + + <listitem> + <para>the <code>final</code> keyword's meaning.</para> + </listitem> + + <listitem> + <para>The way a garbage collector works.</para> + </listitem> + </itemizedlist> + </listitem> + </itemizedlist> + </section> + + <section xml:id="sd1GeometryClasses"> + <title>Dealing with geometry classes</title> + + <qandaset defaultlabel="qanda" xml:id="sd1ImplementRectangle"> + <title>Modeling rectangles</title> + + <qandaentry> + <question> + <para>We want to model rectangles being defined y width and height + to allow for the subsequently demonstrated operations:</para> + + <programlisting language="java">final Rectangle r = new Rectangle(8, 5); <co + linkends="sw1CalloutRectangle-1" xml:id="constructor"/> + +System.out.println("Perimeter:" + r.getPerimeter()<co + linkends="sw1CalloutRectangle-2" xml:id="getPerimeter"/>); +System.out.println("Area:" + r.getArea()<co linkends="sw1CalloutRectangle-3" + xml:id="getArea"/>); + +r.setWidth(4); <co linkends="sw1CalloutRectangle-4" xml:id="setWidth"/> +r.setHeight(7); + +System.out.println("Perimeter:" + r.getPerimeter()); <co + linkends="sw1CalloutRectangle-5" + xml:id="sw1CalloutRectangle-5-co"/> +System.out.println("Area:" + r.getArea());</programlisting> + + <calloutlist> + <callout arearefs="constructor" xml:id="sw1CalloutRectangle-1"> + <para>Creating an instance of class + <classname>Rectangle</classname> by calling a non-default + constructor which allows for providing width (8) and height + (5).</para> + </callout> + + <callout arearefs="getPerimeter" xml:id="sw1CalloutRectangle-2"> + <para>Returning the rectangle's perimeter, </para> + </callout> + + <callout arearefs="getArea" xml:id="sw1CalloutRectangle-3"> + <para>Returning the rectangle's area.</para> + </callout> + + <callout arearefs="setWidth" xml:id="sw1CalloutRectangle-4"> + <para>Setting with and height to new values.</para> + </callout> + + <callout arearefs="sw1CalloutRectangle-5-co" + xml:id="sw1CalloutRectangle-5"> + <para>Write (possibly) changed perimeter and area values.</para> + </callout> + </calloutlist> + + <para>You may start from the following + <classname>Rectangle</classname> class dummy implementation:</para> + + <programlisting language="java">/** + * Representing rectangular shapes. + * + */ +public class Rectangle { + + /** + * + * @param width The rectangle's width + * @param heigth The rectangle's height + */ + public Rectangle (double width, double heigth) { + //TODO + } + /** + * @return The rectangle's area. + */ + public double getArea() { + return 0; // TODO + } + + /** + * @return The rectangle's perimeter. + */ + public double getPerimeter() { + return 0; // TODO + } + + /** + * @return The rectangle's width. + */ + public double getWidth() { + return 0; // TODO + } + /** + * @param width The rectangle's new width + */ + public void setWidth(double width) { + // TODO + } + + /** + * @return The rectangle's height. + */ + public double getHeight() { + return 0; // TODO + } + + /** + * @param width The rectangle's new height + */ + public void setHeight(double height) { + // TODO + } +}</programlisting> + </question> + + <answer> + <para>First we define two instance (= non-static) variables + <code>width</code> and <code>height</code> representing a + <classname>Rectangle</classname>'s corresponding two parameters + <code>width</code> and <code>height</code>:</para> + + <programlisting language="java">public class Rectangle { + + // Instance variables representing a rectangle's parameters + private double <emphasis role="bold">width</emphasis>, <emphasis role="bold">height</emphasis>; +... +}</programlisting> + + <para>Next we allow for changing these two parameters:</para> + + <programlisting language="java">public class Rectangle { + + // Instance variables representing a rectangle's parameters + private double width, height; + +... + /** + * @param width Changing the rectangle's width + */ + public void setWidth(double w) { + <emphasis role="bold">width = w;</emphasis> + } + + /** + * @param width Changing the rectangle's height + */ + public void setHeight(double height) { + <emphasis role="bold">this.height = height;</emphasis> + } +... +}</programlisting> + + <para>Note the subtle implementation difference between + <methodname>setWidth(...)</methodname> and + <methodname>setHeight(...)</methodname>:</para> + + <glosslist> + <glossentry xml:id="glossMethodDiff1"> + <glossterm><methodname>setWidth(double + w)</methodname></glossterm> + + <glossdef> + <para>We use the formal parameter name <quote>w</quote>. Its + name does not conflict with the instance variable name + <quote>width</quote> being defined at class level. We can + simply assign this value to our corresponding instance + variable using <code>width = w;</code>.</para> + </glossdef> + </glossentry> + + <glossentry xml:id="xmlMethodDiff2"> + <glossterm><methodname>setHeight(double + height)</methodname></glossterm> + + <glossdef> + <para>The method's formal parameter <quote>height</quote> + shadows the instance variable's name being defined at class + level. We need the <quote>this</quote> keyword in + <code>this.height = height</code> to resolve the + ambiguity.</para> + </glossdef> + </glossentry> + </glosslist> + + <para>Both ways are perfectly legal. The complete implementation + including all remaining methods reads:</para> + + <programlisting language="java">/** + * Representing rectangular shapes. + * + */ +public class Rectangle { + + // Instance variables representing a rectangle's parameters + private double width, height; + + /** + * + * @param width The rectangle's width + * @param heigth The rectangle's height + */ + public Rectangle (double width, double height) { + setWidth(width); + setHeight(height); + } + /** + * @return The rectangle's area. + */ + public double getArea() { + return width * height; + } + + /** + * @return The rectangle's perimeter. + */ + public double getPerimeter() { + return 2 * (width + height); + } + + /** + * @return The rectangle's width. + */ + public double getWidth() { + return width; + } + /** + * @param width The rectangle's new width + */ + public void setWidth(double w) { + width = w; + } + + /** + * @return The rectangle's height. + */ + public double getHeight() { + return height; + } + + /** + * @param width The rectangle's new height + */ + public void setHeight(double height) { + this.height = height; + } +}</programlisting> + </answer> + </qandaentry> + </qandaset> + + <qandaset defaultlabel="qanda" xml:id="qandasetGeometry"> + <title>Modeling circles</title> + + <qandadiv> + <qandaentry> + <question> + <para>We provide a corresponding class + <classname>Circle</classname> dummy implementation:</para> + + <programlisting language="java">package step1.dummy; + +/** + * A circle of given radius + * + */ +public class Circle { + + /** + * A new circle + * + * @param radius + * The desired radius. + */ + public Circle(double radius) { + // TODO + } + + /** + * @return The circle's area. + */ + public double getArea() { + return 0; // TODO + } + + /** + * @return The circle's perimeter. + */ + public double getPerimeter() { + return 0; // TODO + } + + /** + * @return The circle's radius. + */ + public double getRadius() { + return 0; // TODO + } + + /** + * @param radius + * Setting the circle's radius to a new value. + */ + public void setRadius(double radius) { + // TODO + } +}</programlisting> + + <para>Instances of this class shall be usable in the following + fashion:</para> + + <programlisting language="java"> public static void main(String[] args) { + 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()); + }</programlisting> + + <para>Hint: Obviously you'll have to define an instance variable + within Circle to keep track of its current radius value. All + methods mentioned above simply depend on this single value.</para> + </question> + + <answer> + <para>We define an instance variable <code>radius</code> inside + our class <classname>Circle</classname>:</para> + + <programlisting language="java">public class Circle { + + double radius; +... +}</programlisting> + + <para>Next we implement our method to change a circle's + radius:</para> + + <programlisting language="java"> public void setRadius(double r) { + radius = r; + }</programlisting> + + <para>Note that we have chosen a different value for the method's + formal radius parameter to be <quote>r</quote> rather than + <quote>radius</quote>. Many people prefer to use radius here + making it easier for a programmer to recognize the expected name + in the generated <xref linkend="glo_Javadoc"/>:</para> + + <programlisting language="java"> public void setRadius(double radius) { + <emphasis role="bold">this.</emphasis>radius = radius; + }</programlisting> + + <para>This requires the usage of the <code>this</code> keyword to + distinguish the formal parameter in <methodname>setRadius(double + radius)</methodname> from the instance variable previously being + defined within our class <classname>Circle</classname>. In other + words: We have to resolve a name shadowing conflict.</para> + + <para>The rest of the implementation is (quite) straightforward. A + complete class reads:</para> + + <programlisting language="java">package step1; + +/** + * A circle of given radius + * + */ +public class Circle { + + double radius; + + /** + * A new circle + * @param radius The desired radius. + */ + public Circle(double radius) { + setRadius(radius); + } + + /** + * @return The circle's area. + */ + public double getArea() { + return radius * radius * Math.PI; + } + + /** + * @return The circle's perimeter. + */ + public double getPerimeter() { + return 2 * Math.PI * radius; + } + + /** + * @return The circle's radius. + */ + public double getRadius() { + return radius; + } + + /** + * @param radius Setting the circle's radius to a new value. + */ + public void setRadius(double radius) { + this.radius = radius; + } +}</programlisting> + </answer> + </qandaentry> + + <qandaentry> + <question> + <para>Our current Circle and Rectangle class only models shape + parameters and does not allow for translation in a coordinate + system:</para> + + <itemizedlist> + <listitem> + <para>Add two more instance variables x and y and + corresponding setter methods to account for a shape's + translation vector with respect to the origin (0,0). The + following hint may be helpful:</para> + + <programlisting language="java"> /** + * @param x The circle's x center coordinate value + */ + public void setX(double x) { + // TODO + } + /** + * @param x The circle's x center coordinate value + */ + public void setY(double y) { + // TODO + }</programlisting> + </listitem> + + <listitem> + <para>We would like Rectangle and Circle instances to be + visualized as <acronym>SVG</acronym> graphics. Add a method + <methodname>void writeSvg()</methodname> to both classes which + allows for <acronym>SVG</acronym> code export to standard + output. You may want to read the w3schools <link + xlink:href="http://www.w3schools.com/svg/svg_rect.asp">rectangle</link> + and <link + xlink:href="http://www.w3schools.com/svg/svg_circle.asp">circle</link> + examples. Use System.out.println(...) calls to create the + desired <acronym>SVG</acronym> output. You may need + <code>\"</code> to represent double quotes as in the + subsequent example:</para> + + <programlisting language="java">System.out.println("<rect width=\"20\"" ...</programlisting> + </listitem> + </itemizedlist> + + <para>The following code snippet may serve to illustrate the + intended use of <methodname>void writeSvg()</methodname>:</para> + + <programlisting language="java">public class Driver { + + public static void main(String[] args) { + + System.out.println("<!DOCTYPE html><html><body>"); + System.out.println(" <svg width='300' height='200' >"); + + // Draw a rectangle as SVG + final Rectangle r = new Rectangle(5, 4); + r.setX(2); + r.setY(1); + r.writeSvg(); + + // Draw a circle as SVG + final Circle c = new Circle(1, 1, 3); + c.setX(3); + c.setY(1); + c.writeSvg(); + System.out.println(" </svg >"); + System.out.println("</body></html>"); + } +}</programlisting> + + <para>Implement the method <methodname>void + writeSvg()</methodname> in both classes + <classname>Rectangle</classname> and + <classname>Circle</classname>. This should produce an output + result like:</para> + + <programlisting language="java"><!DOCTYPE html> +<html> + <body> + <svg width='300' height='200' > + <emphasis role="bold"><rect width='100.0' height='80.0' x='40.0' y='20.0'' style='fill:rgb(0,255,0);stroke-width:3;stroke:rgb(0,0,0)'/></emphasis> + <emphasis role="bold"><circle r='60.0' cx='60.0' cy='20.0' style='fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)'/></emphasis> + </svg > + </body> +</html></programlisting> + + <para>You may enter this output into a file sfg.html. A web + browser should visualize this output as:</para> + + <informalfigure> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/svgGeometry.png"/> + </imageobject> + </mediaobject> + </informalfigure> + </question> + + <answer> + <programlisting language="java"> private double x, y, width, height; + ... + /** + * @param x The rectangle's x center coordinate value + */ + public void setX(double x) { + this.x = x; + } + /** + * @param x The rectangle's x center coordinate value + */ + public void setY(double y) { + this.y = y; + } + +public void writeSvg() { + final int scale = 20; + System.out.println( + "<rect width='" + scale * width +"' height='" + scale * height + + "' x='" + scale * x + "'" + " y='" + scale * y + "'" + + "' style='fill:rgb(0,255,0);stroke-width:3;stroke:rgb(0,0,0)'/>"); + } +}</programlisting> + + <programlisting language="java">public class Circle { + + double x, y, radius; +... + + /** + * @param x The circle's x center coordinate value + */ + public void setX(double x) { + this.x = x; + } + /** + * @param x The circle's x center coordinate value + */ + public void setY(double y) { + this.y = y; + } + +public void writeSvg() { + final int scale = 20; + + System.out.println( + "<circle r='" + scale * radius + + "' cx='" + scale * x + "'" + " cy='" + scale * y + + "' style='fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)'/>"); + + } +}</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section version="5.0" xml:id="sd1InterestCalculator"> + <title>Lecture 5 - A simple interest calculator</title> + + <para>Consider the following implementation of an interest + calculator:</para> + + <annotation role="make"> + <para role="eclipse">Sd1/interest/V1</para> + </annotation> + + <glosslist> + <glossentry> + <glossterm>Instance versus class variables and methods</glossterm> + + <glossdef> + <para>Examples:</para> + + <glosslist> + <glossentry> + <glossterm>Instance variables and methods, non-static + declaration</glossterm> + + <glossdef> + <para><code>private double balance</code>, <code>public void + setBalance(double balance)</code></para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Class variables and methods, static + declaration</glossterm> + + <glossdef> + <para><code>private static double </code>interestRate, + <code>public static void setInterestRate(double + z)</code></para> + </glossdef> + </glossentry> + </glosslist> + + <para>For both categories chapter 5, <quote>Fields in a Class + Definition</quote> and <quote>Methods in a Class + Definition</quote>.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Formal parameter names and variable scopes</glossterm> + + <glossdef> + <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>myFunnyVariableName</code></quote>:</para> + + <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 + linkend="sd1ListingThis"/>:</para> + + <programlisting language="java">public class Konto { +... + private double balance; <emphasis role="bold">// variable "stand" being shadowed inside body of setStand(...)</emphasis> +... + public void setStand(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("Balance" + balance + " exceeds " + 10000); + } + } +... +}</programlisting> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Access restrictions public / private / protected to + attributes and methods</glossterm> + + <glossdef> + <programlisting language="java">public class Account { + + <emphasis role="bold">private</emphasis> static double // Visible for class methods only + interestRate = 1.5; +... + <emphasis role="bold">public</emphasis> void applyInterest() { // Externally visible + balance = balance * (1 + interestRate / 100); + } +...</programlisting> + + <para>See , chapter 5, <quote>CONTROLLING ACCESS TO CLASS + MEMBERS</quote>.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Method overloading</glossterm> + + <glossdef> + <para>Example:</para> + + <programlisting language="java">public class Account { + + public Account() { // Default Constructor without any parameter + setBalance(0); + } +... + public Account(double balance) { // <emphasis role="bold">Overloaded</emphasis> non-default constructor creating an account + setBalance(balance); // with (possibly) non-zero balance. + } +... + public void applyInterest() { // Just one year + balance = balance * + (1 + interestRate / 100); + } +... + 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> + + <para>See , chapter 5, <quote>METHOD OVERLOADING</quote>.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Use of standard mathematical functions</glossterm> + + <glossdef> + <programlisting language="java">Math.pow((1 + interestRate / 100), years)</programlisting> + + <para>See , chapter 2, <quote>MATHEMATICAL FUNCTIONS AND + CONSTANTS</quote>.</para> + </glossdef> + </glossentry> + </glosslist> + </section> + + <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 < balance) { + balance = balance * Math.pow((1 + interestRate / 100), years) ; + } else if (balance < 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&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> diff --git a/Doc/Sd1/workingWithNumbers.xml b/Doc/Sd1/workingWithNumbers.xml index 9c99672ab7d55fe48f7d8768e4fa450dab8cf0d5..548a2d866c06b8413838dd2d7cbedb4b71ddc886 100644 --- a/Doc/Sd1/workingWithNumbers.xml +++ b/Doc/Sd1/workingWithNumbers.xml @@ -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/> diff --git a/Doc/lectures.xml b/Doc/lectures.xml index ce1bf3433847b98b75388b1192fd0e8a2cbb3d53..998734ed5d31d15a7051949cbd83fa50940e25d8 100644 --- a/Doc/lectures.xml +++ b/Doc/lectures.xml @@ -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)"/> diff --git a/ws/eclipse/Sde1/Figur/src/step1/DriverCircle.java b/ws/eclipse/Sde1/Figur/src/step1/DriverCircle.java index 0cfc4b56e5db6e48b500e9f256101952eb233e75..d9c48f26b0cb5192420ea21efae27bca002f0bc6 100644 --- a/ws/eclipse/Sde1/Figur/src/step1/DriverCircle.java +++ b/ws/eclipse/Sde1/Figur/src/step1/DriverCircle.java @@ -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()); diff --git a/ws/eclipse/Sde1/Figur/src/step1/DriverRctangle.java b/ws/eclipse/Sde1/Figur/src/step1/DriverRectangle.java similarity index 80% rename from ws/eclipse/Sde1/Figur/src/step1/DriverRctangle.java rename to ws/eclipse/Sde1/Figur/src/step1/DriverRectangle.java index aa66f1e308d02de92a2a09663adfed83e4c81087..0aa0b888b4d6493e022dab641558a60091ab95f9 100644 --- a/ws/eclipse/Sde1/Figur/src/step1/DriverRctangle.java +++ b/ws/eclipse/Sde1/Figur/src/step1/DriverRectangle.java @@ -1,16 +1,18 @@ 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()); diff --git a/ws/eclipse/Sde1/Figur/src/step1/Rectangle.java b/ws/eclipse/Sde1/Figur/src/step1/Rectangle.java index 4db06de036fb01b803242ec0f759eef88afbe487..ec621bbf6c43d53430722321171518134ccbaf43 100644 --- a/ws/eclipse/Sde1/Figur/src/step1/Rectangle.java +++ b/ws/eclipse/Sde1/Figur/src/step1/Rectangle.java @@ -58,4 +58,4 @@ public class Rectangle { public void setHeight(double height) { this.height = height; } -} +} \ No newline at end of file