Java Reference
In-Depth Information
5.1
What are the benefits of using a method?
Check
5.2
How do you define a method? How do you invoke a method?
Point
5.3
How do you simplify the max method in Listing 5.1 using the conditional operator?
5.4
True or false? A call to a method with a void return type is always a statement itself,
but a call to a value-returning method cannot be a statement by itself.
5.5
What is the return type of a main method?
5.6
What would be wrong with not writing a return statement in a value-returning
method? Can you have a return statement in a void method? Does the return
statement in the following method cause syntax errors?
public static void xMethod( double x, double y) {
System.out.println(x + y);
return x + y;
}
5.7
Define the terms parameter, argument, and method signature.
5.8
Write method headers (not the bodies) for the following methods:
a. Compute a sales commission, given the sales amount and the commission rate.
b. Display the calendar for a month, given the month and year.
c. Compute a square root of a number.
d. Test whether a number is even, and returning true if it is.
e. Display a message a specified number of times.
f. Compute the monthly payment, given the loan amount, number of years, and
annual interest rate.
g. Find the corresponding uppercase letter, given a lowercase letter.
5.9
Identify and correct the errors in the following program:
1 public class Test {
2 public static method1( int n, m) {
3 n += m;
4 method2( 3.4 );
5 }
6
7
public static int method2( int n) {
8
if (n > 0 ) return 1 ;
9
else if (n == 0 ) return 0 ;
10
else if (n < 0 ) return -1 ;
11 }
12 }
5.10
Reformat the following program according to the programming style and documenta-
tion guidelines proposed in Section 1.10, Programming Style and Documentation.
Use the next-line brace style.
public class Test {
public static double method1( double i, double j)
{
while (i < j) {
j--;
}
return j;
}
}
 
Search WWH ::




Custom Search