// This is the Hello Rusty program in Java
class HelloRusty {
public static void main (String args[]) {
String name = "Rusty";
/* Now let's say hello */
System.out.println("Hello + name");
}
}
This program prints
Hello + name
The problem is that the second quotation mark should be after the word Hello, not after name. Variable names have no special meanings inside a String.
// This is the Hello program in Java
class Hello {
public static void main (String args[]) {
int i;
System.out.print("Hello "); // Say Hello
i = 0; // Initialize loop counter
while (i <= args.length) { // Test and Loop
System.out.print(args[i] + " ");
i = i + 1; // Increment Loop Counter
}
System.out.println(); // Finish the line
}
}
This program encounters an ArrayIndexOutOfBoundsException
when i becomes eqaul to args.length. Array indexes start at zero
and count to one less than the length of the array as in C and
not at 1 as in Fortran. The <= sign should just be a <.
ArrayIndexOutOfBoundsException
thrown?
i
is initialized to zero. If the length of the array is zero,
then i
is not less than args.length
, so it never enters the loop
and it never tries to read an array element that isn't there.
If infinity is considered to be a number, then infinity + 1 equals infinity.
Joshua Davis of Oberllin College's Mathematics Department suggested the alternate and equally valid answer "When '+' represents a group operator, and the group is, for example, the reals under multiplication."
class thirtysix {
public static void main(String[] args) {
int i;
i = 0;
while (i < 37) {
System.out.println(i);
i = i + 1;
}
}
}
In future chapters you'll see that this can also be written more compactly as:
class thirtysix {
public static void main(String[] args) {
for (int i = 0; i <= 36; i++) {
System.out.println(i);
}
}
}
The trick here is to use three nested loops, one for each number. For example:
class lock {
public static void main(String[] args) {
int i;
int j;
int k;
i = 0;
while (i <= 36) {
j = 0;
while (j <= 36) {
k = 0;
while (k <= 36) {
System.out.println(i + " " + j + " " + k);
k = k + 1;
}
j = j + 1;
}
i = i + 1;
}
}
}
The trick here is to count by three instead of by one; i.e. 1, 4, 7, 10, 13, and so on. 1 is one away from 2 and 2 is one away from 3. 4 is one away from 3 and 5 is one away from 6, and so on. This reduces the number of combinations you have to test from 50,653 (37 times 37 times 37) to only 2,197 (13 times 13 times 13).
Also remember that on a circular combination lock starting at one, the number one is only one away from the number thirty-six. For example:
class badlock {
public static void main(String[] args) {
int i;
int j;
int k;
i = 2;
while (i <= 36) {
j = 2;
while (j <= 36) {
k = 2;
while (k <= 36) {
System.out.println(i + " " + j + " " + k);
k = k + 3;
}
j = j + 3;
}
i = i + 3;
}
}
}
class Fibonacci {
public static void main (String args[]) {
int i;
int fibold;
int fibnew;
int temp;
i = 0;
fibold = 1;
System.out.println(fibold);
fibnew = 1;
System.out.println(fibnew);
while (i < 19) {
temp = fibold + fibnew;
fibold = fibnew;
fibnew = temp;
System.out.println(fibnew);
i = i + 1;
}
}
}