Advertisement
Guest User

Circle

a guest
Oct 21st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. package ex2;
  2.  
  3. /**
  4. * Circle class
  5. *
  6. * @author Hi.Im.darkness
  7. */
  8. public class Circle extends Shape {
  9.     /**
  10.     * This is a constant variable of class
  11.     * This is value of PI in mathimetic
  12.     */
  13.     private static final double PI = 3.14;
  14.  
  15.     /**
  16.     * Radius of circle
  17.     */
  18.     private double radius;
  19.  
  20.     /**
  21.     * Default Constructor
  22.     */
  23.     public Circle() {
  24.         setRadius(1.0);
  25.     }
  26.    
  27.     /**
  28.     * Contructor arguments
  29.     *
  30.     * @param _radius  double type
  31.     */
  32.     public Circle(double _radius) {
  33.         setRadius(_radius);
  34.     }
  35.  
  36.     /**
  37.     * Constructor arguments
  38.     *
  39.     * @param _color    String type
  40.     * @param _filled   boolean type
  41.     * @param _radius   double type
  42.     */
  43.     public Circle(double _radius, String _color, boolean _filled) {
  44.         super(_color, _filled);
  45.         setRadius(_radius);
  46.     }
  47.  
  48.     /**
  49.     * Set radius of circle
  50.     *
  51.     * @param _radius   double type
  52.     */
  53.     public void setRadius(double _radius) {
  54.         radius = _radius;
  55.     }
  56.  
  57.     /**
  58.     * Get radius of Circle
  59.     *
  60.     * @return double type
  61.     */
  62.     public double getRadius() {
  63.         return radius;
  64.     }
  65.  
  66.     /**
  67.     * Compute and return area of Circle
  68.     *
  69.     * @return double type
  70.     */
  71.     public double getArea() {
  72.         return PI * radius * radius;
  73.     }
  74.  
  75.     /**
  76.     * Compute and return perimeter of Circle
  77.     *
  78.     * @return double type
  79.     */
  80.     public double getPerimeter() {
  81.         return PI * 2 * radius;
  82.     }
  83.  
  84.     /**
  85.     * {@inheritDoc}
  86.     *
  87.     * @return addition infomation about radius, area and perimeter of object
  88.     */
  89.     @Override
  90.     public String toString() {
  91.         String shapeStr = super.toString();
  92.         String radiusStr = String.format("%-10s:%10s%n", "Radius", radius);
  93.         String areaStr = String.format("%-10s:%10s%n", "Area", getArea());
  94.         String perimeterStr = String.format("%-10s:%10s%n", "Perimeter", getPerimeter());
  95.         return shapeStr + radiusStr + areaStr + perimeterStr;
  96.     }
  97.  
  98.     public static void main(String[] args) {
  99.         System.out.println(new Circle());
  100.         System.out.println(new Circle(2));
  101.         System.out.println(new Circle(2, "black", true));
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement