Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

D. Methods: A Deeper Look > Answers to Self-Review Exercises

Answers to Self-Review Exercises

D.1

a. method call.

b. local variable.

c. return.

d. void.

e. top.

f. last-in, first-out (LIFO).

g. return; or return expression; or encountering the closing right brace of a method.

h. Random.

i. activation record, stack frame.

j. stack overflow.

k. scope.

l. method overloading.

m. method call.

D.2

a. class body.

b. block that defines method rollDice’s body.

c. class body.

d. class body.

e. block that defines method main’s body.

D.3 The following solution demonstrates the Math class methods in Fig. D.1:


 1   // Exercise D.3: MathTest.java
 2   // Testing the Math class methods.
 3
 4   public class MathTest
 5   {
 6      public static void main( String[] args )
 7      {
 8         System.out.printf( "Math.abs( 23.7 ) = %f\n", Math.abs( 23.7 ) );
 9         System.out.printf( "Math.abs( 0.0 ) = %f\n", Math.abs( 0.0 ) );
10         System.out.printf( "Math.abs( -23.7 ) = %f\n", Math.abs( -23.7 ) );
11         System.out.printf( "Math.ceil( 9.2 ) = %f\n", Math.ceil( 9.2 ) );
12         System.out.printf( "Math.ceil( -9.8 ) = %f\n", Math.ceil( -9.8 ) );
13         System.out.printf( "Math.cos( 0.0 ) = %f\n", Math.cos( 0.0 ) );
14         System.out.printf( "Math.exp( 1.0 ) = %f\n", Math.exp( 1.0 ) );
15         System.out.printf( "Math.exp( 2.0 ) = %f\n", Math.exp( 2.0 ) );
16         System.out.printf( "Math.floor( 9.2 ) = %f\n", Math.floor( 9.2 ) );
17         System.out.printf( "Math.floor( -9.8 ) = %f\n",
18            Math.floor( -9.8 ) );
19         System.out.printf( "Math.log( Math.E ) = %f\n",
20            Math.log( Math.E ) );
21         System.out.printf( "Math.log( Math.E * Math.E ) = %f\n",
22            Math.log( Math.E * Math.E ) );
23         System.out.printf( "Math.max( 2.3, 12.7 ) = %f\n",
24            Math.max( 2.3, 12.7 ) );
25         System.out.printf( "Math.max( -2.3, -12.7 ) = %f\n",
26            Math.max( -2.3, -12.7 ) );
27         System.out.printf( "Math.min( 2.3, 12.7 ) = %f\n",
28            Math.min( 2.3, 12.7 ) );
29         System.out.printf( "Math.min( -2.3, -12.7 ) = %f\n",
30            Math.min( -2.3, -12.7 ) );
31         System.out.printf( "Math.pow( 2.0, 7.0 ) = %f\n",
32            Math.pow( 2.0, 7.0 ) );
33         System.out.printf( "Math.pow( 9.0, 0.5 ) = %f\n",
34            Math.pow( 9.0, 0.5 ) );
35         System.out.printf( "Math.sin( 0.0 ) = %f\n", Math.sin( 0.0 ) );
36         System.out.printf( "Math.sqrt( 900.0 ) = %f\n",
37            Math.sqrt( 900.0 ) );
38         System.out.printf( "Math.tan( 0.0 ) = %f\n", Math.tan( 0.0 ) );
39      } // end main
40   } // end class MathTest


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint