Java Software Solutions, 8H*OREDO(GLWLRQ
Exercise Solutions, Ch. 3
Chapter 3 Exercise Solutions
EX 3.1. Write a statements that prints the number of characters in a String object called
EX 3.2. Write a statement that prints the 8th character of a String object called
EX 3.3. Declare a String variable named str and initialize it to contain the same
characters as a String object called name, except in all uppercase characters.
EX 3.4. Write a declaration for a String variable called change and initialize it to the
characters stored in another String object called original with all ‘e’
characters changed to ‘j’.
EX 3.5. What output is produced by the following code fragment?
The letter “e”.
EX 3.6. What is the effect of the following import statement?
import java.awt.*;
EX 3.7. Assuming that a Random object has been created called generator, what is the
range of the result of each of the following expressions?
a. generator.nextInt(20)
0 to 19, inclusive
b. generator.nextInt(8) + 1
1 to 8, incluslive
c. generator.nextInt(12) + 2
2 to 13, inclusive
d. generator.nextInt(35) + 10
Java Software Solutions, 8e, Global Edition
Exercise Solutions, Ch. 3
e. generator.nextInt(100) 50
EX 3.8. Write code to declare and instantiate an object of the Random class (call the
object reference variable rand). Then write a list of expressions using the
nextInt method that generate random numbers in the following specified
ranges, including the endpoints. Use the version of the nextInt method that
accepts a single integer parameter.
a. 0 to 10
b. 0 to 400
c. 1 to 10
d. 1 to 400
e. 25 to 50
f. 10 to 15
EX 3.9. Write an assignment statement that computes the square root of var1,
subtracts it from var2 and assigns the result to var3.
EX 3.10. Write a single statement that computes and prints the fifth power of sum.
EX 3.11. Write code statements to create a DecimalFormat object that will round a
formatted value to 4 decimal places. Then write a statement that uses that object
to print the value of result, properly formatted.
EX 3.12. Write code statements that prompt for and read a double value from the user,
and then print the result of raising that value to the fourth power. Output the
results to 3 decimal places.
EX 3.13. Write a declaration for an enumerated type that represents the months of the year.