#format java /* Shape OOP test for PythonVsJava */ import junit.framework.*; abstract class Shape { public abstract double area(); public abstract double circumference(); } class Circle extends Shape { protected double r; public Circle(double aRadius) { this.r = aRadius; } public double getRadius() { return r; } public double area() { return Math.PI * r * r; } public double circumference() { return 2 * Math.PI * r; } } class Rectangle extends Shape { protected double w, h; public Rectangle(double aWidth, double aHeight) { this.w = aWidth; this.h = aHeight; } public double getWidth() { return w; } public double getHeight() { return h; } public double area() { return w * h; } public double circumference() { return 2 * (w+h); } } class Square extends Shape { protected double s; public Square(double aSide) { s = aSide; } public double getSide() { return s; } public double area() { return s * s; } public double circumference() { return 4 * s; } } interface Centered { public void setCenter(double aX, double aY); public double getCenterX(); public double getCenterY(); } class CenteredRectangle extends Rectangle implements Centered { private double cx, cy; public CenteredRectangle(double aCx, double aCy, double aWidth, double aHeight) { super(aWidth, aHeight); cx = aCx; cy = aCy; } public void setCenter(double aCx, double aCy) { cx = aCx; cy = aCy; } public double getCenterX() { return cx; } public double getCenterY() { return cy; } } class CenteredCircle extends Circle implements Centered { private double cx, cy; public CenteredCircle(double aCx, double aCy, double aRadius) { super(aRadius); cx = aCx; cy = aCy; } public void setCenter(double aCx, double aCy) { cx = aCx; cy = aCy; } public double getCenterX() { return cx; } public double getCenterY() { return cy; } } class CenteredSquare extends Square implements Centered { private double cx, cy; public CenteredSquare(double aCx, double aCy, double aSide) { super(aSide); cx = aCx; cy = aCy; } public void setCenter(double aCx, double aCy) { cx = aCx; cy = aCy; } public double getCenterX() { return cx; } public double getCenterY() { return cy; } } public class ShapeTest extends TestCase { public static void main(String[] args){ junit.textui.TestRunner.run(ShapeTest.class); } public void testCircle() { Circle myCircle = new Circle(5.5); assertEquals(5.5, myCircle.getRadius(), 0.001); assertEquals(95.0331, myCircle.area(), 0.001); assertEquals(34.557, myCircle.circumference(), 0.001); } public void testRectangle() { Rectangle myRec = new Rectangle(3,4); assertEquals(3, myRec.getWidth(), 0.001); assertEquals(12, myRec.area(), 0.001); } public void testAbstractClass() { Shape[] shapes = new Shape[3]; shapes[0] = new Circle(2.0); shapes[1] = new Rectangle(1, 3.0); shapes[2] = new Rectangle(4.0, 2.0); double total_area = 0; for(int i=0; i < shapes.length; i++) total_area += shapes[i].area(); assertEquals(23.566, total_area, 0.001); } public void testSquare() { Square mySquare = new Square(3); assertEquals(3.0, mySquare.getSide(), 0.001); } public void testCenteredRectangle() { CenteredRectangle myCRec = new CenteredRectangle(1,2,3,4); assertEquals(1.0, myCRec.getCenterX(), 0.001); } public void testCenteredInterface() { Shape[] shapes = new Shape[3]; shapes[0] = new CenteredCircle(1.0, 1.0, 1.0); shapes[1] = new CenteredSquare(2.5, 2, 3); shapes[2] = new CenteredRectangle(2.3, 4.5, 3, 4); double totalArea = 0; double totalDistance = 0; for(int i=0; i < shapes.length; i++) { totalArea += shapes[i].area(); if(shapes[i] instanceof Centered) { Centered c = (Centered)shapes[i]; double cx = c.getCenterX(); double cy = c.getCenterY(); totalDistance += Math.sqrt(cx*cx + cy*cy); } } assertEquals(24.1415, totalArea, 0.001); assertEquals(9.669, totalDistance, 0.001); } }