diff options
Diffstat (limited to 'sem5/oop/m4/shapes/src/MyRectangle.java')
-rw-r--r-- | sem5/oop/m4/shapes/src/MyRectangle.java | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/sem5/oop/m4/shapes/src/MyRectangle.java b/sem5/oop/m4/shapes/src/MyRectangle.java new file mode 100644 index 0000000..591d7d1 --- /dev/null +++ b/sem5/oop/m4/shapes/src/MyRectangle.java @@ -0,0 +1,22 @@ + +public class MyRectangle implements Shape2d{ + private MyPoint a, b; + + /** + * Creates a new rectangle where a, b are opposing corners. + */ + public MyRectangle(MyPoint a, MyPoint b) { + this.a = a; + this.b = b; + } + + public double area() { + MyPoint size = this.b.sub(this.a); + return Math.abs(size.getX() * size.getY()); + } + + public double perimeter() { + MyPoint size = this.b.sub(this.a).abs(); + return size.getX() * 2 + size.getY() * 2; + } +} |