Help with Java - Using "Pi"

Discussion in 'OT Technology' started by unknown_soldier, Oct 4, 2004.

  1. I'm working on a project and trying to figure out how to multiply the input Radius by "Pi"

    Is anyone able to help me with this? I'm imported java.math.* already I just can't figure out how to use it in the code. Am I trying to make it harder than it is?
     
  2. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    post your code?
     
  3. darkrick

    darkrick New Member

    Joined:
    Sep 17, 2004
    Messages:
    1,191
    Likes Received:
    0
    Location:
    Brisbane, AU
    PI is a constant variable refered to using Math.PI. So all you'd have to do is have something like:
    Code:
    double result = input_value*Math.PI;
     
  4. I imported
    import java.lang.Math;

    I got it to work finally, but why did I have to put it as:
    double circumference = r * 2 * java.lang.Math.PI;

    why did I have to include java.lang.Math.PI in the code, and not just "PI" ??
     
  5. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    :dunno:
    Code:
    import java.lang.Math.*;
    
    System.out.println(Math.PI);
    works just fine for me.
     
  6. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    remember its:
    Code:
    import java.lang.Math.*;
    
    you need the .* at the end before the ;
     
  7. quzer

    quzer New Member

    Joined:
    Nov 9, 2003
    Messages:
    206
    Likes Received:
    0
    Code:
    public class test {
       public static void main(String args[]) {
    	  System.out.println(Math.PI);
       }
    }
     
     
    
    you don't need to import anything at all, just use Math.PI
     
  8. darkrick

    darkrick New Member

    Joined:
    Sep 17, 2004
    Messages:
    1,191
    Likes Received:
    0
    Location:
    Brisbane, AU
    java.lang is imported, even if you don't specifiy, that's what I got told anyways :s
     
  9. I did have the asterisk in the code, forgot to put it on here.
    I got it to work, but I had to type the entire link into the code.
    " java.lang.Math.PI "

    I'm brand new at programming, so if you notice any really stupid errors, please let me know about it. I'm pretty much reading a book and teaching myself, my professor isn't teaching anything. :rolleyes:
    -------------------------------
    This doesn't work:
    import javax.swing.*;
    import java.lang.Math.*;
    import java.text.NumberFormat;

    public class Circle{
    public static void main(String [] args) {

    String choice = "";

    String inputRadius = JOptionPane.showInputDialog("Enter Radius: ");
    double r = Double.parseDouble(inputRadius);

    double circumference = r * 2 * PI;

    double result = Math.pow(r, 2);
    double area = PI * result;

    NumberFormat number = NumberFormat.getNumberInstance();
    number.setMaximumFractionDigits(2);

    String message = "Radius: " + r + "\n"
    + "Circumference: " + number.format(circumference) + "\n"
    + "Area: " + number.format(area);
    JOptionPane.showMessageDialog(null, message, "Circle Properties", JOptionPane.PLAIN_MESSAGE);
    System.exit(0);}
    }

    ------------------------------------------

    This does work:

    import javax.swing.*;
    import java.lang.Math;
    import java.text.NumberFormat;


    public class Circle{
    public static void main(String [] args) {

    String choice = "";

    String inputRadius = JOptionPane.showInputDialog("Enter Radius: ");
    double r = Double.parseDouble(inputRadius);

    double circumference = r * 2 * java.lang.Math.PI;

    double result = Math.pow(r, 2);
    double area = java.lang.Math.PI * result;

    NumberFormat number = NumberFormat.getNumberInstance();
    number.setMaximumFractionDigits(2);

    String message = "Radius: " + r + "\n"
    + "Circumference: " + number.format(circumference) + "\n"
    + "Area: " + number.format(area);
    JOptionPane.showMessageDialog(null, message, "Circle Properties", JOptionPane.PLAIN_MESSAGE);


    System.exit(0);}
    }
     
  10. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    this code works for me

    Code:
    import javax.swing.*;
    import java.text.NumberFormat;
    
    public class Circle{
    
    	public static void main(String [] args) {
    		String choice = "";
    
    		String inputRadius = JOptionPane.showInputDialog("Enter Radius: ");
    		double r = Double.parseDouble(inputRadius);
    
    		double circumference = r * 2 * Math.PI;
    
    		double result = Math.pow(r, 2);
    		double area = Math.PI * result;
    
    		NumberFormat number = NumberFormat.getNumberInstance();
    		number.setMaximumFractionDigits(2);
    
    		String message = "Radius: " + r + "\n" + "Circumference: " + number.format(circumference) + "\n" + "Area: " + number.format(area);
    		JOptionPane.showMessageDialog(null, message, "Circle Properties", JOptionPane.PLAIN_MESSAGE);
    		System.exit(0);
    	}
    }
    
     
  11. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    You can't just say PI because PI isn't a member of the current class. You have to at least say Math.PI so that Java knows what class PI comes from.
     
  12. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    if you want to use PI, you can always declair it as a public static final double. see example:

    Code:
    import javax.swing.*;
    import java.text.NumberFormat;
    
    public class Circle{
    	public static final double PI = Math.PI;
    	public static void main(String [] args) {
    		String choice = "";
    		String inputRadius = JOptionPane.showInputDialog("Enter Radius: ");
    		double r = Double.parseDouble(inputRadius);
    		double circumference = r * 2 * PI;
    		double result = Math.pow(r, 2);
    		double area = PI * result;
    		NumberFormat number = NumberFormat.getNumberInstance();
    		number.setMaximumFractionDigits(2);
    		String message = "Radius: " + r + "\n" + "Circumference: " + number.format(circumference) + "\n" + "Area: " + number.format(area);
    		JOptionPane.showMessageDialog(null, message, "Circle Properties", JOptionPane.PLAIN_MESSAGE);
    		System.exit(0);
    	}
    }
    
     
  13. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    you dont need to import anything for the math class, anything in java.lang is part of the languege and does not need to be imported.
     
  14. ooh... I understand now. Thanks for clearing it up :hs: sorry for the newb question... I'm trying to learn but my prof isn't helping much. He tried explaining it to me today but I couldn't figure out what the crap he was talking about.

    thanks :bigthumb:

    now I just gotta figure out how to use these constructors :ugh:
     

Share This Page