Guest User

Untitled

a guest
Jan 12th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. Editable code:
  2.  
  3. Circle.java
  4.  
  5. import java.awt.*;
  6. import java.util.Objects;
  7.  
  8.  
  9. public class Circle {
  10.  
  11.  
  12. private static final double DEFAULT_RADIUS = 1.0; // unit circle
  13.  
  14.  
  15. private static final Point DEFAULT_CENTER = new Point(0, 0); // origin
  16.  
  17.  
  18. private static final Color DEFAULT_COLOR = Color.BLACK;
  19.  
  20.  
  21. private double myRadius;
  22.  
  23.  
  24. private Point myCenter; // Note that java.awt.Point is mutable
  25.  
  26.  
  27. private Color myColor;
  28.  
  29.  
  30. public Circle(final double theRadius,
  31. final Point theCenter,
  32. final Color theColor) {
  33. if (theRadius <= 0) {
  34. throw new IllegalArgumentException("The radius must be a positive value!");
  35. }
  36. myRadius = theRadius;
  37.  
  38.  
  39. myCenter = (Point) Objects.requireNonNull(theCenter).clone();
  40.  
  41.  
  42.  
  43. myColor = Objects.requireNonNull(theColor);
  44. }
  45.  
  46.  
  47. public Circle() {
  48. this(DEFAULT_RADIUS, DEFAULT_CENTER, DEFAULT_COLOR);
  49. }
  50.  
  51.  
  52. public final void setRadius(final double theRadius) {
  53. if (theRadius <= 0) {
  54. throw new IllegalArgumentException();
  55. }
  56. myRadius = theRadius;
  57. }
  58.  
  59.  
  60. public final void setCenter(final Point thePoint) {
  61.  
  62.  
  63.  
  64. myCenter = (Point) Objects.requireNonNull(thePoint).clone();
  65. }
  66.  
  67.  
  68. public final void setColor(final Color theColor) {
  69. myColor = Objects.requireNonNull(theColor);
  70. }
  71.  
  72.  
  73. public final double getRadius() {
  74. return myRadius;
  75. }
  76.  
  77.  
  78. public final Point getCenter() {
  79.  
  80.  
  81. return (Point) myCenter.clone();
  82. }
  83.  
  84.  
  85. public final Color getColor() {
  86. return myColor;
  87. }
  88.  
  89.  
  90. public double calculateDiameter() {
  91. return 2 * myRadius;
  92. }
  93.  
  94.  
  95. public double calculateCircumference() {
  96. return Math.PI * calculateDiameter();
  97. }
  98.  
  99.  
  100. public double calculateArea() {
  101. return 2 * myRadius * Math.PI;
  102. }
  103.  
  104.  
  105.  
  106. @Override
  107. public String toString() {
  108. final StringBuilder builder = new StringBuilder(128); // default initial size = 16
  109. builder.append(getClass().getSimpleName()); // the class name without the package name
  110. builder.append(" [radius=");
  111. builder.append(String.format("%.2f", myRadius));
  112. builder.append(", center=");
  113. builder.append(myCenter);
  114. builder.append(", color=");
  115. builder.append(myColor);
  116. builder.append(']');
  117. return builder.toString();
  118. }
  119. }
  120. Junittesting.java
  121.  
  122. import java.awt.Color;
  123.  
  124. import java.awt.Point;
  125.  
  126. import java.util.Scanner;
  127.  
  128. public class Junittesting {
  129.  
  130.  
  131.  
  132. public static void main(String[] args) {
  133.  
  134.  
  135.  
  136. Scanner sc=new Scanner(System.in);
  137.  
  138. final double radiues = 3.0;
  139.  
  140. final Point center = new Point(10, 10); // origin
  141.  
  142. final Color color = Color.BLUE;
  143.  
  144.  
  145.  
  146. double myRadius;
  147.  
  148. Circle c=new Circle(radiues,center,color) ;
  149.  
  150. System.out.println("Default Circle");
  151.  
  152. System.out.println(c.toString());
  153.  
  154. System.out.println("Enter the radius");
  155.  
  156. myRadius=sc.nextDouble();
  157.  
  158.  
  159.  
  160. c.setRadius(myRadius);
  161.  
  162.  
  163.  
  164. System.out.println("Enter the Point");
  165.  
  166. int x=sc.nextInt();
  167.  
  168. int y=sc.nextInt();
  169.  
  170. Point newPoint=new Point(x,y);
  171.  
  172. c.setCenter(newPoint);
  173.  
  174.  
  175.  
  176.  
  177.  
  178. System.out.println(c.toString());
  179.  
  180. System.out.println("Diameter of the circle"+c.calculateDiameter());
  181.  
  182.  
  183.  
  184. System.out.println("Circumference of the circle"+ c.calculateCircumference());
  185.  
  186.  
  187.  
  188. System.out.println("Area of the circle"+ c.calculateArea());
  189.  
  190.  
  191.  
  192.  
  193.  
  194. }
  195.  
  196.  
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment