Using Math2 Class Methods


Using Math2 Class Methods

Let's write a program named UserMathDemo.java that makes use of some of the methods defined in the Math2 class. A typical use of the common logarithm function is to compute the pH of a liquid. The pH is defined as the negative of the natural logarithm of the concentration of hydrogen ion in the liquid.

Equation 17.10

graphics/17equ10.gif


The hydrogen ion concentration in Eq. (17.10) is in units of moles per liter. The UserMathDemo class defines the getPH() method that returns the pH value based on a specified hydrogen ion concentration. The getPH() method makes use of the log10() method defined in the Math2 class.

A catenary is the curve a uniform flexible cable will assume when supported at its ends and acted upon by a uniform gravitational force. The shape of the curve follows that of the hyperbolic cosine function shown in Eq. (17.11).

Equation 17.11

graphics/17equ11.gif


The x -direction is parallel to the ground and the y -direction is perpendicular to the ground. The point (0, a ), where a is a positive number, is the lowest point of the curve. The UserMathDemo class defines the getCatenaryY() method that returns the value of y for a catenary curve based on specified values of x and a . It makes use of the cosh() method from the Math2 class. The source code for the UserMathDemo class is shown here.

 import TechJava.MathLib.Math2; public class UserMathDemo {   // This method returns the pH of a liquid.   // It tests for zero or negative numbers.   public static double getPH(double concH) {     concH = Math.max(concH,1.0e-30);     return -Math2.log10(concH);   }   // This method returns the y-location of a flexible   // cable   public static double getCatenaryY(double x,double a) {     return a*Math2.cosh(x/a);   }   public static void main(String args[]) {     System.out.println("pH = "+getPH(3.4e-6));     System.out.println("catenary y = " +                         getCatenaryY(3.4,2.0));   } } 

Output ”

 pH = 5.46852108295 catenary y = 5.6566309157 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net