jaredec18

Untitled

Sep 17th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. CircleClient.java
  2.  
  3.  
  4. public class CircleClient {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. Point p1 = new Point(10,5);
  9. Circle c1 = new Circle(p1,7);
  10.  
  11. System.out.println(c1);
  12.  
  13. System.out.println("Area: " + c1.getArea());
  14. System.out.println("Circumference: " + c1.getCircumference());
  15.  
  16. Point p2 = new Point(5,7);
  17.  
  18. if(c1.contains(p2 ))
  19. System.out.println("(" + p2 + ") lies within the circle");
  20. else
  21. System.out.println("(" + p2 + ") does not lie within the circle");
  22.  
  23.  
  24.  
  25. }
  26.  
  27. }
  28. Circle.java
  29.  
  30.  
  31. public class Circle {
  32.  
  33. private int radius;
  34. public Point center;
  35.  
  36.  
  37. public Circle(Point center, int radius) {
  38.  
  39. this.radius = radius;
  40. this.center = center;
  41.  
  42. }
  43.  
  44. public Point getCenter() {
  45.  
  46. return center;
  47.  
  48. }
  49.  
  50. public int getRadius() {
  51.  
  52. return radius;
  53.  
  54. }
  55.  
  56. public double getArea() {
  57.  
  58. return (Math.PI * radius * radius);
  59.  
  60. }
  61.  
  62. public double getCircumference() {
  63.  
  64. return (2 * Math.PI * radius);
  65.  
  66. }
  67.  
  68. public String toString() {
  69.  
  70. return "Circle[center=(" + center + "), radius=" + radius + "]";
  71.  
  72. }
  73.  
  74. public boolean contains(Point p) {
  75.  
  76. if(Point.distance(center, p) <= radius)
  77.  
  78. return true;
  79.  
  80. else
  81.  
  82. return false;
  83.  
  84. }
  85.  
  86. }
  87.  
  88. Point.java
  89.  
  90.  
  91. public class Point {
  92.  
  93. private int xp;
  94. private int yp;
  95.  
  96. public Point(int xp, int yp) {
  97.  
  98. this.xp = xp;
  99. this.yp = yp;
  100.  
  101. }
  102.  
  103. public String toString() {
  104.  
  105. return xp + "," + yp;
  106.  
  107. }
  108.  
  109. public static double distance(Point a, Point b) {
  110.  
  111. return Math.sqrt((a.xp - b.xp)*(a.xp - b.xp) + (a.yp - b.yp)*(a.yp - b.yp));
  112.  
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment