John Llyod E
John Llyod E
3. Write a program that uses an array to find the average of 10 double values. Use any 10
double[] values = {21.5, 12.1, 17.6, 15.3, 26.0, 12.8, 19.2, 14.7, 11.4, 25.1};
double sum = 0;
sum += value;
4. Change the sort in Project 5-1 so that it sorts an array of strings. Demonstrate that it works.
import java.util.Arrays;
System.out.println(str);
5. What is the difference between the String methods indexOf( ) and lastIndexOf( )?
- indexOf() finds the first occurrence of a substring from the beginning of the string,
lastIndexOf() finds the last occurrence of a substring starting from the end of the string.
6. Since all strings are objects of type String, show how you can call the length( ) and
7. Expanding on the Encode cipher class, modify it so that it uses an eight-character string
as the key.
if (key.length() != 8) {
this.key = key;
encodedMessage.append(encodedChar);
return encodedMessage.toString();
}
8. Can the bitwise operators be applied to the double type?
-No, bitwise operators cannot be directly applied to the double type in most programming languages.
else y = 20;
-y = (x < 0) ? 10 : 20;
10. In the following fragment, is the & a bitwise or logical operator? Why?
boolean a, b;
// ...
if(a & b) …
---The & operator in the expression if(a & b) is a bitwise operator. It performs a bitwise AND operation
on the individual bits of the boolean values a and b. This means that it evaluates both a and b regardless
of their values and returns true only if both a and b are true, otherwise it returns false.
If you intended to use a logical AND operator that short-circuits, meaning it evaluates b only if a is true,
you would use the && operator instead of &.
11. Is it an error to overrun the end of an array? Is it an error to index an array with a
negative value?
- (>>>)
13. Rewrite the MinMax class shown earlier in this chapter so that it uses a for-each style
int[] nums = { 99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49 };
14. Can the for loops that perform sorting in the Bubble class shown in Project 5-1 be
-No, the for loops that perform sorting in the Bubble class shown in Try This 5-1 cannot be converted
into for-each style loops.