You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TwoDShape.java 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. */
  10. package tracing;
  11. /**
  12. * TwoDShape is an abstract class that defines generic functionality
  13. * for 2D shapes.
  14. */
  15. public abstract class TwoDShape {
  16. /**
  17. * Coordinates of the center of the shape.
  18. */
  19. protected double x, y;
  20. protected TwoDShape(double x, double y) {
  21. this.x = x; this.y = y;
  22. }
  23. /**
  24. * Returns the x coordinate of the shape.
  25. */
  26. public double getX() { return x; }
  27. /**
  28. * Returns the y coordinate of the shape.
  29. */
  30. public double getY() { return y; }
  31. /**
  32. * Returns the distance between this shape and the shape given as
  33. * parameter.
  34. */
  35. public double distance(TwoDShape s) {
  36. double dx = Math.abs(s.getX() - x);
  37. double dy = Math.abs(s.getY() - y);
  38. return Math.sqrt(dx*dx + dy*dy);
  39. }
  40. /**
  41. * Returns the perimeter of this shape. Must be defined in
  42. * subclasses.
  43. */
  44. public abstract double perimeter();
  45. /**
  46. * Returns the area of this shape. Must be defined in
  47. * subclasses.
  48. */
  49. public abstract double area();
  50. /**
  51. * Returns a string representation of 2D shapes -- simply its
  52. * coordinates.
  53. */
  54. public String toString() {
  55. return (" @ (" + String.valueOf(x) + ", " + String.valueOf(y) + ") ");
  56. }
  57. }