Chapter 3: Extending Hello World

The exercises here are taken from my book, The Java Developer's Resource.

Quiz

  1. What does the following program print?
    
    // 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.

  2. What's wrong with this program?
    
    // 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 <.

  3. What happens if you don't give Program 3.10 any command line arguments? You aren't testing the number of command line arguments anymore so why isn't an 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.

  4. For math whizzes only: I lied. In certain interpretations of certain number systems the statement i = i + 1 does have a valid solution for i. What is it?

    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."

Exercises

  1. Write a program that prints all the integers between 0 and 36.

    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);
         }
         
      }
      
    }
  2. Imagine you need to open a standard combination dial lock but don't know the combination and don't have a pair of bolt cutters. Write a program that that prints all possible combinations so you can print them on a piece of paper and check off each one as you try it. Assume the numbers on the dial range from zero to thirty-six and three numbers in sequence are needed to open the lock.

    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;
         }
         
      }
      
    }
  3. Suppose the lock isn't a very good one, and any number that's no more than one away from the correct number will also work. In other words if the combination is 17-6-32 then 18-5-31 will also open the lock. Write a program that prints the mininum number of combinations you need to try to guarantee opening the lock.

    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;
         }
         
      }
      
    }
  4. The Fibonacci numbers are defined as follows. The zeroth Fibonacci number is 1. The first Fibonacci number is also 1. The second Fibonacci number is 1 + 1 = 2. The third Fibonacci number is 1 + 2 = 3. In other words, except for the first two numbers each Fibonacci number is the sum of the two previous numbers. Write a program that prints the first 20 Fibonacci numbers.
    
    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;
        }
    
      }
      
    }
    


[ Exercises | Cafe Au Lait | Books | Trade Shows | Links | FAQ | Tutorial | User Groups ]

Copyright 1996-1999 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified July 5, 1999