Advertisement
Guest User

Circle Area

a guest
Sep 2nd, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.01 KB | None | 0 0
  1. /**************************************************************************************************
  2. * Program Name:     Computing the Area of a Circle
  3. * Author:           Terry Weiss
  4. * Date:             September 7, 2015
  5. * Course/Section:   CSC 111-003W
  6. * Program Description:
  7. *    This program calculates and displays the area of a circle using a radius provided by the user.
  8. ***************************************************************************************************/
  9.  
  10. import java.util.Scanner;
  11.  
  12. /*
  13.  *  This class will calculate and display the area of a circle.
  14.  *
  15.  *  Attributes:
  16.  *      PI – This is the decimal value of pi.
  17.  *      PI_SYMBOL – This is the Unicode character for pi (π).
  18.  *      radius – This is the radius of the circle.
  19.  *      circle – This is the instantiated WeissLab2 object.
  20.  *  Behaviors:
  21.  *      setRadius – This will allow the radius to be changed if necessary, and
  22.  *                  will return the new radius.
  23.  *      getRadius – This will allow the radius to be retrieved outside the object.
  24.  *      getArea – This will calculate and return the area of the circle.
  25.  *      factoredArea – This will generate the area as a factor of pi, so it can be seen in
  26.  *                     a simplified and mathematically precise visual form.
  27.  *
  28.  *  Algorithm:
  29.  *  Get the radius
  30.  *  Calculate the area of the circle
  31.  *  Display the area of the circle
  32.  */
  33. public class WeissLab2 {
  34.  
  35.     /*
  36.      *  This is the decimal value of pi.
  37.      */
  38.     public final Double PI = 3.14159265;
  39.  
  40.     /*
  41.      *  This is the Unicode character for pi (π).
  42.      */
  43.     public final Character PI_SYMBOL = '\u03C0';
  44.  
  45.     /*
  46.      *  This is the radius of the circle. This is stored as a double since it will be working with
  47.      *  PI, which is also a double.
  48.      */
  49.     private double radius;
  50.  
  51.  
  52.  
  53.     /*
  54.      *  This is the constructor for the WeissLab2 object. It will set the radius of the circle
  55.      *  based on the input provided.
  56.      */
  57.     public WeissLab2( double r )
  58.     {
  59.         radius = r;
  60.     }
  61.  
  62.     /*
  63.      *  This method allows the radius to be changed if necessary, and will return the new radius.
  64.      */
  65.     public Double setRadius( double r )
  66.     {
  67.         return (radius = r);
  68.     }
  69.  
  70.     /*
  71.      *  This method allows the radius to be retrieved outside the object.
  72.      */
  73.     public Double getRadius()
  74.     {
  75.         return radius;
  76.     }
  77.  
  78.     /*
  79.      *  This method calculates the area of the circle.
  80.      *
  81.      *  Area = pi * r^2
  82.      */
  83.     public Double getArea()
  84.     {
  85.         return (PI * (radius * radius));
  86.     }
  87.  
  88.     /*
  89.      *  This method generates the area as a factor of pi, so it can be seem in a simplified
  90.      *  and mathematically precise visual form.
  91.      *
  92.      *  Algorithm:
  93.      *  Calculate the square of radius
  94.      *  Convert to a String
  95.      *  Append PI_SYMBOL
  96.      */
  97.     public String factoredArea()
  98.     {
  99.         return (Double.toString(radius * radius) + PI_SYMBOL);
  100.     }
  101.  
  102.  
  103.  
  104.  
  105.     /*
  106.      *  This program will ask the user for a circle's radius and then calculate and
  107.      *  print out the circle's area.
  108.      *
  109.      *  Algorithm:
  110.      *  Get the radius (radius?)
  111.      *  Make sure radius is valid, or ask again
  112.      *  Create a WeissLab2 object using the radius to perform the calculations
  113.      *  Define PI
  114.      *  Calculate the area of the circle (getArea())
  115.      *      getArea { PI * (radius * radius) }
  116.      *  Generate the area as a factor of pi (factoredArea())
  117.      *      factoredArea { radius * radius + PI_SYMBOL }
  118.      *  Display area of circle as a decimal and as a factor of pi (area(), factoredArea())
  119.      */
  120.     public static void main( String[] args )
  121.     {
  122.         Scanner user_input = new Scanner(System.in);
  123.  
  124.         /*
  125.          *  dimensions is used to capture the user's requested radius
  126.          */
  127.         String dimensions;
  128.  
  129.         /*
  130.          *  r is used to see how big the radius is as a number
  131.          */
  132.         Double r = 0.0;
  133.  
  134.  
  135.         // Continue to ask for the radius until it's within bounds
  136.         while( r <= 0.0 )
  137.         {
  138.             System.out.print("What is the radius of the circle?  ");
  139.             dimensions = user_input.next();
  140.  
  141.             try {
  142.                 r = Double.parseDouble(dimensions);
  143.             } catch( NumberFormatException e ) {
  144.                 System.out.println("If only we could measure dimensions without numbers! "
  145.                     + "Try a positive number.");
  146.                 continue;
  147.             }
  148.  
  149.             if( r <= 0 )
  150.             {
  151.                 System.out.println("That's bigger on the inside than it is on the outside! "
  152.                     + "Try a positive number.");
  153.             }
  154.         }
  155.  
  156.  
  157.         /*
  158.          *  circle is a WeissLab2 object instantiated using the user's radius
  159.          */
  160.         WeissLab2 circle = new WeissLab2(r);
  161.  
  162.         System.out.println("Your circle has an area of " + circle.getArea().toString() +
  163.                 " (" + circle.factoredArea() + ").");
  164.     }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement