Java Software Solutions, 8e, Global Edition Exercise Solutions, Ch. 6
Chapter 6 Exercise Solutions
EX 6.1. How many iterations will the following for loops execute?
EX 6.2. What output is produced by the following code fragment?
EX 6.3. What output is produced by the following code fragment?
EX 6.4. Transform the following while loop into an equivalent do loop (make sure it
produces the same output).
int num = 1; while (num < 20) {
EX 6.5. Transform the while loop from the previous exercise into an equivalent for loop (make
sure it produces the same output).
EX 6.6. Write a do loop that verifies that the user entered value is a multiple of 3.
Scanner scan = new Scanner(System.in);
do
{
EX 6.7. Write a for loop to print the even numbers from 150 down to 50 (inclusive).
EX 6.8. Write a for loop to print the odd multiples of 5 from 1 to 100.
EX 6.9. Write a code fragment that reads 15 floating point values from the user and prints the
lowest and the highest value entered.
Scanner scan = new Scanner(System.in);
float min, max, number;
EX 6.10. Write a code fragment that determines whether the last character of the String object
called text is character n’ or not.
EX 6.11. Write a code fragment that prints the characters stored in a String object called str
backwards.
EX 6.12. Write a code fragment that prints every other character in a String object called word
starting with the first character.
EX 6.13. Write a method called multiplesOfFive that prints the first 5 multiples of 5 (starting
with 5). The method takes no parameters and doesn‟t return anything.
}
EX 6.14. Write a method called alarm that prints the string “Alarm!” multiple times on separate
lines. The method should accept an integer parameter that specifies how many times the
string is printed. Print an error message if the parameter is less than 1.
EX 6.15. Write a method called average25 that returns the average of the integers from 1 to 25,
inclusive.
}
EX 6.16. Write a method called sumRange that accepts two integer parameters that represent a
range. Issue an error message and return zero if the second parameter is less than the
first. Otherwise, the method should return the sum of the integers in that range (inclusive).
EX 6.17. Write a Boolean method called startT that accepts a String parameter and returns true
if the string starts with “T”, otherwise returns false.
EX 6.18. Write a method called reverse that accepts a String parameter and returns a string
that contains the characters of the parameter in reverse order. Note that there is a
method in the String class that performs this operation, but for the sake of this
exercise, you are expected to write your own.