Exercise 5.5
- Some methods return values while others do not.
- What is the difference between the forms of the definition of
each of these kinds of methods?
- What is the difference between the calls to each of these kinds
of methods?
Explain the difference between an argument and a parameter.
What is the scope of a parameter of a method?
In the method outline shown here, state which variables can be used
at each numbered line.
int sample (int a, int b)
{ // 1
int c;
... // 2
for (int d = 0; .. )
{
... // 3
int e;
... // 4
}
... // 5
{
int f;
... // 6
}
int g;
... // 7
}
|
|
Suppose that two method definitions have these headers:
A: void foo (int m, float n)
B: void foo (float i, int j)
For each of the following calls, state, with reasons, whether or not the call is valid and, if it is valid, which version of foo would be called.
- foo(8,4);
- foo(2L,3L);
- foo('A',5);
- foo(4,0.5);
- foo(7,6L);
- foo('x','y');
Write a boolean-valued method isSquare with a single int parameter,
n. The method should return the value true if and only if n is
the square of some integer.
Complete the definition of the method digit with header:
int digit (int n, int position)
so that the method returns the value of the digit that is position
places from the right in the decimal representation of n. As examples,
digit (763,0) should return 3
digit (8574,2) should return 5
digit (78,4) should return 0
Write a character-valued method convertToGrade that has an int
parameter mark. The method should return the grade that corresponds
to mark according to the following table.
Write a method printTriangle that has a char parameter c and an
int parameter n. The method should print a triangular pattern with
the perimeter consisting of the character c and the interior (if there
is one) consisting of blanks. As an example, the call
printTriangle('*',5);
should produce output of the form
You may assume that the value of n is positive.
| |