Assignment 1
Home ] Up ] [ Assignment 1 ] Assignment 2 ] Assignment 3 ] Assignment 4 ] Assignment 5 ] Assignment 6 ] Assignment 7 ] Assignment 8 ]

 

 

Exploring your Java compiler/environment

Note: Please read my recommendations on how to set up your working environment, before you start working on these assignments.

  1. Become familiar with your Java compiler/development kit or Interactive Development Environment (IDE)

IDEs (Interactive Development Environments) include:

  • Borland JBuilder 2005 Foundation -- a very popular freely downloadable Java IDE (JBuilder is Rivier College's standard Java IDE).  JBuilder comes with its own Java Software Development Environment (J2SE)
  • NetBeans -- A popular freely downloadable Java IDE.  Note that NetBeans does not include its own J2SE, but needs one to be installed on your machine for NetBeans to work.  One convenient way to do this is to install the NetBeans/J2SE "bundle" (http://www.netbeans.org/downloads/index.html).
  • Eclipse -- A very popular development open source IDE.  It is written in Java, and comes with Java IDE support built in.  It can be customized by the use of "plug-ins" for numerous other purposes.

I strongly recommend an IDE, because it makes your life so much easier. However, you can do development with the bare JDK and a simple text editor. Here's what you'll need:

  1. Sun's JDK (Java Development Kit)
    • You can choose the version you want to download (I suggest you get the latest non-beta version.)
    • Be sure to download both the JDK and the associated documentation, because you'll want to be able to browse the API (they're all HTML pages, so you just use a browser to view them).
    • If you're running on an Apple Macintosh, you probably need to get the Mac OS Runtime for Java (MRJ) . (Note that I know almost nothing about Macs, so I can't really advise you on what's good to do on that platform!)
  2. An editor of some kind.
    • A plain editor (not a word processor like Word). It needs to generate simple ASCII files.
    • There are quite a few editors available on the Web for free, ranging from the simple to the sophisticated.
    • Some editors available on the Web are more like simple IDEs. You can create Java source code within them, including syntax coloring in some, and you can compile and build your programs from within the editor. However, they usually lack Java debugging, which you will probably find unsatisfactory. A good example is Programmer's File Editor, which is a very good general purpose programmer's editor (for Microsoft Windows systems only); you may have to manually configure it for Java.
    • Windows 95/98/NT comes with Notepad, which is pretty rudimentary; I would recommend something better than that!
    • UNIX systems come with an editor like vi. Often emacs is available, too. If emacs doesn't come with your system, you can download a version from http://www.gnu.org/software/emacs/emacs.html

I really recommend getting some kind of IDE.  Using the bare JDK can be a real pain, and you can spend more time fighting that than learning Java! On the other hand, if you're a real dyed-in-the-wool command line honcho, go for it!

Here is a list of things you need to do:

  1. If you haven't done so already, install the software on your system
    • If necessary, configure your software/system to set environment variables, etc.
    • Be sure to read any Read.me files, etc.
  2. Learn how to use your IDE
    • Read the documentation/help/tutorial/whatever
      • These days, it seems that the documentation for a lot of products is not great, and is often not in hard copy.
      • Books often provide a superior description of how to use a product.
    • With an IDE, you will typically have to learn about 'Projects'
      • How to create one
      • How to configure it/set the necessary parameters
      • Use the editor to create Java source files
      • Compile and build the project
      • Run the resulting executable
      • Debug it.
    • It sounds like a lot, but it's usually a far superior experience to using the bare-bones tools provided by the JDK, which are pretty primitive.
  3. If you use just the JDK with an editor, you'll need to:
    • Learn how to use the editor and, if necessary, to configure it for programming in Java
    • Learn how to use javac, java, and other JDK utilities
    • You'll need to make sure things (especially environment variables) are configured properly so that these utilities work.

You will need to pay close attention to how to set up the following:

  1. The PATH environment variable (the installation of your JDK and/or IDE may take care of this).
    • The main thing to look for is that the PATH includes the /bin directory of your chosen JDK as one its first entries.
    • It is critical that the PATH point to the same JDK as the CLASSPATH, else wierd things will likely happen.
  2. CLASSPATH (how this is done varies with which environment you're using)
  3. Where your current working directory is.
  4. Where the input .java files should be placed.
  5. Where the output .class files are placed by the Java compiler.
  6. Where the input .class files are expected to be located by the Java compiler.

NOTE: One critical thing is that the source code for a (public) Java class must be placed in a file whose name is identical to the Java class name (case is important!), and whose filetype is .java. If you do not do this, the Java compiler will refuse to compile your program.

If you're using the JDK directly, or if your IDE uses the JDK (most of them do in some way), try hard to understand the following command line options:

JDK Tool Option Explanation
javac
(the Java compiler)
-classpath <classpath> Sets the user class path
  -d <directory> Sets the destination root directory for class files
  -g Generates all debugging information
  -verbose Prints messages about what source files are being compiled and what class files are being loaded.
     
java
(the Java Interpreter)
-classpath Specifies where to look for class files
  -verbose Prints a message each time a class file is loaded.

Note: Try to understand the CLASSPATH behavior in your version of the JDK.

If you're using an IDE, then a lot of this is taken care of for you by your IDE (although it's often necessary to understand these concepts in order to interpret the IDE's options, which often map directly to the JDK options). However, you do have to understand how to set the proper parameters for your environment, and for the project you're working on.

  1. Here's a program to type in and run/debug to test all this out:

Note: This program contains intentional errors, so you can get some practice with dealing with compilation and run-time errors. I hope your compiler produces helpful error messages!

Simply cut and paste the program source below, from your browser into your editor.

Be sure to save the result into a file named Assignment1.java (note that upper and lower case letters are not equivalent in Java) before you try compiling it.

public class Assignment1
{
    public static void main(String[] args)
    {
        double x = doFloats();
        doByte();
        doCondition();
    }

    private static double doFloats()
    {
        System.out.println("In doFloats()");
        double one;
        double two = 0.0;
        double result = one/two;
        System.out.println("Result = " + result);
        return result;
    }

    private static void doByte()
    {
        System.out.println("In doByte()");
        byte result = 127;
        result++;
        System.out.println("Result = " + result);
    }

    private static void doCondition()
    {
        System.out.println("In doCondition()");
        int done = 0;
        int count = 5;
        while (!done)
        {
            System.out.print(count + " ");
            if (count-- == 0)
                done = 1;
        }
        System.out.println();
    }
}

Hint: Think in terms of using an appropriate datatype for each operation -- if something is trying to represent true or false, for example, then it should be of type boolean.

  1. Here's a very important second exercise:

  1. Create a new file called Assignment1a.java
  2. Copy the contents of the Assignment1.java file (the one with all the bugs fixed) into this file.
  3. Change the class name from Assignment1 to Assignment1a
  4. Insert the following text as the first line of the file:
    package assignments;

    In other words, place it before the line:

    public class Assignment1a
  5. Find every .class file that you generated from the first part of this exercise, and delete them all.
  6. If you are using an IDE of some kind, you will have to re-specify the class to use as a main class:  instead of simply Assignment1,  you will need to change this to assignments.Assignment1a.  Similarly, if you are using the JDK directly, you will have to modify your use of the java command from something like:
    java Assignment1

    to:

    java assignments.Assignment1a
  7. Now compile the new file, and see what you have to do to get it to work again, with the package statement inserted.

Note: Here it is necessary to understand the above javac and java options, especially where the .class files are placed by the compiler, depending on what package name is specified!

Hint: The -verbose option may help you figure things out...

  1. Here's what I expect you to hand in:

    1. A report on exactly what you did. Include enough detail so I can tell you did the assignment and understood things, but not so excruciatingly detailed that I fall asleep reading it!
    2. Anything you can cause your Java environment to produce that will show me you've done the work. This might be:
    • Listings of programs (with error messages?)
    • Screen shots showing compilation errors, run-time errors, etc.

    Hint: On Microsoft Windows, CTRL/PrintScreen will place a copy of the screen into the paste buffer; you can then paste the results into a program that can deal with bitmaps --MS Word, Paint, whatever, and from there print the results out to a printer.)

    • Anything else you can figure out -- convince me that you've been conscientious!
    1. A listing of the above programs, with the compilation and run-time errors corrected.
 

This page was last modified on 02 October, 2007