302 Methods Chapter 6 6.6 Write a complete (Business web hosting)
302 Methods Chapter 6 6.6 Write a complete Java applet to prompt the user for the double radius of a sphere, and call method sphereVolume to calculate and display the volume of that sphere using the assignment volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 ) The user should input the radius through a JTextField. ANSWERS TO SELF-REVIEW EXERCISES 6.1 a) methods and classes. b) method call. c) local variable. d) return. e) void. f) scope. g) return;or return expression;or encountering the closing right brace of a method. h) init. i) Math.random. j) start. k) paint. l) automatic. m) repaint. n) stop. o) recursive. p) base. q) overloading. r) final. 6.2 a) Class scope. b) Block scope. c) Class scope. d) Class scope. e) Block scope. 6.3 The following solution demonstrates the Math class methods in Fig. 6.2: 1 // Exercise 6.3: MathTest.java 2 // Testing the Math class methods 3 4 public class MathTest { 5 public static void main( String args[] ) 6 { 7 System.out.println( “Math.abs( 23.7 ) = ” + 8 Math.abs( 23.7 ) ); 9 System.out.println( “Math.abs( 0.0 ) = ” + 10 Math.abs( 0.0 ) ); 11 System.out.println( “Math.abs( -23.7 ) = ” + 12 Math.abs( -23.7 ) ); 13 System.out.println( “Math.ceil( 9.2 ) = ” + 14 Math.ceil( 9.2 ) ); 15 System.out.println( “Math.ceil( -9.8 ) = ” + 16 Math.ceil( -9.8 ) ); 17 System.out.println( “Math.cos( 0.0 ) = ” + 18 Math.cos( 0.0 ) ); 19 System.out.println( “Math.exp( 1.0 ) = ” + 20 Math.exp( 1.0 ) ); 21 System.out.println( “Math.exp( 2.0 ) = ” + 22 Math.exp( 2.0 ) ); 23 System.out.println( “Math.floor( 9.2 ) = ” + 24 Math.floor( 9.2 ) ); 25 System.out.println( “Math.floor( -9.8 ) = ” + 26 Math.floor( -9.8 ) ); 27 System.out.println( “Math.log( 2.718282 ) = ” + 28 Math.log( 2.718282 ) ); 29 System.out.println( “Math.log( 7.389056 ) = ” + 30 Math.log( 7.389056 ) ); 31 System.out.println( “Math.max( 2.3, 12.7 ) = v + 32 Math.max( 2.3, 12.7 ) ); 33 System.out.println( “Math.max( -2.3, -12.7 ) = ” + 34 Math.max( -2.3, -12.7 ) ); 35 System.out.println( “Math.min( 2.3, 12.7 ) = ” + 36 Math.min( 2.3, 12.7 ) ); 37 System.out.println( “Math.min( -2.3, -12.7 ) = ” + 38 Math.min( -2.3, -12.7 ) ); Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01