JHTP自测题_第六章_深入理解方法



6.1 Fill in theblanks in each of the following statements:

a) A method is invokedwith a(n) method call.

b) A variable known onlywithin the method in which it’s declared is called a(n) local variable .

c) The returnexpression; statement in a called method can be used to pass the value ofan expression back to the calling method.

d) The void keywordindicates that a method does not return a value.

e) Data can be added orremoved only from the stack frame of a stack.

f) Stacks are known as LIFOdata structures; the last item pushed (inserted) onto the stack is the firstitem popped (removed) from the stack.

g) The three ways toreturn control from a called method to a caller are return; or return expression;or encountering the closing right brace of a method.

h) An object of class SecureRandomproduces truly random numbers.

i) The method-call stackcontains the memory for local variables on each invocation of a method during aprogram’s execution. This data, stored as a portion of the method-call stack,is known as the stack frame or activation record of the methodcall.

j) If there are moremethod calls than can be stored on the method-call stack, an error known asa(n) stack overflow occurs.

k) The scope of a declarationis the portion of a program that can refer to the entity in the declaration byname.

l) It’s possible to haveseveral methods with the same name that each operate on different types ornumbers of arguments. This feature is called method overloading .

6.2 For the classCraps in Fig. 6.8, state the scope of each of the following entities:

a) the variable randomNumbers.

b) the variable die1.

c) the method rollDice.

d) the method main.

e) the variable sumOfDice.

6.3 Write anapplication that tests whether the examples of the Math class methodcalls shown

in Fig. 6.2 actuallyproduce the indicated results.

6.4 Give themethod header for each of the following methods:

a) Method hypotenuse, which takestwo double-precision, floating-point arguments

side1 and side2 and returns adouble-precision, floating-point result.

b) Method smallest, which takesthree integers x, y and z and returns an integer.

c) Method instructions, which doesnot take any arguments and does not return a value.

[Note: Such methodsare commonly used to display instructions to a user.]

d) Method intToFloat, which takesinteger argument number and returns a float.

6.5 Find theerror in each of the following program segments. Explain how to correct theerror.

a) void g()

{

System.out.println("Insidemethod g");

void h()

{

System.out.println("Insidemethod h");

}

}

b) int sum(int x, int y)

{

int result;

result = x + y;

}

c) void f(float a);

{

float a;

System.out.println(a);

}

d) void product()

{

int a = 6, b = 5, c = 4, result;

result = a * b * c;

System.out.printf("Resultis %d%n", result);

return result;

}

6.6 Declaremethod sphereVolume to calculate and return the volume of the sphere. Use the followingstatement to calculate the volume:

double volume = (4.0 / 3.0) * Math.PI *Math.pow(radius, 3)

Write a Java applicationthat prompts the user for the double radius of a sphere, callssphereVolume to calculate the volume and displays the result.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值