Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: can someone tell me if this looks right??

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default can someone tell me if this 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
    can someone tell me if my program looks right?? - Java Forums

    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);
     
    	}
    }
    Last edited by robertsbd; January 27th, 2011 at 12:23 AM.

  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: can someone tell me if this looks right??

    //Assign a radius
    double radius = input.nextInt();
    In this line you are asking for input from the user.

    //Compute area
    double area = radius * radius * PI;
    in this line you are calculating the area.

    System.out.println("Enter radius :" + input.nextInt());
    If you have already taken radius as input , then why you are asking for input in this line too.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: can someone tell me if this looks right??

    +Rep for pointing out the cross post.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: can someone tell me if this looks right??

    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

    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

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: can someone tell me if this looks right??

    Looks right, If that's all the problem stated, then it looks done.
    I'd remove the 2 tabs from the try though -- but that's just semantics.