Java Reference
In-Depth Information
where r is the radius vector and θ is the vectorial angle. The following pro-
gram demonstrates conversion from Cartesian to polar coordinates and
vice versa.
// Java for Engineers
// Filename: CartPol
// Reference: Chapter 23
// Description:
//
Conversion between polar and rectangular
//
coordinates
// Requires:
//
Keyin class in current directory
import java.lang.*;
strictfp class CartPol
{
public static void main(String[] args)
{
double x, y, vA, degs, radVec;
// Obtain rectangular coordinates from user
x = Keyin.inDouble(“Enter x: ”);
y = Keyin.inDouble(“Enter y: ”);
// Convert rectangular to polar coordinates
vA = Math.atan2(x, y);
degs = Math.toDegrees(vA);
System.out.println(“Vectorial angle=”+degs);
// Calculate radius vector
radVec = Math.sqrt((x * x)+(y * y));
System.out.println(“Radius vector=”+radVec);
// Convert polar back to rectangular coordinates
x = radVec * Math.cos(vA);
System.out.println(“x coodinate=”+x);
y = radVec * Math.sin(vA);
System.out.println(“y coordinate=”+y);
}
}
On the Web
The program CartPol.java can be found in the Chapter 23 folder at
www.crcpress.com .
Logarithmic Functions
The classes java.lang.Math and java.lang.StrictMath contain a single
method that relates to logarithms: log(). This method returns the natural
logarithm (base e) of the double argument. However,the logarithmic for -
Search WWH ::




Custom Search