Lab 11

Java : The Programming Language

Objectives:


[Turnin: a .tar.gz file]

Procedure:



Step 1

Getting your grubby paws on the Java Development Environment:

Java can be downloaded for free. For instructions on how to install Java on your machine go to this website :
http://www.cc.gatech.edu/classes/AY2000/cs1312/

For NT/Linux Users

  • In the left hand frame click first on Installing JDKs
  • When the page loads up pick your poison
  • If you have problems post questions to: git.cc.class.cs1311x.lab
  • For others

  • In the left hand frame click first on Installing JDKs
  • When the page loads up pick your poison
  • If you have problems post questions to: git.cc.class.cs1312.questions


  • Step 2

    The Java Development Environment

    Unlike DrScheme where you had one program that did everything writing code in Java involves the following steps:

  • The use of some text editing environment to write your code
  • Compiling/Interpreting the code using the Java Development Kit (JDK)
  • Running the code use the Java Runtime Environment that comes packaged with the JDK
  • The first step in this process you should already be familiar with. Lab 8 introduced you to a great text editing tool, EMACS. This lab is written to go hand in hand with your already proficient EMACS skills.

    Note: This is America jack, the land of the free, and you can use any text editor that you choose. We can't stop you from shooting yourself in the foot. The use however of Pico, notepad or any other inferior enviroment will not garner any sympathy from us TAs. If you want to conduct activites that are highly detremental to your academic future, then you can suffer by yourself. Embracing EMACS now will prove to be a TREMENDOUS help in later parts of the course, as well as in cs1312.

    EMACS has been developed over 20 years. It's stable, versatile, and a very capable editor. EMACS supports development in every computer language ever invented. Learn how to use EMACS or a similarly capable editor such as VI soon.



    Step 3

    EMACS, too, can be downloaded for free. For instructions on how to install EMACS on your machine go to this website :
    http://www.cc.gatech.edu/classes/AY2000/cs1312/

    For NT/Linux Users

  • In the left hand frame click first on Installing Emacs
  • When the page loads up pick your poison
  • If you have problems post questions to: git.cc.class.cs1311x.lab
  • For others

  • In the left hand frame click first on Installing Emacs
  • When the page loads up pick your poison
  • If you have problems post questions to: git.cc.class.cs1312.questions


  • Step 4

    RTFM: Reading the free manual

    This lab is long. All you are asked to do is read this lab carefully and follow some simple steps. If you don't read/follow these instructions, then you will be left all alone in the dark to grope for a flash light.

    If you are using EMACS, look for the line ENLIGHTENED EMACS USERS LOOKY HERE for specific instructions. If you are not using EMACS or feel secure in your EMACS skills, you may skip these chances for enlightenment.

    To start NTEmacs, select the appropriate folder from the Start menu. This distribution is made available on the CS1312 web page as well. Further, emacs runs on Windows, Macs, and even real operating systems like Solaris, Linux and *BSD.

    Ladies and Gentleman you may now start your editor.

    To complete this lab you will need to know how to how to open a file, edit, and save. If you can't do this in EMACS then re-read Lab 8 or print out this list of easy-to-memorize commands. If you aren't using EMACS then you are on your own.

    ENLIGHTENED EMACS USERS LOOKY HERE

    And now for the best feature of emacs EVER. Emacs will automatically indent your code for you. Yes. No more hitting spaces and fussing with margins. No other IDE does it as well as emacs.

    To layout a line, just hit the tab key on the line you wish to adjust. called TestLayout.java --a little program written for this demonstration. You don't have to know this works yet.

    To open it, type:

    C-x C-f TestLa < TAB >

    Hit the Tab key to have emacs finish typing the file name for you. Or, type the whole file name. Looks pretty messed up, eh? Lines everywhere. This kind of program would lose a few style points if it were handed in. Put your cursor on each line, starting at the top and working down, hitting TAB. WHAMMO! Emacs knows how to layout your document. Note you can do this to the entire document at once by typing:

    C-x h < RETURN > This is like "Select All", it captures your entire document. Then, type in:

    M-x indent-region < RETURN > Remember, the M above means "ESC". This auto-indent will save you time and points on programs. Don't forget to save your file with C-x C-s <RETURN> (If it saves you lots of extra time, you can watch the towers of hanoi to pass the time. Type in M-x hanoi < RETURN > while your fellow class mates struggle to layout their code. Part of being an emacs user is being very lazy, and doing things that save you time. Had enough with this demo? Close or kill off this buffer with C-x k < RETURN >)



    Step 4

    Commenting in Java

    Unlike Scheme where you comment code using the ; In Java commenting involves using slashes and asterisks in the header.

    These are many types of comment in Java. One consists of /* and */ . Everything in between the first pair ( /* ) and the second pair ( */ ) is ignored by the compiler. For example,

    /* this is a comment */ 

    These kinds of comments can also span multiple lines. For example,

    /* this is a comment
    that spans multiple lines */
    

    Another common format for these comments (called a javadoc comment) is

    /** this is a javadoc comment
    * that spans multiple lines
    * and is formatted differently
    */ 

    The other kind of comment consists of two slashes, // . Everything on the same line that follows this comment is ignored. The following comment is equivalent to the one above:

    // this is a comment 

    These kinds of comments do not span multiple lines.
    For instance:

    // this isn't quite
    a comment 

    Notice that "a comment" is not on the same line as "this isn't quite" therefore it is not commented out. Only "this isn't quite" got commented.


    ENLIGHTENED EMACS USERS LOOKY HERE

    You can also comment out a large region in emacs. Mark a region (using a mouse drag or C-@ followed the arrow keys). Then, type in:

    M-x comment-region < RETURN > *OR* C-c C-c

    Presto! The region is entirely commented out. This will be helpful when debugging your code and testin complex programs. To undo the commenting, just type C-_

    The file that you turn in must begin with a header block. A header is a block of comments placed at the very top of your programs.



    Step 5

    Header Files

    Here's what a sample header looks like:

    /**
    * CS1311x: Lab #0
    *
    * < PRE>
    * Intro to Java
    * * Revisions: 1.0 Jan 3, 1999
    * Created class Lab11
    * </PRE>
    *
    * @author < A HREF="mailto:doe@cc.gatech.edu" > John Doe </A >
    * @version Version 1.0, Jan 3, 1999
    */
    

    Open the file ident.txt. Modify it so that it accurately reflects your personal information. This header needs to be at the top of all *.java files submitted electronically. You have been warned! You can potentially lose credit on assignments for forgetting the identity box.

    First, start up your editor if you haven't already Open up the file "ident.txt". Edit the file to reflect your personal information. Save the file. Now, insert the contents of "ident.txt" into all the files that you will be turning in lab11.java.


    Step 6

    The Big Picture

    First, all the Java programs you write are just text files. You follow some very specific rules called language constructs to create your program. Once your program is created you compile it. By compiling your program, you are making a special format that the computer can understand and execute. That's leaving out a lot of details but hopefully that will give you an idea of what's going on when you hear stuff about "compiling" your code.

    For example, suppose you created a text file called Test.java. The file Test.java is readable by humans. (Well, it's perhaps not very interesting to read, but it *can* be read. :) This source code cannot be run by the computer. Instead, you have to compile the source into a file called Test.class that can be executed. The Test.class file merely contains a set of bytes (numbers, really) that are 'op codes' or operation instructions for the computer.



    compile ------- > run ----------- > :)

    Test.java ------- > Test.class ---- > :)



    Step 7

    Compiling Java

    We strongly recommend you compile using a command prompt. Here' how. From the Start menu, select "Programs" and then "MSDOS PROMPT." Before you can run your program, you must go into the directory that has the files. Navigate to the directory that contains your .java files.

    Type the following at the command prompt:

    javac *.java

    This calls on the Java compiler to compile all the .java files. Remember that all .java files are compiled into .class files.


    ENLIGHTENED EMACS USERS LOOKY HERE

    This part is sweet. To compile your program, just type in:

    C-c C-v C-c
    That's "control c, control v and control c".

    Emacs should split the screen and show your code on the top half, and the compiler's progress on the bottom.

    If you have any errors, you can type:

    C-x o

    to cycle between split screens. Move the cursor to the compiler's output, and put the cursor right on top of the first error message. Hit return. WHAM! Emacs should take you right to the error, and position your cursor right on the line.

    If you want to go to a particular line, type:

    C-x C-j, (or, type " x goto-line")

    followed by the line number. You can see what line number you are on by looking at the status bar. (If your status bar does not display the line number, type:

    M-x line-number-mode

    to toggle between the line number showing/hidden

    NOTE: For the first lab, if you are using Windows, you are likely to get an error in compilation saying that -Xdepend is not a valid option. This is because OIT still has the old version of Java on their machines. Shortly, this will be fixed, and this trick will work nicely with JDK 1.2. For now, if you get such an error, don't worry. Just open a dos prompt, and compile by typing:

    javac *.java

    If things were setup correctly, you should be able to compile a file now.

    Download the lab11.java file from here The lab11.java file should compile without errors. If you do have errors, make sure you didn't accidentally remove a comment, or alter the file in any way other than adding your ident box. If you can't locate the problem, try removing your changes and getting it to compile first. Then go back and add your ident box back taking care not to clip a part of a commented section. If all else fails, post to the lab newsgroup.

    Check your directory (you can either use NT Explorer or the DOS prompt), and you should see a file called "lab11.class". This is the file that we compiled.

    Let's try running this. To run your program, type:

    java lab11

    Note that this differs from the other command line above. When running .class files, you do not need to include the file extension.
    ENLIGHTENED EMACS USERS LOOKY HERE

    You can even run the program inside of emacs. (The idea is "stay in emacs; keep your hands on the keyboard.") To do this, we first need to create a DOS shell *inside* emacs. (This also works on unix) Type in:

    M-x shell < RETURN >

    This creates a little dos window inside an emacs buffer. (This is handy for debugging lenghty output that might otherwise scroll too fast in a Windows-based DOS terminal.) Then, type in:

    java lab11

    just like in a dos window. Nice, eh? To get back to your coding buffer, select the "Buffers" menu or type in C-x b < TAB > to see a list of available buffers.

    AN EVEN COOLER WAY!

    Type: C-c C-v C-r
    This will automatically run the compiled file, just as java lab11 does!

    When running the program, you should see something that looks like:

    The Sample output should look like this:

    I like VI personally!
    I was a slacker and now I have to preach the EMACS gospel =(
    This is Fruitcake's first Java program

    The word Fruitcake must be replaced with your last name. Seeing your name appear on the screen should be the high point of your day. You should realize that the thrill off seeing your name appear on a screen for the first time, after being generated by a program, is never again achievable. It is all downhill from here...



    Step 8

    Exiting Emacs

    All done? If you used emacs, you probably finished sooner than expected. How about a quick game of tetris? It's included in the NTEmacs distribution:

    M-x tetris < RETURN >

    Remember, 'stay in emacs' even when relaxing. (Later we will cover how to read your main in emacs, read newsgroups, surf the web--all inside emacs.) When done, quit emacs with:

    C-x C-c



    Step 9

    Transferring Files

    Important:
    .java files have to be transferred in ASCII.
    .class files have to be transferred in binary.


    Step 10