Java Reference
In-Depth Information
if (s.charAt(i) == 'e') {
count++;
}
}
20. The following expression would produce the desired result:
String name = "Marla Singer";
int space = name.indexOf(" ");
String lastName = name.substring(space + 1);
String firstInitial = name.substring(0, 1);
String lastNameFirstInitial = lastName + ", " +
firstInitial + ".";
System.out.println(lastNameFirstInitial);
Alternatively, you could use this shorter version:
String name = "Marla Singer";
System.out.println(name.substring(name.indexOf(" ") + 1) +
", " + name.charAt(0) + ".");
21. // assuming that the String is stored in the variable str
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (Character.toLowerCase(str.charAt(i)) >= 'n') {
count++;
}
}
System.out.println(count + " letters come after n.");
22. public static void printTriangleType(int s1, int s2, int s3) {
if (s1 == s2 && s2 == s3) {
System.out.println("equilateral");
} else if (s1 == s2 || s1 == s3 || s2 == s3) {
System.out.println("isosceles");
} else {
System.out.println("scalene");
}
}
Invalid values include negative numbers for a side's length and a number for any
one side length that is greater than the sum of the other two side lengths, because
this cannot be a valid triangle. The precondition of the printTriangleType
method is that the side lengths must constitute a valid triangle.
23. The preconditions of this method are that the grade parameter's value is between
0 and 100 .
Search WWH ::




Custom Search