0% found this document useful (0 votes)
39 views4 pages

Chp06 CheckPointAnswers

Methods allow code reuse and reduce complexity. They are defined with a return type and can be invoked by passing actual parameters in the correct order and type. Overloading methods is allowed based on different parameter types but not return type or modifiers. Local variables exist only within the method block and have scope from declaration to end of block.

Uploaded by

Bibek Dhakal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views4 pages

Chp06 CheckPointAnswers

Methods allow code reuse and reduce complexity. They are defined with a return type and can be invoked by passing actual parameters in the correct order and type. Overloading methods is allowed based on different parameter types but not return type or modifiers. Local variables exist only within the method block and have scope from declaration to end of block.

Uploaded by

Bibek Dhakal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter 6 Methods

1. At least three benefits: (1) Reuse code; (2) Reduce complexity; (3) Easy to maintain.

2. See the Sections 5.2 and 5.3 on how to define and invoke methods.

3. return (num1 > num2) ? num1 : num2;

4. True: a call to a method with a void return type is always a statement itself.

False: a call to a value-returning method is always a component of an expression.

5. void

6. A syntax error occurs if a return statement is not used to return a value in a value-returning
method. You can have a return statement in a void method, which simply exits the method. But a
return statement cannot return a value such as return x + y in a void method.

7. See Section 5.2.

8. (a) Computing a sales commission given the sales amount and the commission rate

public static double getCommission(double salesAmount, double commissionRate)

(b) Displaying a calendar for a month

public static void printCalendar(int month, int year)

(c) Computing a square root

public static double sqrt(double value)

(d) Testing whether a number is even and return true if it is

public static boolean isEven(int value)

(e) Displaying a message for a specified number of times

public static void printMessage(String message, int times)


(f) Computing the monthly payment, given the loan amount, number of years, and annual
interest rate.

public static double monthlyPayment(double loan, int numberOfYears, double


annualInterestRate)

(g) Finding the corresponding uppercase letter given a lowercase letter.

public static char getUpperCase(char letter)

9. Line 2: method1 is not defined correctly. It does not have a return type or void.

Line 2: type int should be declared for parameter m.

Line 7: parameter type for n should be double to match method2(3.4).

Line 11: if (n<0) should be removed in method, otherwise a compile error is reported.

10.

public class Test {

public static double xMethod(double i, double j) {

while (i<j) {

j--;

return j;

11. You pass actual parameters by passing the right type of value in the right order. The actual
parameter can have the same name as its formal parameter.

12. Two errors: 1. the arguments are not passed in the right order. 2. n is a parameter, but is
redeclared in the nPrintln method in line 7.

13. "Pass by value" is to pass a copy of the value to the method.


(A) The output of the program is 0, because the variable max is not changed by invoking
the method max.

(B)

24

248

2 4 8 16

2 4 8 16 32

2 4 8 16 32 64

(C)

Before the call, variable times is 3

n=3

Welcome to Java!

n=2

Welcome to Java!

n=1

Welcome to Java!

After the call, variable times is 3

(D)

21

21

421

i is 5
14.

Space required for the Space required for the


max method max method
max: 0 max: 2
value2: 2 value2: 2
value1: 1 value1: 1
Space required for the Space required for the Space required for the Space required for the
main method main method main method main method

max: 0 max: 0 max: 0 max: 0

Just before max is Just entering max. Just before max is Right after max is
invoked. returned returned

15. Two methods with the same name, defined in the same class, is called method overloading. It is
fine to have same method name, but different parameter types. You cannot overload methods based on
return type, or modifiers.

16. Methods public static void method(int x) and public static int method(int y) have the same
signature method(int).

17. (a) invokes the second method. (b) invokes the second. (c) invokes the first method.

18. A local variable is a variable declared inside a method.

19. The scope of a local variable starts from its declaration and continues to the end of the block that
contains the variable.

You might also like