4. AP 2 Praktikum

Die Aufgabenblätter sind hier zu finden: http://www.gm.fh-koeln.de/~ehses/ap/index.html

Aufgabe 1:

Die Testklassen CircleTest und RectangleTest findet ihr unter Aufgabe 2!

Circle.java:

public class Circle extends AbstractShape{

private double radius;

Circle(String name, double radius){
super(name);
this.radius=radius;
}

public double getArea(){
double temp = Math.PI * radius * radius;
return temp;
}

}

Rectangle.java:

public class Rectangle extends AbstractShape{

private double laenge, breite;

Rectangle(String name, double laenge, double breite){
super(name);
this.laenge=laenge;
this.breite=breite;
}

public double getArea(){
double temp = laenge * breite;
return temp;
}
}

AbstractShape.java:

public String toString() {
return getClass().getName()+”.”+ name;
}

Aufgabe 2:

AbstractShapeTest.java:

import junit.framework.TestCase;

public class AbstractShapeTest extends TestCase {
protected IShape s1;
protected IShape s2;
protected IShape s3;
  public void testArea() {
double area13 =  Math.PI * 10.5 * 10.5;
double area2 =  Math.PI * 11.0 * 11.0;

assertEquals(area13, s1.getArea(), 1e-7);
assertEquals(area2, s2.getArea(), 1e-7);
assertEquals(area13, s3.getArea(), 1e-7);
}

public void testName() {
assertEquals(“a”, s1.getName());
assertEquals(“b”, s2.getName());
assertEquals(“c”, s3.getName());
}

public void testCompare() {
assertEquals(0, s1.compareTo(s3));
assertEquals(0, s3.compareTo(s1));
assertTrue(s1.compareTo(s2) < 0);
assertTrue(s2.compareTo(s1) > 0);
assertTrue(s3.compareTo(s2) < 0);
assertTrue(s2.compareTo(s3) > 0);
}

public void testEquals() {
assertFalse(s1.equals(s2));
assertFalse(s1.equals(s3));
assertFalse(s3.equals(s2));
}

public void testToString() {
String className = s1.getClass().getName();
assertEquals(className+”.a”, s1.toString());
assertEquals(className+”.b”, s2.toString());
assertEquals(className+”.c”, s3.toString());
}
}

CircleTest.java:

public class CircleTest extends AbstractShapeTest {

public void setUp() {
s1 = new Circle(“a”, 10.5);
s2 = new Circle(“b”, 11.0);
s3 = new Circle(“c”, 10.5);
}
}

RectangleTest.java:

public class RectangleTest extends AbstractShapeTest {

public void setUp() {
s1 = new Rectangle(“a”, Math.PI, 10.5 * 10.5);
s2 = new Rectangle(“b”, Math.PI, 11.0 * 11.0);
s3 = new Rectangle(“c”, Math.PI, 10.5 * 10.5);
}
}

Aufgabe 3:

ShapeNameComparator.java:

import java.util.Comparator;

public class ShapeNameComparator implements Comparator{

public int compare(Object arg0, Object arg1) {
return (((IShape)arg0).getName().compareTo(((IShape)arg1).getName()));
}
}

Facebooktwittergoogle_pluslinkedinmail

Leave a Reply