From 2def11c0e770aad15cede66b3937da471e1db943 Mon Sep 17 00:00:00 2001
From: Martin Goik <goik@hdm-stuttgart.de>
Date: Wed, 9 Apr 2014 06:32:58 +0200
Subject: [PATCH] Implementing Rectangles

---
 Sd1/swd1.xml                                  | 270 +++++++++++++++---
 .../step1/{Driver.java => DriverCircle.java}  |   2 +-
 .../Sde1/Figur/src/step1/DriverRctangle.java  |  19 ++
 .../Sde1/Figur/src/step1/Rectangle.java       |  61 ++++
 .../Sde1/Figur/src/step1/dummy/Rectangle.java |  57 ++++
 .../Figur/src/{step2 => step3}/Vector.java    |   2 +-
 6 files changed, 371 insertions(+), 40 deletions(-)
 rename ws/eclipse/Sde1/Figur/src/step1/{Driver.java => DriverCircle.java} (94%)
 create mode 100644 ws/eclipse/Sde1/Figur/src/step1/DriverRctangle.java
 create mode 100644 ws/eclipse/Sde1/Figur/src/step1/Rectangle.java
 create mode 100644 ws/eclipse/Sde1/Figur/src/step1/dummy/Rectangle.java
 rename ws/eclipse/Sde1/Figur/src/{step2 => step3}/Vector.java (89%)

diff --git a/Sd1/swd1.xml b/Sd1/swd1.xml
index 267fb23a1..214b4d2ca 100644
--- a/Sd1/swd1.xml
+++ b/Sd1/swd1.xml
@@ -431,7 +431,7 @@
               <classname>WombatWorld</classname> constructor's code achieves
               the desired behaviour:</para>
 
-              <programlisting>public class WombatWorld {
+              <programlisting language="java">public class WombatWorld {
    public WombatWorld() { <emphasis role="bold">// Constructor will be called each time a WombatWorld</emphasis>
       super(8, 8, 60);    <emphasis role="bold">// is being created</emphasis>
       setBackground("cell.jpg");
@@ -627,7 +627,7 @@
               definition to consistently replace the following four
               definitions:</para>
 
-              <programlisting>public class Wombat extends Actor {
+              <programlisting language="java">public class Wombat extends Actor {
 ...    
     <emphasis role="bold">private static final int EAST = 0;
     private static final int WEST = 1;
@@ -646,7 +646,7 @@
               <para>First we define an appropriate enumeration within our
               <classname>Wombat</classname> class:</para>
 
-              <programlisting>public class Wombat extends Actor {
+              <programlisting language="java">public class Wombat extends Actor {
 
    <emphasis role="bold">enum Direction {
       EAST, WEST, NORTH, SOUTH
@@ -658,7 +658,7 @@
               We may use this newly defined type to redefine our
               <code>direction</code> attribute:</para>
 
-              <programlisting>public class Wombat extends Actor {
+              <programlisting language="java">public class Wombat extends Actor {
 ...
    <emphasis role="bold">private Direction direction;</emphasis> // used to be "private int direction"
 ...</programlisting>
@@ -666,7 +666,7 @@
               <para>Our constructor <classname>Wombat()</classname> no
               reads:</para>
 
-              <programlisting>   public Wombat() {
+              <programlisting language="java">   public Wombat() {
       <emphasis role="bold">setDirection(Direction.EAST)</emphasis>; // improved readability, cf. setDirection(0)
       leavesEaten = 0;
    }</programlisting>
@@ -674,7 +674,7 @@
               <para>The <code>setDirection</code> method may be re-written
               as:</para>
 
-              <programlisting>   public void setDirection(Direction direction) {
+              <programlisting language="java">   public void setDirection(Direction direction) {
       this.direction = direction;
 
       switch (direction) {
@@ -696,7 +696,7 @@
               <para>This way we got rid of the if clause being present in the
               original code:</para>
 
-              <programlisting>public void setDirection(int direction)
+              <programlisting language="java">public void setDirection(int direction)
     {
         <emphasis role="bold">if ((direction &gt;= 0) &amp;&amp; (direction &lt;= 3)) {
             this.direction = direction;
@@ -709,7 +709,7 @@
               {0,1,2,3}. Re-writing the remaining methods is
               straightforward:</para>
 
-              <programlisting>   /**
+              <programlisting language="java">   /**
     * Move one cell forward in the current direction.
     */
    public void move() {
@@ -835,7 +835,7 @@
             <answer>
               <para>Our <classname>Actor</classname> class contains:</para>
 
-              <programlisting>public class Animal extends Actor
+              <programlisting language="java">public class Animal extends Actor
 {
     private <co linkends="sd1DefWalkingSpeed-1" xml:id="sde1CoPrivate"/> static <co
                   linkends="sd1DefWalkingSpeed-2" xml:id="sde1CoStatic"/> final <co
@@ -883,7 +883,7 @@
               move with equal speed. Modify the <classname>Animal</classname>
               class by adding the following method:</para>
 
-              <programlisting>public void setMyWalkingSpeed(int speed) {...}</programlisting>
+              <programlisting language="java">public void setMyWalkingSpeed(int speed) {...}</programlisting>
 
               <para>This requires the introduction of a new variable within
               our <classname>Animal</classname> class. This variable shall be
@@ -902,7 +902,7 @@
               role="bold">myWalkingSpeed</emphasis> to account for individual
               walking speed values:</para>
 
-              <programlisting>public class Animal extends Actor
+              <programlisting language="java">public class Animal extends Actor
 {
     private static final double WALKING_SPEED = 5.0; <co
                   linkends="sd1QandaWalkingSpeed-1"
@@ -1033,7 +1033,7 @@
               a new attribute <code>nextTurningAngle</code> of type
               <code>int</code>:</para>
 
-              <programlisting>public class Crab extends Animal {
+              <programlisting language="java">public class Crab extends Animal {
 
    <emphasis role="bold">int nexturningAngle = 5;</emphasis> // Initial value
 
@@ -1060,7 +1060,7 @@
               each crab's current state and initialize its value to the first
               of our three states:</para>
 
-              <programlisting>public class Crab extends Animal {
+              <programlisting language="java">public class Crab extends Animal {
    
    int nextTurningAngle = 5; <co xml:id="sd1VarCrabState"/> // Crabs start in state 1
 
@@ -1110,7 +1110,7 @@
               maintainability by introducing three integer constants
               corresponding to our crabs distinct states:</para>
 
-              <programlisting>public class Crab extends Animal {
+              <programlisting language="java">public class Crab extends Animal {
    
    // These definitions are not strictly required but do help
    // understanding the code
@@ -1144,7 +1144,7 @@
 
               <para>Our complete implementation now reads:</para>
 
-              <programlisting>public class Crab extends Animal {
+              <programlisting language="java">public class Crab extends Animal {
    
    // A crab is in exactly one of these states:
    final static int state1 = 5,
@@ -1177,16 +1177,12 @@
   </chapter>
 
   <chapter xml:id="sd1CrabsEnhance">
-    <title>Lecture 5 - Enhancing crabs' behaviour I</title>
+    <title>Lecture 5 - Classes, instances and internal state</title>
 
     <section xml:id="sd1CrabsEnhancePrepare">
       <title>Preparations</title>
 
       <itemizedlist>
-        <listitem>
-          <para>Read chapter 3 of <xref linkend="bibKoelling2010Ger"/>.</para>
-        </listitem>
-
         <listitem>
           <para>Read Chapter 5 from <xref linkend="bibHorton2011"/> excluding
           the sections <quote>Recursion</quote> and <quote>Nested
@@ -1212,16 +1208,211 @@
     <section xml:id="sd1GeometryClasses">
       <title>Dealing with geometry classes</title>
 
+      <qandaset>
+        <title>Rectangles</title>
+
+        <qandaentry>
+          <question xml:id="sd1ImplementRectangle">
+            <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>
+                <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>
+                <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>/**
+ * 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 dummy implementation of the following class
-              <classname>Circle</classname>:</para>
+              <para>Complete the the following class
+              <classname>Circle</classname>'s dummy implementation:</para>
 
-              <programlisting>package step1.dummy;
+              <programlisting language="java">package step1.dummy;
 
 /**
  * A circle of given radius
@@ -1272,7 +1463,7 @@ public class Circle {
               <para>Instances of this class shall be usable in the following
               fashion:</para>
 
-              <programlisting>public class Driver {
+              <programlisting language="java">public class Driver {
 
   public static void main(String[] args) {
     Circle c = new Circle(2.3);
@@ -1295,11 +1486,10 @@ public class Circle {
             </question>
 
             <answer>
-              <para>We define an instance (= non-static) variable radius
-              inside our class <classname>Circle</classname>:</para>
+              <para>We define an instance variable radius inside our class
+              <classname>Circle</classname>:</para>
 
-              <programlisting>package step1;
-...
+              <programlisting language="java">
 public class Circle {
 
   double radius;
@@ -1309,7 +1499,7 @@ public class Circle {
               <para>Next we implement our method to change a circle's
               radius:</para>
 
-              <programlisting>  public void setRadius(double r) {
+              <programlisting language="java">  public void setRadius(double r) {
     radius = r;
   }</programlisting>
 
@@ -1319,7 +1509,7 @@ public class Circle {
               here making it easier for a programmer to recognize the expected
               name in the generated javadoc:</para>
 
-              <programlisting>  public void setRadius(double radius) {
+              <programlisting language="java">  public void setRadius(double radius) {
     <emphasis role="bold">this.</emphasis>radius = radius;
   }</programlisting>
 
@@ -1333,7 +1523,7 @@ public class Circle {
               <para>The rest of the implementation is (quite) straightforward.
               A complete class reads:</para>
 
-              <programlisting>package step1;
+              <programlisting language="java">package step1;
 
 /**
  * A circle of given radius
@@ -1387,9 +1577,13 @@ public class Circle {
   </chapter>
 
   <chapter xml:id="sd1CrabsEnhance2">
-    <title>Lecture 5 - Enhancing crabs' behaviour II</title>
+    <title>Lecture 5 - Enhancing crabs' behaviour</title>
 
-    <para/>
+    <itemizedlist>
+      <listitem>
+        <para>Read chapter 2 of <xref linkend="bibKoelling2010Ger"/>.</para>
+      </listitem>
+    </itemizedlist>
   </chapter>
 
   <chapter xml:id="sd1CrabsFinish">
@@ -1424,7 +1618,7 @@ public class Circle {
             <para>A simple implementation to count worms being eaten
             reads:</para>
 
-            <programlisting>public class Crab extends Animal {
+            <programlisting language="java">public class Crab extends Animal {
 
    <emphasis role="bold">int noOfWormsEaten = 0;          // initially no worm has been eaten</emphasis> 
    // other methods ...
@@ -1464,7 +1658,7 @@ public class Circle {
             solution of <xref linkend="sd1WormEatWorm"/> we need to replace
             the <code>if</code> clause by a <code>while</code> loop:</para>
 
-            <programlisting>   public void lookForWorm() {
+            <programlisting language="java">   public void lookForWorm() {
       <emphasis role="bold">while</emphasis> (canSee(Worm.class)) {
          eat(Worm.class);
          noOfWormsEaten++;
@@ -1491,7 +1685,7 @@ public class Circle {
             <para>Replacing the inner if statement by a switch statement is
             straightforward:</para>
 
-            <programlisting>public class Crab extends Animal {
+            <programlisting language="java">public class Crab extends Animal {
    
    // These definitions are not strictly required but do help
    // understanding the code
@@ -1550,7 +1744,7 @@ public class Circle {
             <para>Implementing a running crab was achieved by introducing two
             image objects in Greenfoot/code 4.2 example:</para>
 
-            <programlisting>public class Crab extends Animal {
+            <programlisting language="java">public class Crab extends Animal {
   private GreenfootImage image1;
   private GreenfootImage image2;
   ...
@@ -1623,7 +1817,7 @@ public class Circle {
           <answer>
             <para>A complete implementation reads:</para>
 
-            <programlisting>public class Crab extends Animal {
+            <programlisting language="java">public class Crab extends Animal {
     
     private final static <co
                 linkends="sd1CrabAnimateStaticImageImplementation-1"
diff --git a/ws/eclipse/Sde1/Figur/src/step1/Driver.java b/ws/eclipse/Sde1/Figur/src/step1/DriverCircle.java
similarity index 94%
rename from ws/eclipse/Sde1/Figur/src/step1/Driver.java
rename to ws/eclipse/Sde1/Figur/src/step1/DriverCircle.java
index f69d06047..0cfc4b56e 100644
--- a/ws/eclipse/Sde1/Figur/src/step1/Driver.java
+++ b/ws/eclipse/Sde1/Figur/src/step1/DriverCircle.java
@@ -1,6 +1,6 @@
 package step1;
 
-public class Driver {
+public class DriverCircle {
 
   public static void main(String[] args) {
     Circle c = new Circle(2.3);
diff --git a/ws/eclipse/Sde1/Figur/src/step1/DriverRctangle.java b/ws/eclipse/Sde1/Figur/src/step1/DriverRctangle.java
new file mode 100644
index 000000000..aa66f1e30
--- /dev/null
+++ b/ws/eclipse/Sde1/Figur/src/step1/DriverRctangle.java
@@ -0,0 +1,19 @@
+package step1;
+
+
+public class DriverRctangle {
+
+  public static void main(String[] args) {
+    
+    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
new file mode 100644
index 000000000..4db06de03
--- /dev/null
+++ b/ws/eclipse/Sde1/Figur/src/step1/Rectangle.java
@@ -0,0 +1,61 @@
+package step1;
+
+/**
+ * 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;
+  }
+}
diff --git a/ws/eclipse/Sde1/Figur/src/step1/dummy/Rectangle.java b/ws/eclipse/Sde1/Figur/src/step1/dummy/Rectangle.java
new file mode 100644
index 000000000..422176489
--- /dev/null
+++ b/ws/eclipse/Sde1/Figur/src/step1/dummy/Rectangle.java
@@ -0,0 +1,57 @@
+package step1.dummy;
+
+/**
+ * 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
+  }
+}
diff --git a/ws/eclipse/Sde1/Figur/src/step2/Vector.java b/ws/eclipse/Sde1/Figur/src/step3/Vector.java
similarity index 89%
rename from ws/eclipse/Sde1/Figur/src/step2/Vector.java
rename to ws/eclipse/Sde1/Figur/src/step3/Vector.java
index f21639c14..cf55d5d4c 100644
--- a/ws/eclipse/Sde1/Figur/src/step2/Vector.java
+++ b/ws/eclipse/Sde1/Figur/src/step3/Vector.java
@@ -1,4 +1,4 @@
-package step2;
+package step3;
 
 public class Vector {
 
-- 
GitLab