Chapter 2

The exercises here are taken from my forthcoming book tentatively titled Getting Started with Java though the name will probably change before it's published.

Quiz

  1. What happens if you change the name of the source code file, e.g. HelloEarth.java instead of HelloWorld.java?

    For now this changes nothing. The name of the source code file appears not to matter. However it will matter a great deal in the not too distant future so don't go arbitrarily changing the names of your .java files.

  2. What happens if you keep the name of the source code file the same (HelloWorld.java) but change the class's name, e.g. class HelloEarth?

    For now this changes nothing. The name of the source code file appears not to matter. However it will matter a great deal in the not too distant future so don't go arbitrarily changing the names of your .java files.

  3. How would you write the Hello World program in French? Spanish? Latin? Arabic?

    In French:

    
    class HelloWorld {
    
      public static void main (String args[]) {
    
        System.out.println("Bonjour Monde!");
    
      }
      
    }
    
    In Spanish:

    
    class HelloWorld {
    
      public static void main (String args[]) {
    
        System.out.println("Hola Mundo!");
    
      }
      
    }
    
    In Latin:

    
    class HelloWorld {
    
      public static void main (String args[]) {
    
        System.out.println("Vale Mundum!");
    
      }
      
    }
    
    In other words, you only translate the String, not the code itself. Java is still an English based langauge. Arabic's impossible in Java 1.0 because of the alphabet problem. It may be possible in future releases using Unicode escapes. (See Chapter 5).

  4. What would happen if you added the following lines to the Hello World program?

    
    /* THIS PROGRAM CANNOT BE EXECUTED
    WITHOUT SYSADMIN PRIVILEGES */
    
    Absolutely nothing. These lines are comments. They have no effect on the final output.

Exercises

  1. Personalize the Hello World program with your name so that it tells you Hello rather than the somewhat generic "World."

    All you need to do is change the word World to your name. For example

    
    class HelloWorld {
    
      public static void main (String args[]) {
    
        System.out.println("Hello Rusty!");
    
      }
      
    }
    
    You may of course use your own name instead of Rusty.

  2. Write a program that produces the following output:
    
    Hello World!
    It's been nice knowing you.
    Goodbye world!
    
    This is three lines of text so it needs three System.out.println() statements. In other words:

    
    class HelloWorld {
    
      public static void main (String args[]) {
    
        System.out.println("Hello World!");
        System.out.println("It's been nice knowing you.");
        System.out.println("Goodbye world!");
    
      }
      
    }
    
    C programmer's can note that \n works in Java just like it works in C so you could also write
    
    class HelloWorld {
    
      public static void main (String args[]) {
    
        System.out.println("Hello World!\nIt's been nice knowing you.\nGoodbye world!");
    
      }
      
    }
    

  3. Write a program that draws the following figures one above the other.
    * * * * *             *  
    * * * * *            * *  
    * * * * *           * * *
    * * * * *         * * * * *
    
    Now modify it to draw them next to each other like above.

    
    class DrawPicture1 {
    
      public static void main (String args[]) {
    
        System.out.println("* * * * *");
        System.out.println("* * * * *");
        System.out.println("* * * * *");
        System.out.println("* * * * *");
        System.out.println("    *");
        System.out.println("   * *");
        System.out.println("  * * *");
        System.out.println("* * * * *");
      }
      
    }
    
    and

    
    class DrawPicture2 {
    
      public static void main (String args[]) {
    
        System.out.println("* * * * *             *  ");
        System.out.println("* * * * *            * * ");
        System.out.println("* * * * *           * * *");
        System.out.println("* * * * *         * * * * *");
    
      }
      
    }
    


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

Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
Last Modified April 3, 1996