230 Methods Chapter 6 6.4 Write an application

230 Methods Chapter 6 6.4 Write an application that tests whether the examples of the math library method calls shown in Fig. 6.2 actually produce the indicated results. 6.5 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, z, and returns an integer. c) Method instructions, which does not take any arguments and does not return a val ue. [Note: Such methods commonly are used to display instructions to a user.] d) Method intToFloat, which takes an integer argument, number, and returns a floating- point result. 6.6 Find the error in each of the following program segments and explain how the error can be corrected: a) int g() { Console.WriteLine( “Inside method g” ); int h() { Console.WriteLine( “Inside method h” ); } } b) int sum(int x, int y ) { int result; result = x + y; } c) int sum(int n ) { if ( n == 0 ) return 0; else n + sum( n - 1 ); } d) void f( float a ); { float a; Console.WriteLine( a ); } e) void product() { int a = 6, b = 5, c = 4, result; result = a * b * c; Console.WriteLine( “Result is ” + result ); return result; } ANSWERS TO SELF-REVIEW EXERCISES 6.1 a) methods, classes. b) method call. c) local variable. d) return. e) void. f) scope. g) return;, return expression;, encountering the closing right brace of a method. h) Random.Next. i) automatic. j) recursive. k) base. l) overloading. m) block. n) repetition. o) selection. p) method. q) modules. r) name. s) base case. t) syntax. u) parameter list. v) return-value-type. w) event. 6.2 a) False. Math method Abs returns the absolute value of a number. b) True. c) True. d) False. Type char can be promoted to int, float, long and double. e) True. f) False. When a method recursively calls itself, it is known as the recursive call or recursion step. g) True. h) True. i) False. Infinite recursion will occur when a recursive method does not converge on the base case. j) False. Recursion repeatedly invokes the mechanism, and consequently, the overhead, of method calls. k) True.
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Leave a Reply