Java Reference
In-Depth Information
12. The code has a fencepost problem; it will print a dash after the last letter. The fol-
lowing code corrects the problem:
public static void printLetters(String text) {
if (text.length() > 0) {
System.out.print(text.charAt(0));
for (int i = 1; i < text.length(); i++) {
System.out.print("-" + text.charAt(i));
}
}
System.out.println(); // to end the line of output
}
13. int SENTINEL = -1;
System.out.print("Type a number (or " + SENTINEL + " to stop): ");
Scanner console = new Scanner(System.in);
int input = console.nextInt();
int min = input;
int max = input;
while (input != SENTINEL) {
if (input < min) {
min = input;
} else if (input > max) {
max = input;
}
System.out.print("Type a number (or " + SENTINEL
+ " to stop): ");
input = console.nextInt();
}
if (min != SENTINEL) {
System.out.println("Maximum was " + max);
System.out.println("Minimum was " + min);
}
14. (a) true
(b) true
(c) false
(d) true
(e) true
(f) false
(g) false
(h) true
(i) true
(j) true
(k) true
(l) false
Search WWH ::




Custom Search