More Data Types and Operators Flashcards
Show two ways to declare a one-dimensional array of 12 doubles.
double x[] = new double[12];
double[] x = new double[12];
Show how to initialize a one-dimensional array of integers to the values 1 through 5.
int[] x = { 1, 2, 3, 4, 5 };
Write a program that uses an array to find the average of ten double values. Use any ten
values you like.
// Average 10 double values.
class Avg {
public static void main(String[] args) {
double[] nums = { 1.1, 2.2, 3.3, 4.4, 5.5,
6.6, 7.7, 8.8, 9.9, 10.1 };
double sum = 0;
for(int i=0; i < nums.length; i++) sum += nums[i]; System.out.println("Average: " + sum / nums.length); } }
Change the sort in Try This 5-1 so that it sorts an array of strings. Demonstrate that it works.
// Demonstrate the Bubble sort with strings.
class StrBubble {
public static void main(String[] args) {
String[] strs = {
“this”, “is”, “a”, “test”,
“of”, “a”, “string”, “sort”
};
int a, b;
String t;
int size;
size = strs.length; // number of elements to sort // display original array System.out.print("Original array is:"); for(int i=0; i < size; i++) System.out.print(" " + strs[i]); System.out.println(); // This is the bubble sort for strings. for(a=1; a < size; a++) for(b=size-1; b >= a; b--) { if(strs[b-1].compareTo(strs[b]) > 0) { // if out of order // exchange elements t = strs[b-1]; strs[b-1] = strs[b]; strs[b] = t; } } // display sorted array System.out.print("Sorted array is:"); for(int i=0; i < size; i++) System.out.print(" " + strs[i]); System.out.println(); } }
What is the difference between the String methods indexOf( ) and lastIndexOf( )?
The indexOf( ) method finds the first occurrence of the specified substring. lastIndexOf( ) finds the
last occurrence.
Since all strings are objects of type String, show how you can call the length( ) and charAt( ) methods on this string literal: “I like Java”.
As strange as it may look, this is a valid call to length( ):
System.out.println(“I like Java”.length());
The output displayed is 11. charAt( ) is called in a similar fashion.
Expanding on the Encode cipher class, modify it so that it uses an eight-character string as
the key.
// An improved XOR cipher.
class Encode {
public static void main(String[] args) {
String msg = “This is a test”;
String encmsg = “”;
String decmsg = “”;
String key = “abcdefgi”;
int j;
System.out.print("Original message: "); System.out.println(msg); // encode the message j = 0; for(int i=0; i < msg.length(); i++) { encmsg = encmsg + (char) (msg.charAt(i) ^ key.charAt(j)); j++; if(j==8) j = 0; } System.out.print("Encoded message: "); System.out.println(encmsg); // decode the message j = 0; for(int i=0; i < msg.length(); i++) { decmsg = decmsg + (char) (encmsg.charAt(i) ^ key.charAt(j)); j++; if(j==8) j = 0; } System.out.print("Decoded message: "); System.out.println(decmsg); } }
Can the bitwise operators be applied to the double type?
No.
Show how this sequence can be rewritten using the ? operator.
if(x < 0) y = 10;
else y = 20;
Here is the answer:
y = x < 0 ? 10 : 20;
In the following fragment, is the & a bitwise or logical operator? Why?
boolean a, b;
// …
if(a & b) …
It is a logical operator because the operands are of type boolean.
Is it an error to overrun the end of an array?
Is it an error to index an array with a negative value?
Yes.
Yes. All array indexes start at zero.
What is the unsigned right-shift operator?
> > >
Rewrite the MinMax class shown earlier in this chapter so that it uses a for-each style for loop.
// Find the minimum and maximum values in an array.
class MinMax {
public static void main(String[] args) {
int[] nums = new int[10];
int min, max;
nums[0] = 99; nums[1] = -10; nums[2] = 100123; nums[3] = 18; nums[4] = -978; nums[5] = 5623; nums[6] = 463; nums[7] = -9; nums[8] = 287; nums[9] = 49; min = max = nums[0]; for(int v : nums) { if(v < min) min = v; if(v > max) max = v; } System.out.println("min and max: " + min + " " + max); } }
Can the for loops that perform sorting in the Bubble class shown in Try This 5-1 be converted into for-each style loops? If not, why not?
No, the for loops in the Bubble class that perform the sort cannot be converted into for-each style loops. In the case of the outer loop, the current value of its loop counter is needed by the inner loop. In the case of the inner loop, out-of-order values must be exchanged, which implies assignments. Assignments to the underlying array cannot take place when using a for-each style loop.
Can a String control a switch statement?
Yes.