home *** CD-ROM | disk | FTP | other *** search
/ Borland JBuilder 6 / jbuilder6.iso / Documents / JAVA Programming / examples / 07 / Point3DDist.java < prev    next >
Encoding:
Java Source  |  2000-09-08  |  1.2 KB  |  47 lines

  1. class Point { int x, y;
  2. Point(int x, int y) {
  3. this.x = x;
  4. this.y = y;
  5. double distance(int x, int y) {
  6. int dx = this.x - x;
  7. int dy = this.y - y;
  8. return Math.sqrt(dx*dx + dy*dy);
  9. }
  10. double distance(Point p) {
  11. return distance(p.x, p.y);
  12. }
  13. }
  14. class Point3D extends Point { int z;
  15. Point3D(int x, int y, int z)    {
  16. super(x, y);
  17. this.z = z;
  18. }
  19. double distance(int x, int y,    int z) {
  20. int dx = this.x - x;
  21. int dy = this.y - y;
  22. int dz = this.z - z;
  23. return Math.sqrt(dx*dx + dy*dy + dz*dz);
  24. }
  25. double distance(Point3D other) {
  26. return distance(other.x, other.y, other.z);
  27. }
  28. double distance(int x, int y)    {
  29. double dx = (this.x / z) - x;
  30. double dy = (this.y / z) - y;
  31. return Math.sqrt(dx*dx + dy*dy);
  32. }
  33. }
  34. class Point3DDist {
  35. public static void main(String args[]) {
  36. Point3D p1 = new Point3D(30, 40, 10);
  37. Point3D p2 = new Point3D(0, 0, 0);
  38. Point p = new Point(4, 6);
  39. System.out.println("p1 = " + p1.x + ", " + p1.y + ", " + p1.z);
  40. System.out.println("p2 = " + p2.x + ", " + p2.y + ", " + p2.z);
  41. System.out.println("p = " + p.x + ", " + p.y);
  42. System.out.println("p1.distance(p2) = " + p1.distance(p2));
  43. System.out.println("p1.distance(4, 6) = " + p1.distance(4, 6));
  44. System.out.println("p1.distance(p) = " + p1.distance(p));
  45. } } 
  46.