Exercise 5.3
- Study this method and then answer the questions that follow it.
public static int mystery (double a, double b)
{
int value = 0;
if (a < b)
value = -1;
if (a > b)
value = 1;
return value;
}
- What is the identifier of the method?
- What are its parameters?
- What type of value is returned by the method?
- What part of the definition forms the heading?
- Rewrite the method using a nested if structure.
- Rewrite the method using multiple return statements.
- The following method definitions lack both punctuation and indentation. Rewrite each definition correcting these defects and state, in a few words, the purpose of each method.
- public static char first(char a char b){if(a<b)
return a else return b}
- public static double second(double a double b)
{double answer if(a<b)answer=a-b else answer=b-a
return answer}
- Assuming that the method f has been defined as it was in Example
1, state, with reasons, which of the following fragments are invalid.
- System.out.println(f(-7));
- double x = f(-7);
- double x = System.out.println(f(-7));
- double x = -7; f(x);
- Write a method largest that returns the value of the largest of its
three double parameters.
- Write a method gcd that returns the value of the greatest common divisor of its two int parameters
| |