Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This works out the area of a circle A as stated in
- * Worksheet 1 exercise 1 - please see the comments
- * below to see which part of the source code answers
- * the first question. For reference, I expect that your
- * worksheet is available online through your Universities
- * online resources, but it asks to compute the area of a
- * circle as PI * Radius squared.
- *
- * @author Shaun B
- * @version 2012-10-28
- */
- import java.lang.Math;
- import java.util.Scanner;
- public class exerciseOne
- {
- // This is a constant available at a class level
- static final double PI = Math.PI;
- // This will read in our keyboard inputs via the Scanner utility
- static Scanner keyboardInput = new Scanner(System.in);
- // This will tell the main routine to continue running:
- static boolean run = true;
- public static void main(String[] args)
- {
- // Local variables available only in this scope:
- float radius = 0.00f;
- float area = 0.00f;
- int readNumber = 0;
- // Our main loop (will continue until run is set to false):
- while (run)
- {
- System.out.println("Please enter the radius of a circle");
- System.out.println("or enter 0 (zero) to exit this demonstration.");
- System.out.print("C:\\>");
- // Because we're using the scanner to read in numbers, we
- // need to try it to catch any errors:
- try
- {
- // Gets a new instance of the keyboard scanner, which takes in
- // system inputs from the keyboard, and takes the next integer
- // value
- readNumber = keyboardInput.nextInt();
- }
- // This will catch an error in case there is a problem with the
- // keyboard entry, ie, it is not an integer number or has
- // illegal characters such as alpha characters:
- catch (Exception e)
- {
- System.err.println("Illegal keyboard input in main class");
- run = false;
- // This will promptly exit the Java run-time environment:
- System.exit(0);
- }
- if(readNumber == 0)
- {
- System.out.println("Goodbye");
- run = false;
- break;
- }
- else
- {
- // This answers the criteria on the worksheet in exercise one:
- radius = readNumber;
- // Calculates the area of the circle:
- area = (float) PI * (radius * radius);
- System.out.print("The area is: ");
- // Outputs the value as a decimal number:
- System.out.format("%f\n",area);
- }
- }
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement