Chapter 5 Methods

1.    At least three benefits: (1) Reuse code; (2) Reduce complexity; (3) Easy to maintain. See the Sections 5.2 and 5.3 on how to declare and invoke methods. What is the subtle difference between “defining a method” and “declaring a variable”? A declaration usually involves allocating memory to store a variable, but a definition doesn’t.

 

2. void

 

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

 

4. True,方法返回void,对方法的调用必须是一条语句。

            False,应该是若方法返回一个值,对方法的调用通常当作一个值处理。

 

5. 会发生语法错误.在 a void method中可以有return语句, which simply exits the method. But a return statement cannot return a value such as return x + y in a void method.

 

6. 形参:定义在方法头中的变量就是形参。

            实参:调用方法时,给参数传递一个值,这个值就是实参。

         

 

7. Computing a sales commission given the sales amount and the commission rate

public static double getCommission(double salesAmount, double commissionRate)

 

Printing a calendar for a month

public static void printCalendar(int month, int year)

 

Computing a square root

public static double sqrt(double value)

 

Testing whether a number is even and return true if it is

public static boolean isEven(int value)

 

Printing a message for a specified number of times

public static void printMessage(String message, int times)

 

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)

Finding the corresponding uppercase letter given a lowercase letter.

public static char getUpperCase(char letter)

 

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

            Line 2: m类型多余

            Line 7: 应该是double类型

Line 11: 删掉if (n<0) 

 

9.

 

public class Test {

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

    while (i<j) {

      j--;

    } 

 

    return j;

  }

}

 

10. You pass actual parameters by passing the right type of value in the right order. 可以同名

 

11. 值传递:当调用带参数的方法时,实参的值传递给形参这个过程称为通过值传递

 

(A) 结果是0,因为变量max在方法中没有被改变。

                               改一下:

public static void main(String[] args) {

int max = 0;

max(1, 2, max);

}

 

public static void max(int value1, int value2, int max){

 

if (value1 > value2)

max = value1;

else

max = value2;

 

System.out.println(max);

}

 

}

 

(B)

2 2 4 2 4 8 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)

2 1 

2 1 

4 2 1 

i is 5

12.

 

 

13. a在一个类中有两个方法,它们具有相同的名字,但有不同的参数列表。

           b不可以。

 

14.     两个方法具有相同的方法签名。 

 

15.    Line 7: int n = 1 错了,因为n已经在方法签名中被申明。

 

16. True 三角函数方法中的参数是以弧度为参数的角。

 

17.     (a) 34 + (int)(Math.random() * (55 – 34))

(b) (int)(Math.random() * 1000)

(c) 5.5 + (Math.random() * (55.5 – 5.5))

(d) (char)(‘a’ + (Math.random() * (‘z’ – ‘a’ + 1))

 

18.

 

Math.sqrt(4) = 2.0

Math.sin(2*Math.PI) = 0

Math.cos(2*Math.PI) = 1

Math.pow(2, 2) = 4.0

Math.log(Math.E) = 1

Math.exp(1) = 2.718

Math.max(2, Math.min(3, 4)) = 3

Math.rint(-2.5) = -2.0

Math.ceil(-2.5) = -2.0

Math.floor(-2.5) = -3.0

Math.round(-2.5f) = -2

Math.round(-2.5) = -2

Math.rint(2.5) = 2.0

Math.ceil(2.5) = 3.0

Math.floor(2.5) = 2.0

Math.round(2.5f) = 3

Math.round(-2.5) = -2

Math.round(Math.abs(-2.5)) = 3

 

 

posted on 2013-02-03 20:19  bailun  阅读(1210)  评论(0编辑  收藏  举报