|
| |
Review Questions
- Answer each of the following: Solutions
- Program modules in Java are called _____ and _____.
- A method is invoked with a _____.
- A variable known only within the method in which it is defined is
called a _____.
- The _____ statement in a called method can be used to pass the value
of an expression back to the calling method.
- The keyword _____ is used in a method header to indicate that a method
does not return a value.
- The _____ of an identifier is the portion of the program in which the
identifier can be used.
- The _____ method is used to produce random numbers.
- The _____ qualifier is used to declare read-only variables.
- Write an application that will display the following using the Math
library methods:
|
Solutions
|
What do you think the answer will be before
running the application? |
- The absolute value of -34.5
- The absolute value of 0.0
- The absolute value of 45.3
- The cosine of 0.0 radians
- The cosine of 1.0 radians
- The ceiling of 9.5
- The ceiling of -12.4
- The floor of 8.4
- The floor of -34.4
- The larger of -34.4 and -12.4
- The smaller of 8.4 and 9.5
- The value of 45
- The value of 40.5
- The value of 4-5
- A random decimal number from 0 to 1
- Round 5.6
- Round -5.6
- The sine of -1.0 radians
- The sine of 1.0 radians
- The square root of 10.
- The square root of 2560.5
- The tan of -1.0 radians
- The tan of 1.0 radians
- The number of radians in 45°
- The number of radians in 135°
|
|
- Find the error(s) in each of the following program segments and explain how
the error can be corrected. Solutions
- Give the method header for each of the following methods. Solutions
- Method hypotenuse which takes two double-precision,
floating-point arguments side1 and side2 and returns a
double-precision, floating-point result.
- Method smallest, which takes three integers, x, y,
and z and returns an integer.
- Method instructions, which does not take any arguments and does
not return a value. Note: this type of method may be used to show
instructions to the user.
- Method intToFloat, which takes an integer argument, number
and returns a floating-point result.
- Write a complete Java application to find the volume of a sphere, Use a
method to prompt the user for the double radius of a sphere. A
method, sVolume, is used to calculate the volume. The application
displays the volume with a concluding sentence. Solution
- Write a statement that assigns a random integer to the variable n
in the following ranges.
- Write a single statement that will print a number at random from the set
of numbers:
6, 10, 14, 18, 22.
- a) Write a method named average that receives 2 integer numbers and
returns the average of the two numbers.
b) Write an application that makes use of the method average. Solution
- Complete the method described in 4a and use it in an application.
- Complete the method described in 4b and use it in an application.
- a) Write a String method named changeCase. It accepts a string and
a character. The character is 'U' or 'u' or 'L' or 'l' (lower case L). If it
is U or u then the returned string should be uppercase. If it is L or l then
the returned string should be lowercase. Any other character should return
the string unchanged.
b) Write an application that makes use of the method in (a). Solution
-
Source: Deitel, H.M. and Deitel P.J. Java: How to Program. 3rd ed. Upper Saddle River, New
Jersey. Prentice Hall.1999.
ISBN 0-13-012507-5, p 254-261. and others.
|