diff --git a/Doc/Sd1/class.xml b/Doc/Sd1/class.xml index a2bbc1fda65bb8fb3e0862521f8e31ef786945f8..96e7f86d7823d17a79153c2ed6b217e006080649 100644 --- a/Doc/Sd1/class.xml +++ b/Doc/Sd1/class.xml @@ -10,716 +10,6 @@ <title>-------------------------------- Classes, instances and internal state (20.10)</title> - <section xml:id="sd1CrabsEnhancePrepare"> - <title>Preparations</title> - - <itemizedlist> - <listitem> - <para>Read Chapter 5 fro ding the sections <quote>Recursion</quote> - and <quote>Nested classes</quote>. Carefully read the explanations - regarding:</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>Rectangles</title> - - <qandaentry> - <question> - <para>Complete the the following class - <classname>Rectangle</classname>'s 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 - representing a <classname>Rectangle</classname>'s 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 width, height; -... -}</programlisting> - - <para>Next we might allow to change these two parameters:</para> - - <programlisting language="java">public class Rectangle { - - // Instance variables representing a rectangle's parameters - private double width, height; - -... - public void setWidth(double w) { - <emphasis role="bold">width = w;</emphasis> - } - - /** - * @return The rectangle's height. - */ - public void setHeight(double height) { - <emphasis role="bold">this.height = height;</emphasis> - } -... -}</programlisting> - - <para>Notice the subtle difference in the implementation of - setWidth(...) and setHeight(...):</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. We continue our - implementation:</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>Circles</title> - - <qandadiv> - <qandaentry> - <question> - <para>Complete the the following class - <classname>Circle</classname>'s 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 class Driver { - - public static void main(String[] args) { - 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 radius 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 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 instances are only shapes - yet and do not allow to be moved in a coordinate system:</para> - - <itemizedlist> - <listitem> - <para>Add two more instance variables x and y and - corresponding setter methods to account for a shapes origin - being different from (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 (22.10.)</title> - - <!-- - <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> ---> - - <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>