Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. /**
  2. *Circle class
  3. */
  4.  
  5. public class Circle implements Comparable{
  6. private static final double PI = 3.14;
  7. private double radius;
  8.  
  9. /**
  10. *constructor
  11. *pre: none
  12. *post: A Circle object created. Radius initialized to 1.
  13. */
  14.  
  15. public Circle(){
  16. radius = 1;
  17. }
  18.  
  19. public Circle(double r){
  20. radius = r;
  21. }
  22. public void setRadius(double newRadius){
  23. radius = newRadius;
  24. }
  25. public double getRadius(){
  26. return radius;
  27. }
  28. //....getRadius(), setRadius(), and other Circle class
  29. //methods
  30.  
  31. /**
  32. *Determines if object c is smaller, the same,
  33. *or larger than this Circle object.
  34. *pre: c is a Circle object
  35. *post: -1 has been returned if c is larger than
  36. *this Circle, 0 has been returned if they are the same
  37. *size, and 1 has been returned if c is smaller than this Cirlce.
  38. */
  39. public int compareTo(Object c){
  40. Circle testCircle = (Circle) c;
  41.  
  42. if(radius < testCircle.getRadius()){
  43. return (-1);
  44. }
  45. else if( radius == testCircle.getRadius()){
  46. return (0);
  47. }
  48. else{
  49. return (1);
  50. }
  51. }
  52.  
  53. public String toString(){
  54. String str = "Circle with radius " + radius;
  55. return str;
  56. }
  57.  
  58. public double area(){
  59. double area = 3.14 * radius * radius;
  60. return area;
  61. }
  62.  
  63.  
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement