Java Reference
In-Depth Information
public static int
mystery(
int
x,
int
y,
int
z)
{
if
(x <= y && x <= z)
return
(y + z - x);
else if
(y <= z && y <= x)
return
(z + x - y);
else
return
(x + y - z);
}
}
20.
Write the definition of a method that takes as input three numbers and
returns the sum of the first two numbers multiplied by the third number.
(Assume that the three numbers are of type
double
.)
Show the output of the following program:
public class
MysteryClass
{
21.
public static void
main(String[] args)
{
int
n;
for
(n = 1; n <= 5; n++)
System.out.println(mystery(n));
}
public static int
mystery(
int
k)
{
int
x, y;
y = k;
for
(x = 1; x <= (k - 1); x++)
y = y * (k - x);
return
y;
}
}
22.
Explain how a reference variable as a formal parameter works.
23.
In the following program fragment, identify the following items: method
heading, method body, method definition, formal parameters, actual para-
meters, method call, and local variables.
public class
Exercise23
//Line 1
{
//Line 2
public static void
main(String[] args)
//Line 3
{
//Line 4
int
x;
//Line 5
double
y;
//Line 6
char
z;
//Line 7
//...
//Line 8
hello(x, y, z);
//Line 9
Search WWH ::
Custom Search