// Exercise 4.3: MathTest.
java
// Testing the Math class methods
public class MathTest {
public static void main( String args[] )
{
System.out.println( "Math.abs( 23.7 ) = " +
Math.abs( 23.7 ) );
System.out.println( "Math.abs( 0.0 ) = " +
Math.abs( 0.0 ) );
System.out.println( "Math.abs( -23.7 ) = " +
Math.abs( -23.7 ) );
System.out.println( "Math.ceil( 9.2 ) = " +
Math.ceil( 9.2 ) );
System.out.println( "Math.ceil( -9.8 ) = " +
Math.ceil( -9.8 ) );
System.out.println( "Math.cos( 0.0 ) = " +
Math.cos( 0.0 ) );
System.out.println( "Math.exp( 1.0 ) = " +
Math.exp( 1.0 ) );
System.out.println( "Math.exp( 2.0 ) = " +
Math.exp( 2.0 ) );
System.out.println( "Math.floor( 9.2 ) = " +
Math.floor( 9.2 ) );
System.out.println( "Math.floor( -9.8 ) = " +
Math.floor( -9.8 ) );
System.out.println( "Math.log( 2.718282 ) = " +
Math.log( 2.718282 ) );
System.out.println( "Math.log( 7.389056 ) = " +
Math.log( 7.389056 ) );
System.out.println( "Math.max( 2.3, 12.7 ) = " +
Math.max( 2.3, 12.7 ) );
System.out.println( "Math.max( -2.3, -12.7 ) = " +
Math.max( -2.3, -12.7 ) );
System.out.println( "Math.min( 2.3, 12.7 ) = " +
Math.min( 2.3, 12.7 ) );
System.out.println( "Math.min( -2.3, -12.7 ) = " +
Math.min( -2.3, -12.7 ) );
System.out.println( "Math.pow( 2, 7 ) = " +
Math.pow( 2, 7 ) );
System.out.println( "Math.pow( 9, .5 ) = " +
Math.pow( 9, .5 ) );
System.out.println( "Math.sin( 0.0 ) = " +
Math.sin( 0.0 ) );
System.out.println( "Math.sqrt( 25.0 ) = " +
Math.sqrt( 25.0 ) );
System.out.println( "Math.tan( 0.0 ) = " +
Math.tan( 0.0 ) );
}
}
// Exercise 6.6: SphereTest.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SphereTest extends JApplet
implements ActionListener {
JLabel prompt;
JTextField input;
public void init()
{
Container c = getContentPane();
c.setLayout( new FlowLayout() );
prompt = new JLabel( "Enter sphere radius: " );
input = new JTextField( 10 );
input.addActionListener( this );
c.add( prompt );
c.add( input );
}
public void actionPerformed( ActionEvent e )
{
double radius =
Double.parseDouble( e.getActionCommand() );
showStatus( "Volume is " + sphereVolume( radius ) );
}
public double sphereVolume( double radius )
{
double volume =
( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 );
return volume;
}
}