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.
/*<br /> * File: GoldenRatio.java<br /> * -----------------------<br /> * This program dispalys the golden ratio.<br /> * fi = (1 + sqrt(5)) / 2;<br /> */<br />import acm.program.*;<br />import java.lang.Math;<br />public class GoldenRatio extends ConsoleProgram {<br />public void run() {<br />double fi = (1 + Math.sqrt(5)) / 2;<br />println("golden ratio: fi = " + fi);<br />}<br />}<br />