6Methods: A Deeper Look
Objectives
In this chapter you’ll learn:
How static methods and
fields are associated with
Self-Review Exercises 2
Self-Review Exercises
6.1 Fill in the blanks in each of the following statements:
a) A method is invoked with a(n) .
b) A variable known only within the method in which it’s declared is called a(n) .
c) The statement in a called method can be used to pass the value of an expres-
sion back to the calling method.
d) The keyword indicates that a method does not return a value.
e) Data can be added or removed only from the of a stack.
f) Stacks are known as data structures; the last item pushed (inserted) onto the
stack is the first item popped (removed) from the stack.
g) The three ways to return control from a called method to a caller are ,
and .
h) An object of class produces truly random numbers.
i) The method-call stack contains the memory for local variables on each invocation of a
method during a program’s execution. This data, stored as a portion of the method-call
stack, is known as the or of the method call.
j) If there are more method calls than can be stored on the method-call stack, an error
known as a(n) occurs.
k) The of a declaration is the portion of a program that can refer to the entity
in the declaration by name.
l) It’s possible to have several methods with the same name, each operating on different
types or numbers of arguments. This feature is called method .
6.2 For the class Craps in Fig. 6.8, state the scope of each of the following entities:
a) the variable randomNumbers.
b) the variable die1.
c) the method rollDice.
d) the method main.
6.3 Write an application that tests whether the examples of the Math class 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.
1// Exercise 6.3: MathTest.java
2// Testing the Math class methods.
3public class MathTest {
4 public static void main(String[] args) {
5 System.out.printf(“Math.abs(23.7) = %f%n”, Math.abs(23.7));
6 System.out.printf(“Math.abs(0.0) = %f%n”, Math.abs(0.0));
7 System.out.printf(“Math.abs(-23.7) = %f%n”, Math.abs(-23.7));
8 System.out.printf(“Math.ceil(9.2) = %f%n”, Math.ceil(9.2));
9 System.out.printf(“Math.ceil(-9.8) = %f%n”, Math.ceil(-9.8));
10 System.out.printf(“Math.cos(0.0) = %f%n”, Math.cos(0.0));
11 System.out.printf(“Math.exp(1.0) = %f%n”, Math.exp(1.0));
12 System.out.printf(“Math.exp(2.0) = %f%n”, Math.exp(2.0));
13 System.out.printf(“Math.floor(9.2) = %f%n”, Math.floor(9.2));
14 System.out.printf(“Math.floor(-9.8) = %f%n”, Math.floor(-9.8));
15 System.out.printf(“Math.log(Math.E) = %f%n”, Math.log(Math.E));
16 System.out.printf(“Math.log(Math.E * Math.E) = %f%n”,
17 Math.log(Math.E * Math.E));
18 System.out.printf(“Math.max(2.3, 12.7) = %f%n”, Math.max(2.3, 12.7));
19 System.out.printf(“Math.max(-2.3, -12.7) = %f%n”, Math.max(-2.3, -12.7));
20 System.out.printf(“Math.min(2.3, 12.7) = %f%n”, Math.min(2.3, 12.7));
21 System.out.printf(“Math.min(-2.3, -12.7) = %f%n”, Math.min(-2.3, -12.7));
22 System.out.printf(“Math.pow(2.0, 7.0) = %f%n”, Math.pow(2.0, 7.0));
23 System.out.printf(“Math.pow(9.0, 0.5) = %f%n”, Math.pow(9.0, 0.5));
24 System.out.printf(“Math.sin(0.0) = %f%n”, Math.sin(0.0));
25 System.out.printf(“Math.sqrt(900.0) = %f%n”, Math.sqrt(900.0));
26 System.out.printf(“Math.tan(0.0) = %f%n”, Math.tan(0.0));
27 }
28 }
Math.abs(23.7) = 23.700000
Math.abs(0.0) = 0.000000
Math.abs(-23.7) = 23.700000
Math.ceil(9.2) = 10.000000
Math.ceil(-9.8) = -9.000000
Math.cos(0.0) = 1.000000
Math.exp(1.0) = 2.718282
Math.exp(2.0) = 7.389056
Math.floor(9.2) = 9.000000
Math.floor(-9.8) = -10.000000
Math.log(Math.E) = 1.000000
Math.log(Math.E * Math.E) = 2.000000
Math.max(2.3, 12.7) = 12.700000
Math.max(-2.3, -12.7) = -2.300000
Math.min(2.3, 12.7) = 2.300000
Math.min(-2.3, -12.7) = -12.700000
Math.pow(2.0, 7.0) = 128.000000
Math.pow(9.0, 0.5) = 3.000000
Math.sin(0.0) = 0.000000
Math.sqrt(900.0) = 30.000000
Math.tan(0.0) = 0.000000
Self-Review Exercises 4
c) Method instructions, which does not take any arguments and does not return a value.
[Note: Such methods are commonly used to display instructions to a user.]
d) Method intToFloat, which takes integer argument number and returns a float.
6.5 Find the error in each of the following program segments. Explain how to correct the error.
a)
b)
c)
1void g() {
2 System.out.println(“Inside method g”);
3
4 void h() {
5 System.out.println(“Inside method h”);
6 }
7}
1int sum(int x, int y) {
2 int result;
3 result = x + y;
4}
1void f(float a); {
2 float a;
3 System.out.println(a);
4}
© 2018 Pearson Education, Inc., 330 Hudson Street, NY NY 10013. All rights reserved.
5Chapter 6 Methods: A Deeper Look
d)
6.6 Declare method sphereVolume to calculate and return the volume of the sphere. Use the
following statement to calculate the volume:
double volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3)
Write a Java application that prompts the user for the double radius of a sphere, calls sphereVolume
to calculate the volume and displays the result.
1void product() {
2 int a = 6;
3 int b = 5;
4 int c = 4;
5 int result = a * b * c;
6 System.out.printf(“Result is %d%n”, result);
7 return result;
8}
Exercises 6
6.7 What is the value of x after each of the following statements is executed?
a) double x = Math.abs(7.5);
b) double x = Math.floor(7.5);
c) double x = Math.abs(0.0);
d) double x = Math.ceil(0.0);
e) double x = Math.abs(-6.4);
f) double x = Math.ceil(-6.4);
g) double x = Math.ceil(-Math.abs(-8 + Math.floor(-5.5)));
6.11 Answer each of the following questions:
a) What does it mean to choose numbers “at random”?
b) Why is the nextInt method of class SecureRandom useful for simulating games of
chance?
c) Why is it often necessary to scale or shift the values produced by a SecureRandom object?
d) Why is computerized simulation of real-world situations a useful technique?
6.12 Write statements that assign random integers to the variable n in the following ranges:
a) 1 n 2.
b) 1 n 100.
c) 0 n 9.
d) 1000 n 1112.
e) –1 n 1.
f) –3 n 11.
6.13 Write statements that will display a random number from each of the following sets:
a) 2, 4, 6, 8, 10.
b) 3, 5, 7, 9, 11.
c) 6, 10, 14, 18, 22.