fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. GeometricObject2D geoObject1 = new ComparableCircle2D(0, 5, 2);
  13. GeometricObject2D geoObject3 = new ComparableCircle2D(0, 0, 2);
  14. System.out.println("geoObject1 overlaps geoObject3: "
  15. + geoObject1.intersect(geoObject3));
  16. }
  17. }
  18. abstract class GeometricObject2D<T extends GeometricObject2D> implements
  19. Comparable<GeometricObject2D> {
  20. public double x, y;
  21.  
  22. GeometricObject2D() {
  23. this.x = 0;
  24. this.y = 0;
  25. }
  26.  
  27. GeometricObject2D(double x, double y) {
  28. this.x = x;
  29. this.y = y;
  30. }
  31.  
  32. public abstract double getArea();
  33.  
  34. public abstract double getPerimeter();
  35.  
  36. public abstract boolean intersect(GeometricObject2D g);
  37.  
  38. @Override
  39. public int compareTo(GeometricObject2D o) {
  40. // TODO Auto-generated method stub
  41. return 0;
  42. }
  43. }
  44. class ComparableCircle2D extends GeometricObject2D<ComparableCircle2D> {
  45.  
  46. public double x, y;
  47. public double radius;
  48.  
  49. ComparableCircle2D() {
  50. super();
  51. this.radius = 1.0;
  52. }
  53.  
  54. ComparableCircle2D(double radius) {
  55. super();
  56. this.radius = Math.abs(radius);
  57. }
  58.  
  59. ComparableCircle2D(double x, double y, double radius) {
  60. super(x, y);
  61. this.radius = Math.abs(radius);
  62. }
  63.  
  64. public double getArea() {
  65. return Math.PI * getRadius() * getRadius();
  66. }
  67.  
  68. public double getPerimeter() {
  69. return 2 * Math.PI * getRadius();
  70. }
  71.  
  72. public void setRadius(double setRadius) {
  73. this.radius = Math.abs(setRadius);
  74. }
  75.  
  76. public double getRadius() {
  77. return radius;
  78. }
  79.  
  80.  
  81.  
  82. public double getX() {
  83. return x;
  84. }
  85.  
  86. public void setX(double x) {
  87. this.x = x;
  88. }
  89.  
  90. public double getY() {
  91. return y;
  92. }
  93.  
  94. public void setY(double y) {
  95. this.y = y;
  96. }
  97.  
  98. @Override
  99. public boolean intersect(GeometricObject2D g) {
  100. ComparableCircle2D other = (ComparableCircle2D) g;
  101. double dx = other.x - getX();
  102. double dy = other.y - getY();
  103. double radi = other.radius + getRadius();
  104. System.out.println("dx=" + dx + ", dy=" + dy + ", radi=" + radi);
  105. return (dx * dx + dy * dy < radi * radi);
  106. }
  107.  
  108. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
dx=0.0, dy=0.0, radi=4.0
geoObject1 overlaps geoObject3: true