Description

Methods: A Deeper Look, Solutions 6 E pluribus unum. (One composed of many.) —Virgil O! call back yesterday, bid time return. —William Shakespeare Call me Ishmael. —Herman Melville Answer me in one word. —William Shakespeare There is a point at which methods devour themselves. —Frantz Fanon Objectives In this chapter you’ll learn: ■ How static methods and fields are associated with classea rather than objects. ■ How the method call/return mechanism is supported by the method-call stack. ■ How packages group related classes. ■ How to use random-number generation to implement game-playing applications. ■ How the visibility of declarations is limited to specific regions of programs. ■ What method overloading is and how to create overloaded methods. ANS: block that defines method rollDice’s body. a) the variable randomNumbers. an error known as a(n) occurs. e) the variable sumOfDice. c) The statement in a called method can be used to pass the value of an ex- pression back to the calling method. ANS: local variable. ANS: Random. ANS: top. or return expression. and . d) The keyword indicates that a method does not return a value. b) the variable die1. ANS: method call. j) If there are more method calls than can be stored on the program-execution stack. m) The program-execution stack is also referred to as the stack. ANS: method overloading. This data. ANS: class body. l) It’s possible to have several methods with the same name that each operate on different types or numbers of arguments. first-out (LIFO). ANS: last-in. 6. h) An object of class produces random numbers. 6. is known as the or of the method call. ANS: void. ANS: return. ANS: class body. ANS: scope. ANS: activation record.1 Fill in the blanks in each of the following statements: a) A method is invoked with a(n) . c) the method rollDice. stored as a portion of the pro- gram-execution stack. ANS: stack overflow. Self-Review Exercises 2 Self-Review Exercises 6. or encountering the closing right brace of a method.3 Write an application that tests whether the examples of the Math class method calls shown in Fig. b) A variable known only within the method in which it’s declared is called a(n) . ANS: method call. i) The program-execution stack contains the memory for local variables on each invoca- tion of a method during a program’s execution.2 actually produce the indicated results. ANS: return. e) Data can be added or removed only from the of a stack. d) the method play.9. 6. This feature is called method . stack frame. k) The of a declaration is the portion of a program that can refer to the entity in the declaration by name. 6.2 For the class Craps in Fig. . f) Stacks are known as data structures—the last item pushed (inserted) on the stack is the first item popped (removed) from the stack. ANS: block that defines method play’s body. g) The three ways to return control from a called method to a caller are . state the scope of each of the following entities: ANS: class body. 3.7 ) ).abs( 0.E ) = %f\n".389056 Math.printf( "Math.3.out. 31 System.pow( 2.out.sqrt( 900.7 ) = 23. 26 Math. 12.7 ) = %f\n".7 ) = %f\n".abs( -23.2: 1 // Exercise 6.7 ) ). 23 System.min( -2.700000 Math.000000 Math. 11 System. 13 System.out.cos( 0.3. 0.2 ) ).000000 Math.cos( 0.ceil( -9.printf( "Math.8 ) = -9.out.7 ) = %f\n". 12. 38 System. 12. 3 Chapter 6 Methods: A Deeper Look.out.printf( "Math.7 ) = -12.abs( 23.0 ) ).000000 .floor( -9.0 ) ).printf( "Math. Math.0 ) = %f\n".000000 Math. Math.out.printf( "Math. Math.printf( "Math.0 ) = %f\n". 21 System. -12. Math.0.0 ) ).8 ) = -10.0 ) = %f\n".ceil( 9.7 ) = %f\n".8 ) ).8 ) = %f\n".sqrt( 900.7 ) ).log( Math. -12.0 ) ).log( Math. 16 System.exp( 1.0 ) = %f\n".out. 9 System.0. 7.floor( 9.E ) ). -12.printf( "Math.out.000000 Math.pow( 9.ceil( -9.cos( 0.E * Math.2 ) ). 30 Math.E * Math.abs( -23.8 ) ).sin( 0.tan( 0. 19 System.exp( 1.0 ) = 30.000000 Math.000000 Math. 12.printf( "Math.3. Math. 22 Math. 10 System.8 ) = %f\n". 24 Math. 36 System.7 ) = 12.E ) = 1.min( -2.0. -12.max( 2. 3 4 public class MathTest 5 { 6 public static void main( String[] args ) 7 { 8 System. 15 System. Solutions ANS: The following solution demonstrates the Math class methods in Fig.5 ) ).3.E ) = 2.0 ) ).out.out.log( Math.3: MathTest.2 ) = %f\n".min( 2.000000 Math.7 ) = %f\n".out. 0.7 ) ).floor( -9.exp( 2.0 ) = 0.floor( 9. Math.718282 Math.ceil( 9.3.printf( "Math.700000 Math.0 ) = 128.printf( "Math.out.7 ) = 2.7 ) ).log( Math. 33 System.pow( 9.out. Math.tan( 0.abs( 0.out.floor( 9.max( 2.out.0 ) = 0.printf( "Math.sqrt( 900.3.max( -2.printf( "Math.300000 Math. 18 Math.min( 2.max( -2.exp( 2.0 ) = %f\n".log( Math.out. Math. 27 System.exp( 1. 25 System.E ) = %f\n".abs( -23. 14 System.out.abs( 23. 12.0 ) ).3. 39 } // end main 40 } // end class MathTest Math.pow( 9.0 ) = %f\n".java 2 // Testing the Math class methods.max( 2.tan( 0.log( Math.ceil( 9.printf( "Math. 34 Math. 20 Math.E ) ). 12 System. 6. -12.printf( "Math. 12. 37 Math. 0.3.300000 Math.abs( 0.0 ) = %f\n".2 ) = 9. Math.7 ) = %f\n".3.pow( 2.0. 7.printf( "Math.printf( "Math.printf( "Math. Math. 29 System.ceil( -9.min( 2.0 ) ).printf( "Math. Math.2 ) = %f\n".max( -2.5 ) = %f\n".0 ) ).0.2 ) = 10.sin( 0.5 ) = 3. 28 Math.0 ) = 1. 7.000000 Math.E * Math.floor( -9.0.out.000000 Math.7 ) ).3.000000 Math.printf( "Math.printf( "Math. 35 System.000000 Math. 32 Math.out.out.0 ) = %f\n".pow( 2.min( -2. -12.700000 Math.0 ) = 0.sin( 0.3.printf( "Math.700000 Math. 17 System.7 ) = -2.000000 Math.0 ) = 7.out.abs( 23.exp( 2.0 ) = 2.7 ) = 23. println( a ). return result.4 Give the method header for each of the following methods: a) Method hypotenuse. Correction: Delete the variable result. result. Self-Review Exercises 4 6. which takes an integer argument number and returns a floating- point result. and the parameter a should not be redeclared in the method. y and z and returns an integer. { float a. or add the following statement at the end of the method body: return result. void h() { System.out. d) void product() { int a = 6. which takes two double-precision. result ).out. ANS: int smallest( int x.printf( "Result is %d\n". and delete the declaration float a. result = x + y.println( "Inside method h" ). } } ANS: Error: Method h is declared within method g. and place the statement return x + y. c) void f( float a ). b = 5. in the method. double side2 ) b) Method smallest.println( "Inside method g" ). but does not. Explain how to correct the error. int y ) { int result. ANS: double hypotenuse( double side1. floating-point arguments side1 and side2 and returns a double-precision. Correction: Delete the semicolon after the right parenthesis of the parameter list. [Note: Such methods are commonly used to display instructions to a user. which does not take any arguments and does not return a value. int z ) c) Method instructions. ANS: float intToFloat( int number ) 6.out. } . c = 4. Correction: Move the declaration of h outside the declaration of g. } ANS: Error: The method is supposed to return an integer.5 Find the error in each of the following program segments. System. result = a * b * c. b) int sum( int x. } ANS: Error: The semicolon after the right parenthesis of the parameter list is incorrect.] ANS: void instructions() d) Method intToFloat. System.. a) void g() { System.out. int y. floating-point result. which takes three integers x. printf( "Volume is %f\n".6: SphereTest. 3 ).in ).0 / 3. 3 ) ANS: The following solution calculates the volume of a sphere. 16 } // end method determineSphereVolume 17 18 // calculate and return sphere volume 19 public double sphereVolume( double radius ) 20 { 21 double volume = ( 4.pow( radius. 10 mySphere.java 2 // Calculate the volume of a sphere. using the radius entered by the user: 1 // Exercise 6.java 2 // Calculate the volume of a sphere.nextDouble(). 11 12 System.determineSphereVolume(). 11 } // end main 12 } // end class SphereTest Enter radius of sphere: 4 Volume is 268. 3 4 public class SphereTest 5 { 6 // application starting point 7 public static void main( String[] args ) 8 { 9 Sphere mySphere = new Sphere(). 4 5 public class Sphere 6 { 7 // obtain radius from user and display volume of sphere 8 public void determineSphereVolume() 9 { 10 Scanner input = new Scanner( System.print( "Enter radius of sphere: " ). sphereVolume( radius ) ). Correction: Change the return type from void to int.0 ) * Math.Scanner.out. and call method sphereVolume to calculate and display the volume of the sphere.6: Sphere. 23 } // end method sphereVolume 24 } // end class Sphere 1 // Exercise 6.PI * Math. 22 return volume. 13 double radius = input.out.6 Write a complete Java application to prompt the user for the double radius of a sphere. Solutions ANS: Error: The method returns a value when it’s not supposed to. 14 15 System.pow( radius.PI * Math. 3 import java.0 / 3.082573 .0 ) * Math.util. 6. Use the following state- ment to calculate the volume: double volume = ( 4. 5 Chapter 6 Methods: A Deeper Look. nextInt( 2 ). such as cars arriving at toll booths and people arriving in lines at a supermarket.0 ).4 f) x = Math.0 ).nextInt( 3 ).abs( 0. f) –3 ≤ n ≤ 11 ANS: n = -3 + randomNumbers. b) 1 ≤ n ≤ 100 ANS: n = 1 + randomNumbers.nextInt( 15 ). c) Why is it often necessary to scale or shift the values produced by a Random object? ANS: To produce random numbers in a specific range.nextInt( 113 ). 6.ceil( -6. ANS: [Note: See the test program in the ch06solutions\ex06_12 folder.8’s solution is located in the folder ex06_08.floor( 7. write a single statement that will display a number at random from the set: . c) 0 ≤ n ≤ 9 ANS: n = randomNumbers.5 ).abs( -8 + Math. ANS: -14. ANS: 7. exercise 6.0 6. e) –1 ≤ n ≤ 1 ANS: n = -1 + randomNumbers.ceil( 0.12 Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 ANS: n = 1 + randomNumbers. Each exercise has its own folder named ex06_## where ## is a two-digit number represent- ing the exercise number.5 ) ) ).11 Answer each of the following questions: a) What does it mean to choose numbers “at random”? ANS: Every number has an equal chance of being chosen at any time.7 What is the value of x after each of the following statements is executed? a) x = Math. ANS: 7.floor( -5.4 ).13 For each of the following sets of integers. For example.5 ). d) 1000 ≤ n ≤ 1112 ANS: n = 1000 + randomNumbers. 6.0 c) x = Math. d) Why is computerized simulation of real-world situations a useful technique? ANS: It enables more accurate predictions of random events. ANS: 0. b) Why is the nextInt method of class Random useful for simulating games of chance? ANS: Because it produces a series of random numbers.0 d) x = Math.5 b) x = Math. Exercises 6 Exercises NOTE: Solutions to the programming exercises are located in the ch06solutions folder.nextInt( 10 ). The results of a simulation can help determine how many toll booths to have open or how many cashiers to have open at specified times.0 e) x = Math.nextInt( 100 ). ANS: 0. ANS: -6.4 ).abs( -6.abs( 7. ANS: 6.ceil( -Math.] 6.0 g) x = Math. b) 3.nextInt( 5 ) * 4 ). ANS: System. 9. 8. 22.7 Chapter 6 Methods: A Deeper Look. 10. 18.nextInt( 5 ) * 2 ). ANS: [Note: See the test program in the ch06solutions\ex06_12 folder.] . 10.out.println( 3 + randomNumbers. 6. c) 6. ANS: System. 5. Solutions a) 2.println( 2 + randomNumbers.println( 6 + randomNumbers. 14. 7. 11.out.out.nextInt( 5 ) * 2 ). ANS: System. 4.
Copyright © 2024 DOKUMEN.SITE Inc.