Java Programming

Java is a programming language.  You can use it to write commands that tell the computer what it should do.

A program is a sequence of commands.  The computer will execute the commands in order.

Java is a very exact language - it is not as sloppy or forgiving as English.  The computer will do exactly what you write in the program.  

    "Careful what you wish for - you might get it."

If you write the commands exactly correctly, the computer will do what you want.  Otherwise, it might do something different.  You need to master the Java language so that you can successfully make the computer do things that are useful and correct.

First Program

Here is a very simple Java program:
 public class Magic
 {
    public static void main(String[] args)
    {
       System.out.println("The magic word is");

       System.out.println("ABRACADABRA");
    }
 }

Like a song, it is more interesting when it is performed than just sitting there on the page.  

One, two, three ... Play!

There are 3 steps in creating and running a program:

  1. Edit - type the code (commands) into the computer and save the program
  2. Compile - checks the program for errors (mistakes) and translates the commands into machine language
  3. Run - tell the computer to execute the commands

... oops, forgot to Warm Up ...

Before starting, you need to copy the editor and compiler programs onto the hard-disk.  You could run them directly from the class CD, but that would be very slow.  

  1. Create a folder with your name on drive D:
  2. Copy the folder JavaSDK from the CD into your folder.

Edit

You should find a program named ScEditor inside the JavaSDK folder on drive D:.  Start this program and you will see:

[Image]  

Of course, the program won't be there - you need to type it.  DON'T copy it - you really NEED to type it.  Be careful to put in all the correct brackets and punctuation marks.  Capital/small letters are important - they must be exactly correct.  Blank spaces don't matter, but don't put any blank spaces in the middle of words.

Question:  The picture above contains two errors - this program is not correct.  Compare it to the correct  version in the box further above.  Can you spot the two mistakes?

Save

You must save the program before compiling and running.  
You can save it anywhere (except on the CD), but you MUST use the correct name:   Magic.java  
And, yes, the capital M must be exactly right - the name of the file must match the first line of the program.

Compile

The Java compiler is a large set of programs written by Sun Microsystems and distributed for free.  The compiler is included in the JavaSDK folder that you copied.  To run the compiler, open the Tools menu and click on Compile.  

[Image]

The compiler is your best friend - it will find your mistakes for you.  It will even tell you what you did wrong - well, sort of.  It isn't nearly as clever as you might like.  It says :  ';' expected , which is correct.  But is says the error is on line 6 when it is actually on line 5.  Don't expect perfect answers from the compiler - it will give you pretty good hints about errors, and that is good enough.

Apparently there is only 1 error - the missing semi-colon ( ; ) at the end of line 5.  But the text above said there were 2 errors.  Was the text wrong?

Fix the program by adding the missing semi-colon, then compile it again.  You can only run a program after it compiles successfully, without errors.

[Image]

Wow - that error message is pretty long and confusing.  It says the error is on line 7 - that should help.  But what's wrong with this command?  The programmer accidentally type the number 1 instead of the small letter l.  They do look very similar.  Don't worry about the error message - you'll learn how to read error messages better as time goes on.  

After changing the 1 to an l, we need to compile one more time (if you have more errors, you'll need to fix them, too).

[Image]

Exit Code : 0 - means there were no errors!  Now we're ready to run.

Run

To run (start) the program, open the Tools menu and click on Go  (or simply press the F5 key).

[Image]

The program should run and print:

    The magic word is
    ABRACADABRA

If it didn't work, keep trying.


Problem 1:

   Write a new program called Haiku that prints the following:

    Computer programs
    Control the world and our lives
    Without errors, please 

   Feel free to write a better Haiku if you wish.
   Be sure to change the name of the program and save it in a different file.


Problem 2:
   This program prints a joke, then waits before printing the punch-line.
public class Joke
{
   public static void main(String[] args)
   {
      System.out.println("What's the difference between");
      System.out.println("a bagpipe and an onion?");
      
      try { Thread.sleep(5000); }
      catch (Exception e) { }
      
      System.out.println("");
      System.out.println("Nobody cries if you chop up a bagpipe.");
   }
}

Type in the program, save it (be sure to use the correct name),
compile it, remove any errors, and run it.   Then answer the following questions:

Questions:

  1. Which commands cause the computer to wait before printing the last line?
  2. Approximately how long does the computer wait?
  3. What do you need to change to make the computer wait twice as long?
  4. What is the purpose of this command:     System.out.println(""); 
  5. Did you like my joke?


Problem 3:

   Make a program that prints this (or a different word, if you prefer):

      JJJJJ    A     V   V    A
         J    A A    V   V   A A
         J   AAAAA   V   V  AAAAA
      J  J   A   A    V V   A   A
       JJ    A   A     V    A   A 

Question:  
   Explain why it is not possible to print just the big J, then wait,
    then print the big A, then wait, then print the big V, etc.


Unicode

Non-English languages use "foreign" characters, like Å ú þ.  Mathematicians use "strange" symbols, like ¾ ± x²

In a word-processor, you can insert special characthers like these.  In Java, you use Unicode - that is a number that represents a character.

For this discussion, we will focus on Greek letters.  These are used both in the Greek language and in mathematics.

     α β γ δ ε - "It's all Greek to me."

The Greek letters above are alpha, beta, gamma, delta, and epsilon.
You can find the Unicode numbers for these at:
http://www.unicode.org/charts/

To print a Unicode character from its numeric code, type something like this:

   System.out.println("\u0041");   -->  A

The example prints the letter 'A', because it's Unicode number is  0041 .  The \u  tells the computer that a Unicode number should follow.


Problem 4:

Find the Unicode numbers for each of the following symbols:

     Yen :    ¥         Copyright :    ©            Euro :      €            British Pound :   £

Then write a program that prints the following:

     $ 1  =  0.85 €  =  £ 0.65   =  100 ¥


Problem 5 :

Note that you cannot print the symbol '\' normally, because the slash is a control character and does not get printed.  To print the \ , you must write \\ :

   System.out.println("\\");   -->  \

Using double-slash to print back slashes, write a program to print a rocket:

          /\
/ \
/ \
| |
| |
| |
| |
| |
| |
| |
| | 
///\\\