Chapter 05 Methods
Review questions
-------------------------------------------------------------------------------
1. Explain in your own words the difference between a method and a program.
Answer:
lies in who or what makes use of it
-------------------------------------------------------------------------------
2. Define the following terms as they apply to methods: call, argument, return.
Answer:
call: to apply a mehtod
aruments: pass the paramter to a method
return: return a result from a method
-------------------------------------------------------------------------------
3. What is the difference between passing information to a method by using
arguments and reading input data using methods like readInt? When would each
action be appropriate?
Answer:
Input and output refer to communication between a program and its user.
Arguments and results represetn communication between a method and its caller.
-------------------------------------------------------------------------------
4. How do you specify the result of a method in Java?
Answer:
return expression;
-------------------------------------------------------------------------------
5. Can there be more than one return statement in the body of a method?
Answer:
yes. for example:
private double min(double x, double y)
{
if (x < y)
return x;
else
return y;
}
-------------------------------------------------------------------------------
6. How do you indicate that you want to apply a method to another object?
Answer:
reciever.name(arguments);
-------------------------------------------------------------------------------
7. Why has it been unnecessary to specify receivers in the programs presented in
the earlier chapters?
Answer:
for those methods have defined in their superclass
-------------------------------------------------------------------------------
8. Why was it unnecessary to include a break statement at the end of each case
clause in the monthName method presented in the chapter?
Answer:
return statements automatically exit from the entire method
-------------------------------------------------------------------------------
9. What is a predicate method?
Answer:
return values of type boolean
-------------------------------------------------------------------------------
10. How do you tell whether two strings contain the same characters?
Answer:
private boolean haveSameCharacter(String s1, String s2)
{
for (int i = 0; i < s1.length(); i++)
for (int j=0; j<s2.length(); j++)
if (s1[i] == s2[j])
return true;
return false;
}
-------------------------------------------------------------------------------
11. What is the relationship between arguments and formal parameters?
Answer:
copy
-------------------------------------------------------------------------------
12. Variables declared within a method are said to be local variables. What is
the significance of the word local in this context?
Answer:
the variable scope
-------------------------------------------------------------------------------
13. What does the term return address mean?
Answer:
the point at which execution should continue
-------------------------------------------------------------------------------
14. What is a brute-force algorithm?
Answer:
int gcd(int x, int y)
{
int guess = Math.min(x, y);
while (x % guess != 0 || y % guess != 0)
guess--;
return guess;
}
-------------------------------------------------------------------------------
15. Use Euclid’s algorithm to compute the greatest common divisor of 7735 and
4185. What values does the local variable r take on during the calculation?
Answer:
3350
835
10
5
0
-------------------------------------------------------------------------------
16. In the examples of Euclid’s algorithm to calculate gcd(x, y) that appear in
this chapter, x is always larger than y. Does this condition matter? What
happens if x is smaller than y?
Answer:
doesn't matter
if x < y, then r = x % y = x and x != 0, so x = y and y = r
then y > x;
-------------------------------------------------------------------------------
Programming exercises
1. Write a program that displays the value of the mathematical constant
This constant φ is called the golden ratio. Classical mathematicians believed that this
number represented the most aesthetically pleasing ratio for the dimensions of a
rectangle, but it also turns up in computational mathematics.
6. Write a method nDigits(n) that returns the number of digits in the integer n, which
you may assume is positive. Design a main program to test your method. For hints
about how to write this program, you might want to look back at the DigitSum
program that was given in Figure 4-6.
7. Write a predicate method isPerfectSquare(n) that returns true if the integer n is a
perfect square. Remember that the method Math.sqrt returns a double, which is
therefore only an approximation of the actual square root.
8. Write a predicate method askYesNoQuestion(str) that prints out the string str as
a question for the user and then waits for a response. If the user enters the string
"yes", the askYesNoQuestion method should return true; if the user enters "no",
the method should return false. If the user enters anything else, askYesNoQuestion
should remind the user that it is seeking a yes-or-no answer and then repeat the
question. For example, if the program includes the statement
if (askYesNoQuestion("Would you like instructions")) . . .
the interaction with the user might look like this:
10. An integer greater than 1 is said to be prime if it has no divisors other than itself and
one. The number 17, for example, is prime, because it is divisible only by 1 and 17.
The number 91, however, is not prime because it is divisible by 7 and 13. Write a
predicate method isPrime(n) that returns true if the integer n is prime, and false
otherwise. As an initial strategy, implement isPrime using a brute-force algorithm
that simply tests every possible divisor. Once you have that version working, try to
come up with improvements to your algorithm that increase its efficiency without
sacrificing its correctness.
11. Greek mathematicians took a special interest in numbers that are equal to the sum of
their proper divisors (a proper divisor of n is any divisor less than n itself). They
called such numbers perfect numbers. For example, 6 is a perfect number because
it is the sum of 1, 2, and 3, which are the integers less than 6 that divide evenly into
6. Similarly, 28 is a perfect number because it is the sum of 1, 2, 4, 7, and 14.
Write a predicate method isPerfect(n) that returns true if the integer n is perfect,
and false otherwise. Test your implementation by writing a main program that uses
the isPerfect method to check for perfect numbers in the range 1 to 9999 by testing
each number in turn. Whenever it identifies a perfect number, your program should
display that number on the screen. The first two lines of output should be 6 and 28.
Your program should find two other perfect numbers in that range as well.
最后一题没有做出来。。。待续