Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. // This class defines a circle
  2.  
  3. public class Circle
  4. {
  5. // Create a public constant of type double called PI, initialize it to 3.141593
  6. public static double PI = 3.141593;
  7. // Create two private integer variables, one called x and one called y.
  8. // These will store the center of the circle
  9. private int x, y;
  10. // Create a private double variable called radius
  11. private double radius;
  12. // Create a private static integer variable called numCircles and initialize it to 0
  13. // This variable will keep a count of the number of Circle objects created
  14. private static int numCircles = 0;
  15.  
  16. // Create a default constructor method with no parameters that initializes instance variables
  17. // x and y above to 0 and radius to 1.0
  18. // Don't forget to increment the numCircles variable
  19. public class one()
  20. {
  21. x = 0;
  22. y = 0;
  23. radius = 1;
  24. numCircles++;
  25. }
  26.  
  27. // Create a constructor method that takes two integer parameters, one to set instance variable x and
  28. // one to set instance variable y. Set the radius instance variable to 1.0
  29. // One way to do this is to use the "this" keyword and first call the default constructor method
  30. // above and then set instance variables x,y using the parameters
  31. // Make sure the numCircles variable has been incremented only once
  32. public class two(int x, int y)
  33. {
  34. this.x = x;
  35. this.y = y;
  36. radius = 1;
  37. numCircles++;
  38. }
  39.  
  40. // Create a constructor method that takes three parameters, one for instance variable x, one for y and
  41. // one for radius
  42. // Don't forget to increment the numCircles variable
  43. public class three(int x, int y, double radius)
  44. {
  45. this.x = x;
  46. this.y = y;
  47. this radius = radius;
  48. numCircles++;
  49. }
  50.  
  51. // Create a static method that returns the number of circle objects created
  52. public static int CircleObject ()
  53. {
  54. return numCircles;
  55. }
  56.  
  57. // Create an accessor method (i.e. get method) for the radius instance variable
  58. public int getRadius()
  59. {
  60. return radius;
  61. }
  62. // Create an accessor method (i.e. get method) for the x instance variable
  63. public int getX()
  64. {
  65. return x;
  66. }
  67. // Create an accessor method (i.e. get method) for the y instance variable
  68. public int getY()
  69. {
  70. return y;
  71. }
  72. // Create an instance method called getArea() to compute the area of this circle object
  73. // Recall that the area of a circle is PI*radius*radius
  74. // The method should return a double value
  75. public double getArea()
  76. {
  77. return PI*radius*radius;
  78. }
  79.  
  80. // Create a static method also called getArea() that takes one parameter - a radius value
  81. // This value has nothing to do with the radius variable declared above - it is passed in
  82. // when this method is called from the main() method
  83. // This method uses the given radius parameter, computes the area of a circle using this value,
  84. // then returns the area
  85. public static double getArea(int radius)
  86. {
  87. return PI*radius*radius;
  88. }
  89.  
  90. // Create a method called getDescription() that returns a String
  91. // The string returned contains a concatenation of:
  92. // The string "Circle[ Radius = " followed by the radius of this circle followed by the string
  93. // " X = " followed by the x value of this circle followed by the string " Y = " followed by
  94. // the y value of this circle followed by the string "]"
  95. // NOTE: do not call System.out.println() in here, return a String!!!!
  96. public String getDescription()
  97. {
  98. return ("Circle[ Radius = " + radius + " X = " + x + " Y = " + y + "]")
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement