Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use math.pi in java

Tags:

java

pi

I am having problems converting this formula V = 4/3 π r^3. I used Math.PI and Math.pow, but I get this error:

';' expected

Also, the diameter variable doesn't work. Is there an error there?

import java.util.Scanner;  import javax.swing.JOptionPane;  public class NumericTypes     {     public static void main (String [] args)     {         double radius;         double volume;         double diameter;          diameter = JOptionPane.showInputDialog("enter the diameter of a sphere.");          radius = diameter / 2;          volume = (4 / 3) Math.PI * Math.pow(radius, 3);          JOptionPane.showMessageDialog("The radius for the sphere is "+ radius + "and the volume of the sphere is ");     } } 
like image 366
IvanNewYork Avatar asked Sep 26 '12 03:09

IvanNewYork


People also ask

Is there a Math pi in Java?

Math. PI is a static final double constant in Java, equivalent to in π Mathematics. Provided by java. lang.

How do you type the pi symbol in Java?

The 'π' character corresponds to the code point of 03C0 - a valid point of the UNICODE-16, the encoding used by the Java source. Since the number of the code point fits in 16 bits, you can simply copy-paste the symbol into your Java source.


2 Answers

You're missing the multiplication operator. Also, you want to do 4/3 in floating point, not integer math.

volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);            ^^      ^ 
like image 178
David Yaw Avatar answered Sep 25 '22 11:09

David Yaw


Here is usage of Math.PI to find circumference of circle and Area First we take Radius as a string in Message Box and convert it into integer

public class circle {      public static void main(String[] args) {         // TODO code application logic here          String rad;          float radius,area,circum;         rad = JOptionPane.showInputDialog("Enter the Radius of circle:");          radius = Integer.parseInt(rad);         area = (float) (Math.PI*radius*radius);         circum = (float) (2*Math.PI*radius);          JOptionPane.showMessageDialog(null, "Area: " + area,"AREA",JOptionPane.INFORMATION_MESSAGE);         JOptionPane.showMessageDialog(null, "circumference: " + circum, "Circumfernce",JOptionPane.INFORMATION_MESSAGE);     }  } 
like image 35
tabish ali Avatar answered Sep 25 '22 11:09

tabish ali