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

1. Member
Join Date
Oct 2010
Posts
44
Rep Power
0

## 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 :
Enter radius : 3
The radius is : 3
The area is : 28.274333882308138

when i try to run the program nothing happens??

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

Java 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

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);

}
}```

2. Senior Member
Join Date
Mar 2010
Posts
953
Rep Power
4
Java 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]

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-

3. 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.

Java Code:
`final double PI = 3.14159;`
That doesn't follow the instructions you were given.

4. Member
Join Date
Oct 2010
Posts
44
Rep Power
0
Yeah I'm not exactly sure how to implement it...and I'm using eclipse and when I run it there is no output

5. Member
Join Date
Jan 2011
Posts
21
Rep Power
0
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

int radius = 0;
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
area = radius * radius * PI;

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

}
}

6. Do not spoonfeed. They learn nothing other than it is ok to cheat.

7. Member
Join Date
Oct 2010
Posts
44
Rep Power
0
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

Java Code:
```import java.io.*;
public class Assign1_Roberts{
public static void main(String[] args){
double radius = 0;
try{
System.out.println("Enter Radius  : ");
System.out.println("The Area is : "+area);

}
catch(Exception e){
System.out.println("Error : "+e);
}
}
}```

3
Radius is : 3.0
The Area is : 28.274333882308138

8. Senior Member
Join Date
Mar 2010
Posts
953
Rep Power
4
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

Java Code:
```import java.io.*;
public class Assign1_Roberts{
public static void main(String[] args){
double radius = 0;
try{
System.out.println("Enter Radius  : ");
System.out.println("The Area is : "+area);

}
catch(Exception e){
System.out.println("Error : "+e);
}
}
}```

3
Radius is : 3.0
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-

9. Senior Member
Join Date
Mar 2010
Posts
953
Rep Power
4
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-

10. Member
Join Date
Oct 2010
Posts
44
Rep Power
0
alright here is my updated code before i turn it in...i believe its right

my output will be posted under

Java 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 : ");

System.out.println("The Area is : "+area);

}
}```
3
Radius is : 3
The Area is : 28.274333882308138

11. Member
Join Date
Jan 2011
Posts
4
Rep Power
0
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

Java 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("The Area is : "+area);

}
}```
3
Radius is : 3
The Area is : 28.274333882308138

Both work.

12. Senior Member
Join Date
Mar 2010
Posts
953
Rep Power
4
It's better. You're indenting is inconsistent, and you still have some +s and *s with no spaces around them. Did you consider using print() rather than println() for that first prompt? Also, you don't need to initialize radius to zero, and you can declare it right on the nextInt() line. I wouldn't say that the way you have it now is wrong, exactly, but if it were my code, I'd change it. And the comment seems unnecessary. I'm nit-picking now -- you know better than I whether your instructor cares about such things.

-Gary-

EDIT: dawgpwnd got in ahead of me on the initialization thing.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•