Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. /** Problem Sheet 3, Question 1.
  2. *
  3. * Write a short implementation for a Point class with the following operations:
  4. *
  5. * public double getX() // get the x value
  6. * public double getY() // get the y value
  7. * public void movePoint(double x, double y) // move the point by x and y, relative to current position
  8. * public void setPoint(double x, double y) // set the point to x and y
  9. * public String toString() // write out the value of the point in the form "(x, y)"
  10. * public boolean equals(Point p) // compare if two points are equal
  11. *
  12. * Your class should have 2 constructors, one taking an x and y value, the other setting default values to 0.0.
  13. *
  14. * Additionally, a method called "distance" that returns the euclidian distance between itself and another point,
  15. * passed as a parameter.
  16. *
  17. * Finally, a method called "findMidpoint" that returns a point that is exactly halfway between itself and another point.
  18. * i.e.,
  19. * Point p1 = new Point(0.0,0.0);
  20. * Point p2 = new Point(4.0,4.0);
  21. * Point p3 = p1.findMidpoint(p2);
  22. *
  23. * Your Point class should fit this specification exactly.
  24. */
  25.  
  26. public class Point{
  27. Point p1, p2;
  28. double x, y;
  29.  
  30.  
  31. public Point (double x, double y){
  32. this.x = x;
  33. this.y = y;
  34. }
  35. public double getX() {
  36. return x;
  37. }
  38. public double getY() {
  39. return y;
  40. }
  41. public void setX(double x){
  42. this.x = x;
  43. }
  44. public void setY(double y){
  45. this.y = y;
  46. }
  47. public void setPoint(double newX, double newY){
  48. x = newX;
  49. y = newY;
  50. }
  51. public void movePoint(double newX, double newY){
  52. x = x + newX;
  53. y = y +newY;
  54. }
  55. public String toString() {
  56. return );
  57. }
  58. public boolean equals(Point P) {
  59. return (p1.getX() == p2.getX() && p1.getY() == p2.getY());
  60. }
  61. public double distance(Point p) {
  62. double lengthA = (double)(this.getX() - p.getX());
  63. double lengthB = (double)(this.getY() - p.getY());
  64. double distt = Math.sqrt(((lengthA * lengthA) + (lengthB * lengthB)));
  65. return distt;
  66. }
  67. public Point midPoint(Point p) {
  68. double mx = (p.x + x)/2;
  69. double my = (p.y + y)/2;
  70. return new Point(mx,my);
  71. }
  72. public static void main(String[] args) {
  73. Point p1 = new Point(2,4);
  74. Point p2 = new Point(3,-6);
  75. double distance = p1.distance(p2);
  76. Point p3 = p1.midPoint(p2);
  77. System.out.print(distance); //just to test
  78. }
  79.  
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement