Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. public class CircleTester
  2. {
  3. public static void main(String[] args)
  4. {
  5. Circle one = new Circle(10, "blue", 50, 35);
  6. Circle two = new Circle(10, "blue", 50, 35);
  7. Circle three = new Circle(20, "red", 0, 0);
  8. Circle four = three;
  9.  
  10. // Modify this program to correctly compare objects
  11. // We should not be comparing objects using ==
  12.  
  13. if(one.equals(two))
  14. {
  15. System.out.println("Circles one and two are equal!");
  16. System.out.println(one);
  17. System.out.println(two);
  18. }
  19.  
  20. if(three.equals(four))
  21. {
  22. System.out.println("Circles three and four are equal!");
  23. System.out.println(three);
  24. System.out.println(four);
  25. }
  26. }
  27. }
  28. public class Circle
  29. {
  30. private int radius;
  31. private String color;
  32. private int x;
  33. private int y;
  34.  
  35. public Circle(int theRadius, String theColor, int xPosition, int yPosition)
  36. {
  37. radius = theRadius;
  38. color = theColor;
  39. x = xPosition;
  40. y = yPosition;
  41. }
  42.  
  43. public int getRadius()
  44. {
  45. return radius;
  46. }
  47.  
  48. public int getX()
  49. {
  50. return x;
  51. }
  52.  
  53. public int getY()
  54. {
  55. return y;
  56. }
  57.  
  58. public String getColor()
  59. {
  60. return color;
  61. }
  62.  
  63. // Implement a toString method and an equals method here!
  64. public String toString()
  65. {
  66. // Change this!
  67. return color + " circle with a radius of " + radius + " at position (" + x + ", " + y + ")";
  68. }
  69.  
  70. public boolean equals(Circle other)
  71. {
  72. // Change this!
  73. return radius == other.getRadius() && color.equals(other.getColor()) && x == other.getX() && y ==other.getY();
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement