Results 1 to 12 of 12
  1. #1
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default 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
    		
    		//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);
    		
    	}
    }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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]
    		
    		//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-

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    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. #4
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

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

    Default

    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


    //Assign a radius
    int radius = 0;
    double area = 0;


    System.out.print("Enter radius :");
    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. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

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

  7. #7
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

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

    Enter Radius :
    3
    Radius is : 3.0
    The Area is : 28.274333882308138

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by clemsontigers View Post
    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{
            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);
          }        
      }
    }

    Enter Radius :
    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. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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. #10
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    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 : ");
            radius = input.nextInt();
            
            System.out.println("Radius is : " + radius);
            double area = java.lang.Math.PI*radius*radius;
            System.out.println("The Area is : "+area);
            
                
      }
    }
    Enter Radius :
    3
    Radius is : 3
    The Area is : 28.274333882308138

  11. #11
    dawgpwnd is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by clemsontigers View Post
    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("Radius is : " + radius);
            double area = java.lang.Math.PI*radius*radius;
            System.out.println("The Area is : "+area);
            
                
      }
    }
    Enter Radius :
    3
    Radius is : 3
    The Area is : 28.274333882308138

    Both work.

  12. #12
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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.

Similar Threads

  1. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  2. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 04:17 PM
  3. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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