liangm20

Unit 3: Class Point

Sep 25th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. //object of the class Point
  2. public class PointMain3
  3. {
  4. public static void main(String[] args)
  5. {
  6. Point p1 =new Point(2,3);
  7. Point p2 =new Point();
  8. System.out.println("p1: (" + p1.GetX() + ", " + p1.GetY() + ")");
  9. System.out.println("p2: (" + p2.GetX() + ", " + p2.GetY() + ")");
  10. p2.SetX(3);
  11. p2.SetY(5);
  12. System.out.println("I changed my p2.");
  13. System.out.println("p2: (" + p2.GetX() + ", " + p2.GetY() + ")");
  14. p2.translateX(-4);
  15. p2.translateY(3);
  16. System.out.println("I translated p2.");
  17. System.out.println("p2: (" + p2.GetX() + ", " + p2.GetY() + ")");
  18. System.out.println("The distance between p1 and p2 is " + p2.calcdistance(2, -1, 3, 8));
  19. System.out.println("The slope of the line that p1 and p2 create is " + p2.calcSlope(2,-1,3,8));
  20. }
  21. }
  22. //the main class
  23. public class Point
  24. {
  25. private int x;
  26. private int y;
  27. public Point()
  28. {
  29. x=1;
  30. y=1;
  31. }
  32. public Point(int x1, int y1)
  33. {
  34. x=x1;
  35. y=y1;
  36. }
  37. public int GetX()
  38. {
  39. return x;
  40. }
  41. public int GetY()
  42. {
  43. return y;
  44. }
  45. public void SetX(int x1)
  46. {
  47. x=x1;
  48. }
  49. public void SetY(int y1)
  50. {
  51. y=y1;
  52. }
  53. public void translateX(int t1)
  54. {
  55. x = x+t1;
  56. }
  57. public void translateY(int t2)
  58. {
  59. y = y+t2;
  60. }
  61. public static double calcdistance(int x, int x1, int y, int y1)
  62. {
  63. return Math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));
  64. }
  65. public static double calcSlope(double x, double x1, double y, double y1)
  66. {
  67. return (y1-y)/(x1-x);
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment