diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-02-18 10:40:32 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-18 10:40:32 +0000 |
| commit | 8952297006cc07b7393cf13a78d24fb1cb7ff99d (patch) | |
| tree | 95858365980f672a9486a45c8766c393256ab55a /challenge-152/lubos-kolouch/java/ch-2.java | |
| parent | 3acd29f6195bfb005f78108c56423a018f2d44c4 (diff) | |
| parent | c4a63597ef2a392701b49be29f7615f0cad91cde (diff) | |
| download | perlweeklychallenge-club-8952297006cc07b7393cf13a78d24fb1cb7ff99d.tar.gz perlweeklychallenge-club-8952297006cc07b7393cf13a78d24fb1cb7ff99d.tar.bz2 perlweeklychallenge-club-8952297006cc07b7393cf13a78d24fb1cb7ff99d.zip | |
Merge pull request #5668 from LubosKolouch/master
Challenge 152 LK Perl Python PHP Java
Diffstat (limited to 'challenge-152/lubos-kolouch/java/ch-2.java')
| -rw-r--r-- | challenge-152/lubos-kolouch/java/ch-2.java | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/challenge-152/lubos-kolouch/java/ch-2.java b/challenge-152/lubos-kolouch/java/ch-2.java new file mode 100644 index 0000000000..85e10266cb --- /dev/null +++ b/challenge-152/lubos-kolouch/java/ch-2.java @@ -0,0 +1,58 @@ +class Point { + int x, y; + + public Point(int x, int y) { + this.x = x; + this.y = y; + } +}; + +class Rectangle { + Point left, right; + + public Rectangle(Point left, Point right) { + this.left = left; + this.right = right; + } + + public int get_area() { + return Math.abs(this.left.x - this.right.x) * + Math.abs(this.left.y - this.right.y); + } +}; + +class OverlapArea { + + static int get_total_area(Rectangle first, Rectangle second) { + int area1 = first.get_area(); + int area2 = second.get_area(); + + // calculate the overlapping area + int x_dist = Math.min(first.right.x, second.right.x) - + Math.max(first.left.x, second.left.x); + int y_dist = Math.min(first.right.y, second.right.y) - + Math.max(first.left.y, second.left.y); + int areaI = 0; + if (x_dist > 0 && y_dist > 0) { + areaI = x_dist * y_dist; + } + + return (area1 + area2 - areaI); + } + + public static void main(String[] args) { + Rectangle first = new Rectangle(new Point(-1, 0), new Point(2, 2)); + Rectangle second = new Rectangle(new Point(0, -1), new Point(4, 4)); + + if (get_total_area(first, second) != 22) { + System.out.println("Failed test 1"); + } + + first = new Rectangle(new Point(-3, -1), new Point(1, 3)); + second = new Rectangle(new Point(-1, -3), new Point(2, 2)); + + if (get_total_area(first, second) != 25) { + System.out.println("Failed test 2"); + } + } +} |
