Advertisement
Guest User

Point

a guest
Dec 3rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. public class Point {
  2. int x;
  3. int y;
  4.  
  5. public Point(int initialX, int initialY){
  6. x = initialX;
  7. y = initialY;
  8. }
  9.  
  10. public Point(){
  11. x = 0;
  12. y= 0;
  13. }
  14. public void translate(int dx, int dy){
  15. x += dx;
  16. y += dy;
  17. }
  18. public int getX(){
  19. return x;
  20. }
  21. public int getY(){
  22. return y;
  23. }
  24. public void setXY(int setX, int setY){
  25. x = setX;
  26. y = setY;
  27. }
  28. // toString method //produces and returns the desired string, e.g (19, 5)
  29. // getQuadrant method //returns the quadrant in which (x, y) lies (or 0 if on an axis)
  30. // distanceToOrigin method //returns distance from (x, y) to origin (use double)
  31.  
  32. public String toString(int x, int y){
  33. return "(" + x + ", " + y + ")";
  34. }
  35. public static int getQuadrant(int x, int y) {
  36. if (x > 0 && y > 0) {
  37. return 1;
  38. } else if (x < 0 && y > 0) {
  39. return 2;
  40. } else if (x < 0 && y < 0) {
  41. return 3;
  42. } else if (x > 0 && y < 0) {
  43. return 4;
  44. } else {
  45. return 0;
  46. }
  47. }
  48. public double distanceToOrigin(){
  49. return Math.sqrt(x*x) + (y*y);
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement