# can someone tell me if my program looks right??

• 01-27-2011, 06:14 AM
clemsontigers
can someone tell me if my program looks right??
these are the instructions that i was given

Write a program that calculates the area of a circle from its radius. The radius will be an integer entered via the keyboard.
Use this formula : area = PI * radius2
You will need to use the constant PI which you get by using Math.PI
A sample run would look like this :
The area is : 28.274333882308138

when i try to run the program nothing happens??

cross posted thanks
http://www.javaprogrammingforums.com...html#post25575

Code:

```import java.util.Scanner; public class Assign1_Roberts{         public static void main(String[]args){                 Scanner input = new Scanner(System.in);                 //Prompt the user for input                 final double PI = 3.14159;//Declare a constant                                 //Assign a radius                 double radius = input.nextInt();                                 //Compute area                 double area = radius * radius * PI;                                 System.out.println("Enter radius :" + input.nextInt());                 System.out.println("The radius is : " + radius);                 System.out.println("The area is: " + area);                         } }```
• 01-27-2011, 06:31 AM
gcalvin
Code:

```import java.util.Scanner; public class Assign1_Roberts{         public static void main(String[]args){                 Scanner input = new Scanner(System.in);                 //Prompt the user for input                 final double PI = 3.14159;//Declare a constant [COLOR="Blue"]// aren't you supposed to use Math.PI?[/COLOR]                                 //Assign a radius                 double radius = input.nextInt();                                 //Compute area                 double area = radius * radius * PI;                                 System.out.println("Enter radius :" + input.nextInt()); [COLOR="Blue"]// why are you trying to input a new radius here?[/COLOR]                 System.out.println("The radius is : " + radius);                 System.out.println("The area is: " + area);                         } }```
-Gary-
• 01-27-2011, 06:33 AM
Junky
Does absolutely nothing???

Does it compile? If not post error messages.
Does it run but give runtime errors? Once again post error messages.

Make sure that your class and the .java file have the same name. Remember Java is case sensitive.

Code:

`final double PI = 3.14159;`
That doesn't follow the instructions you were given.
• 01-27-2011, 06:34 AM
clemsontigers
Yeah I'm not exactly sure how to implement it...and I'm using eclipse and when I run it there is no output
• 01-27-2011, 06:39 AM
the beginner
try this one if it'll help :)

import java.util.*;

public class Assign1_Roberts
{
static Scanner console = new Scanner(System.in);

public static void main(String[]args)
{

final double PI = 3.14159;//Declare a constant

double area = 0;

radius = console.nextInt(); //if you declare your variable as Double you should put nextInt()
//and you should get the input from the user first then the calculation :D

//Compute area

System.out.println("The area is: " + String.format("%.2f" , area)); //to format your output with 2decimal places

}
}
• 01-27-2011, 06:40 AM
Junky
Do not spoonfeed. They learn nothing other than it is ok to cheat.
• 01-27-2011, 05:59 PM
clemsontigers
alright here is my updated code..and my out put will be posted at the bottom...getting the correct results just tell me if I am on the right track thanks

Code:

```import java.io.*; public class Assign1_Roberts{   public static void main(String[] args){     double radius = 0;           try{         BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));         System.out.println("Enter Radius  : ");         radius = Integer.parseInt(br1.readLine());         System.out.println("Radius is  : " + radius);         double area = java.lang.Math.PI*radius*radius;         System.out.println("The Area is : "+area);               }       catch(Exception e){         System.out.println("Error : "+e);       }          } }```

3
The Area is : 28.274333882308138
• 01-27-2011, 08:14 PM
gcalvin
Quote:

Originally Posted by clemsontigers
alright here is my updated code..and my out put will be posted at the bottom...getting the correct results just tell me if I am on the right track thanks

Code:

```import java.io.*; public class Assign1_Roberts{   public static void main(String[] args){     double radius = 0;           try{         BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));         System.out.println("Enter Radius  : ");         radius = Integer.parseInt(br1.readLine());         System.out.println("Radius is  : " + radius);         double area = java.lang.Math.PI*radius*radius;         System.out.println("The Area is : "+area);               }       catch(Exception e){         System.out.println("Error : "+e);       }          } }```

3
The Area is : 28.274333882308138

You're mostly OK. If you don't want the input on a separate line, use print() instead of println() for your prompt. Most people would use a Scanner rather than a BufferedReader/InputStreamReader for the input. You may want to take a look at that (you don't really need buffering for the keyboard unless somebody's going to type that integer really fast). Also, your Exception handling isn't quite right -- test it by typing "three" instead of "3" at the prompt.

Finally, style points (I'd be awarding/deducting points for style; I don't know if your instructor will): put a space between the class name and the opening brace, a space before the opening brace of your main() method, a space after try, a space after catch, and a space before and after your inline operators (+ and *).

-Gary-
• 01-27-2011, 08:18 PM
gcalvin
Oh, and radius is supposed to be an int (according to your first post and your input code) so declare it as an int, and not a double.

-Gary-
• 01-28-2011, 02:09 AM
clemsontigers
alright here is my updated code before i turn it in...i believe its right

my output will be posted under

Code:

```import java.util.Scanner; public class Assign1_Roberts {           public static void main(String[] args){                   //Create a Scanner                 Scanner input = new Scanner(System.in);               int radius = 0;                   System.out.println("Enter Radius : ");         radius = input.nextInt();                 System.out.println("Radius is : " + radius);         double area = java.lang.Math.PI*radius*radius;         System.out.println("The Area is : "+area);                       } }```
3
The Area is : 28.274333882308138
• 01-28-2011, 02:19 AM
dawgpwnd
Quote:

Originally Posted by clemsontigers
alright here is my updated code before i turn it in...i believe its right

my output will be posted under

Code:

```import java.util.Scanner; public class Assign1_Roberts {           public static void main(String[] args){                   //Create a Scanner                 Scanner input = new Scanner(System.in);               int radius = 0;  [B][COLOR="Blue"]//not needed[/COLOR][/B]                   System.out.println("Enter Radius : ");         [COLOR="Red"][B]int [/B][/COLOR]radius = input.nextInt();                 System.out.println("Radius is : " + radius);         double area = java.lang.Math.PI*radius*radius;         System.out.println("The Area is : "+area);                       } }```