Java Reference
In-Depth Information
{
double
num1;
//Line 1
double
num2;
//Line 2
System.out.println("Line 3: The larger of "
+ "5.6 and 10.8 is "
+ larger(5.6, 10.8));
//Line 3
System.out.print("Line 4: Enter two "
+ "numbers: ");
//Line 4
num1 = console.nextDouble();
//Line 5
num2 = console.nextDouble();
//Line 6
System.out.println();
//Line 7
System.out.println("Line 8: The larger of "
+ num1 + " and " + num2 + " is "
+ larger(num1, num2));
//Line 8
}
public static double
larger(
double
x,
double
y)
{
double
max;
7
if
(x >= y)
max = x;
else
max = y;
return
max;
}
}
Sample Run: (In this sample run, the user input is shaded.)
Line 3: The larger of 5.6 and 10.8 is 10.8
Line 4: Enter two numbers: 34 43
Line 8: The larger of 34.0 and 43.0 is 43.0
You can put methods within a class in any order.
A value-returning method must return a value. Consider the following method,
secret
,
which takes as a parameter an
int
value. If the value of the parameter,
x
, is greater
than
5
, it should return twice the value of
x
; otherwise, it should return the value of
x
.
public static int
secret(
int
x)
{
if
(x > 5)
//Line 1
return
2 * x;
//Line 2
}
Search WWH ::
Custom Search