ChaseKeskinyan

MyPoint

Oct 11th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. /**
  2. * Write a description of class MyPoint here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. public class MyPoint
  8. {
  9. private int xPoint;
  10. private int yPoint;
  11.  
  12. public MyPoint()
  13. {
  14. xPoint = 0;
  15. yPoint = 0;
  16. }
  17.  
  18. public MyPoint(int x, int y)
  19. {
  20. xPoint = x;
  21. yPoint = y;
  22. }
  23.  
  24. public int getXPoint()
  25. {
  26. return xPoint;
  27. }
  28.  
  29. public int getYPoint()
  30. {
  31. return yPoint;
  32. }
  33.  
  34. public void setXPoint(int x)
  35. {
  36. xPoint = x;
  37. }
  38.  
  39. public void setYPoint(int y)
  40. {
  41. yPoint = y;
  42. }
  43.  
  44. public void setXY(int x, int y)
  45. {
  46. xPoint = x;
  47. yPoint = y;
  48. }
  49.  
  50. public double findDistance(int x, int y)
  51. {
  52. return Math.sqrt((double)Math.pow(x - this.xPoint, 2) + Math.pow(y - this.yPoint, 2));
  53. }
  54.  
  55. public double findDistance(MyPoint another)
  56. {
  57. return findDistance(another.xPoint, another.yPoint);
  58. }
  59.  
  60. public double findSlope(int x, int y)
  61. {
  62. return ((double)y - this.yPoint)/((double)x - this.xPoint);
  63. }
  64.  
  65. public double findSlope(MyPoint another)
  66. {
  67. return findSlope(another.xPoint, another.yPoint);
  68. }
  69.  
  70. public String toString()
  71. {
  72. return "(" + xPoint + "," + yPoint + ")";
  73. }
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. /**
  84. * Write a description of class MyPointClient here.
  85. *
  86. * @author (your name)
  87. * @version (a version number or a date)
  88. */
  89. public class MyPointClient
  90. {
  91. public static void main(String[]args)
  92. {
  93. MyPoint pointA = new MyPoint();
  94. MyPoint pointB = new MyPoint(2,3);
  95. System.out.println("The current coordinate of Point A is " + pointA);
  96. pointA.setXPoint(7);
  97. pointA.setYPoint(2);
  98. System.out.println("The x coordinate of Point A is now " + pointA.getXPoint());
  99. System.out.println("The y coordinate of Point A is now " + pointA.getYPoint());
  100. System.out.println("The current coordinate of Point A is " + pointA);
  101. pointB.setXY(2,2);
  102. System.out.println("The coordinate of Point B is currently " + pointB);
  103. System.out.println("The distance from Point B to Point (5,2) is " + pointB.findDistance(5,2));
  104. System.out.println("The distance from Point A to Point B is " + pointA.findDistance(pointB));
  105.  
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment