Java Reference
In-Depth Information
Do you want to hear it again? y
She sells seashells by the seashore.
Do you want to hear it again? n
10. Write a method called zeroDigits that accepts an integer parameter and returns the number of digits in the number
that have the value 0 . For example, the call zeroDigits(5024036) should return 2 , and zeroDigits(743)
should return 0 . The call zeroDigits(0) should return 1 . (We suggest you use a do/while loop in your solution.)
11. Write a do/while loop that repeatedly prints random numbers between 0 and 1000 until a number above 900 is
printed. At least one line of output should always be printed, even if the first random number is above 900 . Here is a
sample execution:
Random number: 235
Random number: 15
Random number: 810
Random number: 147
Random number: 915
Section 5.2: Fencepost Algorithms
12. Consider the flawed method printLetters that follows, which accepts a String as its parameter and attempts to
print the letters of the String , separated by dashes. For example, the call of printLetters("Rabbit") should
print R-a-b-b-i-t . The following code is incorrect:
public static void printLetters(String text) {
for (int i = 0; i < text.length(); i++) {
System.out.print(text.charAt(i) + "-");
}
System.out.println(); // to end the line of output
}
What is wrong with the code? How can it be corrected to produce the desired behavior?
13. Write a sentinel loop that repeatedly prompts the user to enter a number and, once the number -1 is typed, displays
the maximum and minimum numbers that the user entered. Here is a sample dialogue:
Type a number (or -1 to stop): 5
Type a number (or -1 to stop): 2
Type a number (or -1 to stop): 17
Type a number (or -1 to stop): 8
Type a number (or -1 to stop): -1
Maximum was 17
Minimum was 2
If -1 is the first number typed, no maximum or minimum should be printed. In this case, the dialogue would look
like this:
Type a number (or -1 to stop): -1
Search WWH ::




Custom Search