Advertisement
Shaun_B

Calculating an area of a circle from the radius

Oct 28th, 2012
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. /**
  2.  * This works out the area of a circle A as stated in
  3.  * Worksheet 1 exercise 1 - please see the comments
  4.  * below to see which part of the source code answers
  5.  * the first question. For reference, I expect that your
  6.  * worksheet is available online through your Universities
  7.  * online resources, but it asks to compute the area of a
  8.  * circle as PI * Radius squared.
  9.  *
  10.  * @author Shaun B
  11.  * @version 2012-10-28
  12.  */
  13. import java.lang.Math;
  14. import java.util.Scanner;
  15. public class exerciseOne
  16. {
  17.     // This is a constant available at a class level
  18.     static final double PI = Math.PI;
  19.     // This will read in our keyboard inputs via the Scanner utility
  20.     static Scanner keyboardInput = new Scanner(System.in);
  21.     // This will tell the main routine to continue running:
  22.     static boolean run = true;
  23.     public static void main(String[] args)
  24.     {
  25.         // Local variables available only in this scope:
  26.         float radius = 0.00f;
  27.         float area = 0.00f;
  28.         int readNumber = 0;
  29.         // Our main loop (will continue until run is set to false):
  30.         while (run)
  31.         {
  32.             System.out.println("Please enter the radius of a circle");
  33.             System.out.println("or enter 0 (zero) to exit this demonstration.");
  34.             System.out.print("C:\\>");
  35.             // Because we're using the scanner to read in numbers, we
  36.             // need to try it to catch any errors:
  37.             try
  38.             {
  39.                 // Gets a new instance of the keyboard scanner, which takes in
  40.                 // system inputs from the keyboard, and takes the next integer
  41.                 // value
  42.                 readNumber = keyboardInput.nextInt();
  43.             }
  44.             // This will catch an error in case there is a problem with the
  45.             // keyboard entry, ie, it is not an integer number or has
  46.             // illegal characters such as alpha characters:
  47.             catch (Exception e)
  48.             {
  49.                 System.err.println("Illegal keyboard input in main class");
  50.                 run = false;
  51.                 // This will promptly exit the Java run-time environment:
  52.                 System.exit(0);
  53.             }
  54.             if(readNumber == 0)
  55.             {
  56.                 System.out.println("Goodbye");
  57.                 run = false;
  58.                 break;
  59.             }
  60.             else
  61.             {
  62.                 // This answers the criteria on the worksheet in exercise one:
  63.                 radius = readNumber;
  64.                 // Calculates the area of the circle:
  65.                 area = (float) PI * (radius * radius);
  66.                 System.out.print("The area is: ");
  67.                 // Outputs the value as a decimal number:
  68.                 System.out.format("%f\n",area);
  69.             }
  70.         }
  71.         System.exit(0);
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement