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

Missing method comments, chaining example in App class

parent bb95708d
No related branches found
No related tags found
No related merge requests found
......@@ -33,7 +33,7 @@
<para>We might implement translation (move) related parameters and methods
in both classes Rectangle and Circle independently. But since the
underlying operation is indeed shape independent we choose a different
approach using inheritance.</para>
approach using inheritance in the subsequent exercises.</para>
<section xml:id="sd1SectFigureBaseClass">
<title>Defining a <classname>Figure</classname> class hierarchy.</title>
......@@ -72,12 +72,55 @@
linkend="sd1FigFigureMove"/> and the two methods
<methodname>getArea()</methodname> and
<methodname>getPerimeter()</methodname>.</para>
<para>Why is the <methodname>move(...)</methodname> method being
defined returning an instance of class
<classname>Figure</classname>?</para>
</question>
<answer>
<annotation role="make">
<para role="eclipse">Sd1/Figure/BaseClass</para>
</annotation>
<para>Defining <methodname>move(...)</methodname> returning an
instance of class <classname>Figure allows for operation
chaining:</classname></para>
<programlisting language="none">final Circle c = new Circle(1., 2., 3.0);
c.move(1, 5).move(-3, 7);</programlisting>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
<section xml:id="sd1SectFigureScale">
<title>Scaling figures</title>
<qandaset defaultlabel="qanda" xml:id="sd1QandaFigureScaling">
<qandadiv>
<qandaentry>
<question>
<para>We want allow for scaling of figures:</para>
<mediaobject>
<imageobject>
<imagedata fileref="Ref/Fig/figureScale.fig"/>
</imageobject>
</mediaobject>
<para>In the above example the rectangle is being
<quote>inflated</quote> whereas the circle is being shrunk to
half its original size. Add a corresponding scale(...) method to
the Figure inheritance hierarchy which allows for operation
chaining as well.</para>
</question>
<answer>
<annotation role="make">
<para role="eclipse">Sd1/Figure/XXX</para>
</annotation>
</answer>
</qandaentry>
</qandadiv>
......
......@@ -11,6 +11,8 @@ public class App {
final Circle c = new Circle(1., 2., 3.0);
System.out.println(c.getPerimeter());
c.move(1, 5).move(-3, 7);
}
}
package de.hdm_stuttgart.mi.sd1.figure.model;
/**
*
*/
public class Circle extends Figure {
private double radius;
/**
* Extending {@link Figure#Figure(double, double)} by adding parameter radius
* on top of center coordinates.
*
* @param x
* @param y
* @param radius
*/
public Circle(double x, double y, double radius) {
super(x, y);
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;
}
......
package de.hdm_stuttgart.mi.sd1.figure.model;
/**
* @author goik
*
*/
public abstract class Figure {
private double x,y;
/**
* Creating a figure with given center coordinates
*
* @param x
* @param y
*/
public Figure(double x, double y) {
setX(x);
setY(y);
}
// To be implemented in derived classes Rectangle and Circle
/**
*
* @return The current figure's area e.g. with times height in case of a rectangle
*/
public abstract double getArea();
/**
* @return The current figure's perimeter e.g. with 2 * (height + width) in case of a rectangle
*/
public abstract double getPerimeter();
public double getX() { return x;}
......@@ -18,6 +36,13 @@ public abstract class Figure {
public double getY() { return y;}
public void setY(double y) { this.y = y;}
/**
* Translating a figure by a given vector (x,y).
*
* @param x
* @param y
* @return The current object.
*/
public final Figure move(double x, double y) {
setX(getX() + x);
setY(getY() + y);
......
......@@ -4,10 +4,16 @@ public class Rectangle extends Figure {
// Instance variables representing a rectangle's additional parameters
private double width, height;
/**
*
* @param width The rectangle's width
* @param heigth The rectangle's height
* Extending {@link Figure#Figure(double, double)} by adding parameters
* width and height on top of center coordinates.
*
* @param x
* @param y
* @param width
* @param height
*/
public Rectangle(double x, double y, double width, double height) {
super(x, y);
......
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