Guest User

Untitled

a guest
Jan 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. package hackerrankQuestion.fidelity;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Solution
  6. {
  7.  
  8. // The following code is already present.
  9. public static void main(String[] args)
  10. {
  11. Scanner sc = new Scanner(System.in);
  12. int x1 = sc.nextInt();
  13. int y1 = sc.nextInt();
  14. int z1 = sc.nextInt();
  15. int x2 = sc.nextInt();
  16. int y2 = sc.nextInt();
  17. int z2 = sc.nextInt();
  18.  
  19. Point2D p1 = new Point2D(x1, y1);
  20. Point2D p2 = new Point2D(x2, y2);
  21.  
  22. Point3D p3D1 = new Point3D(x1, y1, z1);
  23. Point3D p3D2 = new Point3D(x2, y2, z2);
  24.  
  25. double twoDdistance = p1.distanceFrom(p2);
  26. double threeDdistance = p3D1.distanceFrom(p3D2);
  27. p1.printDistance(twoDdistance);
  28. p3D1.printDistance(threeDdistance);
  29.  
  30. }
  31.  
  32. }
  33.  
  34. //we need to implement the following classes
  35. class Point2D
  36. {
  37. int x, y;
  38.  
  39. public Point2D(int x, int y)
  40. {
  41. this.x = x;
  42. this.y = y;
  43. }
  44.  
  45. public double distanceFrom(Point2D p)
  46. {
  47. int subx = p.x - this.x;
  48. int suby = p.y - this.y;
  49.  
  50. return Math.sqrt(((subx * subx) + (suby * suby)));
  51. }
  52.  
  53. public void printDistance(double d)
  54. {
  55. System.out.println("2D distance=" + (int) Math.ceil(d));
  56. }
  57. }
  58.  
  59. class Point3D extends Point2D
  60. {
  61. int z;
  62.  
  63. public Point3D(int x, int y, int z)
  64. {
  65. super(x, y);
  66. this.z = z;
  67. }
  68.  
  69. public double distanceFrom(Point3D p)
  70. {
  71. int subx = p.x - this.x;
  72. int suby = p.y - this.y;
  73. int subz = p.z - this.z;
  74.  
  75. return Math.sqrt(((subx * subx) + (suby * suby) + (subz * subz)));
  76. }
  77.  
  78. public void printDistance(double d)
  79. {
  80. System.out.println("3D distance=" + (int) Math.ceil(d));
  81. }
  82. }
Add Comment
Please, Sign In to add comment