Simple web server - Chapter 6 Methods 301 7 8 for (
Chapter 6 Methods 301 7 8 for ( x = 1; x <= 10; x++ ) { 9 g.drawString( cube( x ), 25, yPos ); 10 yPos += 15; 11 } 12 } 13 14 public int cube( int y ) 15 { 16 return y * y * y; 17 } 18 } 6.3 Write an application that tests if the examples of the math-library method calls shown in Fig. 6.2 actually produce the indicated results. 6.4 Give the method header for each of the following methods: a) Method hypotenuse, which takes two double-precision, floating-point arguments side1 and side2 and returns a double-precision, floating-point result. b) Method smallest, which takes three integers x, y and zand returns an integer. c) Method instructions, which does not take any arguments and does not return a val ue. [Note: Such methods are commonly used to display instructions to a user.] d) Method intToFloat, which takes an integer argument number and returns a floating- point result. 6.5 Find the error in each of the following program segments. Explain how to correct the error. a) int g() { System.out.println( “Inside method g” ); int h() { System.out.println( “Inside method h” ); } } b) int sum(int x, int y ) { int result; result = x + y; } c) intsum( int n ) { if ( n == 0 ) return 0; else n + sum( n - 1 ); } d) void f( float a ); { float a; System.out.println( a ); } e) void product() { int a = 6, b = 5, c = 4, result; result = a * b * c; System.out.println( “Result is ” + result ); return result; } Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01