Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 30th, 2012, 01:46 PM
Shaun_B Shaun_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 62 Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 1 h 9 m 54 sec
Reputation Power: 2
Thumbs up Calculating the area of a circle from the radius.

Somewhere hidden in this code, you might be able to find the answer to a problem typically set by academics.
Java Code:
Original - Java Code
  1. /**
  2. * This works out the area of a circle as PI * Radius
  3. * squared. Please carefully look  through the source
  4. * code to see which bit is doing the mathematics. I
  5. * don't think this provides perfect answers, but it's
  6. * close enough.
  7. *
  8. * @author Shaun B
  9. * @version 2012-10-28
  10. **/
  11. import java.lang.Math;
  12. import java.util.Scanner;
  13. public class exerciseOne
  14. {
  15.     // This is a constant available at a class level
  16.     static final double PI = Math.PI;
  17.     // This will read in our keyboard inputs via the Scanner utility
  18.     static Scanner keyboardInput = new Scanner(System.in);
  19.     // This will tell the main routine to continue running:
  20.     static boolean run = true;
  21.     public static void main(String[] args)
  22.     {
  23.         // Local variables available only in this scope:
  24.         float radius = 0.00f;
  25.         float area = 0.00f;
  26.         int readNumber = 0;
  27.         // Our main loop (will continue until run is set to false):
  28.         while (run)
  29.         {
  30.             System.out.println("Please enter the radius of a circle");
  31.             System.out.println("or enter 0 (zero) to exit this demonstration.");
  32.             System.out.print("C:\\>");
  33.             // Because we're using the scanner to read in numbers, we
  34.             // need to try it to catch any errors:
  35.             try
  36.             {
  37.                 // Gets a new instance of the keyboard scanner, which takes in
  38.                 // system inputs from the keyboard, and takes the next integer
  39.                 // value
  40.                 readNumber = keyboardInput.nextInt();
  41.             }
  42.             // This will catch an error in case there is a problem with the
  43.             // keyboard entry, ie, it is not an integer number or has
  44.             // illegal characters such as alpha characters:
  45.             catch (Exception e)
  46.             {
  47.                 System.err.println("Illegal keyboard input in main class");
  48.                 run = false;
  49.                 // This will promptly exit the Java run-time environment:
  50.                 System.exit(0);
  51.             }
  52.             if(readNumber == 0)
  53.             {
  54.                 System.out.println("Goodbye");
  55.                 run = false;
  56.                 break;
  57.             }
  58.             else
  59.             {
  60.                 // This answers the criteria on the worksheet in exercise one:
  61.                 radius = readNumber;
  62.                 // Calculates the area of the circle:
  63.                 area = (float) PI * (radius * radius);
  64.                 System.out.print("The area is: ");
  65.                 // Outputs the value as a decimal number:
  66.                 System.out.format("%f\n",area);
  67.             }
  68.         }
  69.         System.exit(0);
  70.     }
  71. }


Have fun,

Shaun.

Last edited by Shaun_B : October 30th, 2012 at 01:48 PM. Reason: Needed a bit of editing.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Calculating the area of a circle from the radius.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap