Home -> Teaching -> CSAS 1111 -> Assignment 1

Creating, Compiling, and Executing Java Programs

The Basics - Creating a Snow Man

Your assignment is to create a Java program that draws a figure that resembles, as best as you can, a snow man. You may take the program "Rings" listed below as a guide.

To guide you through the creating, compilation, and execution process for Java programs, here are a few tips.

Table of Contents:

    1. Obtaining the Tools
    2. Creating the Source Code File
    3. Compiling your source code file
    4. Executing your Code
    5. Refining your Program
    6. A more complicated Program: Rings.java
    7. How to go about creating a "Snow Man"

If you use the SHU Public Labs, please note: In the Seton Hall public labs the JDK version installed is version 1.1.6, and not 1.2. It does not make a difference to this class whether you are using version 1.1.6 or version 1.2, but if you are using a public lab computer, you should replace all references to jdk1.2 in the following text by jdk1.1.6, i.e. if it states here to type c:\jdk1.2\bin\javac\javac filename.java and you are using a public lab where that command does not work, you should type c:\jdk1.1.6\bin\javac filename.java instead.

1. Obtaining the Tools

You will need two programs that help you create your own Java program.

Programmer's File Editor
This is a nice, multi-window text editor with some additional features that help create, compile, and execute your own programs.
Java's Developer's Kit  and Documentation for Windows 95/98/NT
This contains the compiler, the Java Virtual Machine, and many other tools that might be helpful to create Java programs, as well as the documentation and API for the Java Developer's Toolkit. You really must have these to use Sun's JDK effectively

You need to download and install both programs as discussed in class, accepting all default values when installing the program. The remainder of these instructions will assume that you are using these tools to create your Java program. You are free to use any other Java compiler you can find, but in this class everything will focus on the above two tools - after all, they are free, and will serve us perfectly well. 

2. Creating the Source Code File

Click on START, choose Programs, then start the Programmer's Editor. Select File | New and type in the following little Java program:

public class Welcome
{
   public static void main(String args[])
   {
      System.out.println("Welcome to the world");
      System.out.println("of Java.");
   }
}

Please note that you must type the above lines exactly as shown, including any capitalization, semicolons, and everything.

Next, select File | Save and save your file as C:\TEMP\Welcome.java. Please note that the filename must be Welcome.java, including the capitalization, the period, and the java at the end.

We have chosen to save our file in the folder Temp on your hard disk C:. Any folder would work, actually, as long as the name of your file is Welcome.java

3. Compiling your source code file

With your Programmer's file editor still open, position your mouse to point anywhere inside your source code document. Then click the right mouse button and select Change to File's Directory. You always have to change to the file's directory, regardless of which Java source code file you are going to compile.

Now press F11, or click on the fourth button from the right on the button bar. A Window will open up, asking you to enter a Command: and a Directory:.

You need to enter the following information in that windows:

Command: C:\jdk1.2\bin\javac %f
Directory: . (a single period)

Please note that you only have to do that once. If you compile additional files, these entries will "stick", and you can accept them regardless of the source code file (but you must have first switched to the "file's directory".

Now press RETURN (or click on OK) to compile your file.

A DOS Window will briefly open up and the javac compiler will attempt to compile your program. If you have no errors in your source code, you will see a second windows in the Programmer's Editor called CommandOutput that will not contain any error message. If your source code does contain a mistake, one or more error messages will appear in the CommandOutput window. You need to click back on the Welcome.java and fix your mistakes. Remember, your code has to look exactly as the one above, and it must be saved as Welcome.java. If you need to compile your modified code again, just hit F11, then press Ok once (or twice, if your source code needs to be saved again).

Hopefully, you will be able to compile your source code so that there are no mistakes, eventually. 

4. Executing your Code

Once your source code has compiled without errors, you are ready to execute your program. Make sure the Programmer's Editor is your current window, then press F10 (or click on the fifth button from the right). A DOS box will appear, already in the same directory where your source code and class file is located (if you right-clicked to "change to file's directory" previously).

Now you can start the Java Virtual Machine and have it execute your program. Type, at the DOS prompt:

   c:\jdk1.2\bin\java Welcome

If all is going according to plan, you should now see on your screen the lines

Welcome to the world
of Java.

5. Refining your Program

Now let's assume you want your program to say "Welcome to the world of Java - it is cool.". Here are the steps involved to accomplish this in the most effective way:

      System.out.println("of Java.");

to

      System.out.println("of Java - it is cool.");
Leave the DOS box open and hit ALT-TAB to cycle through your open applications until you are back at the Programmer's Editor.
Make the necessary changes to your source code. In our case, we simply change the line
Now press F11 to recompile (you may have to hit OK twice to save the new version of your file.
Press ALT-TAB to cycle through the open applications until you are back at the previous DOS window.
Press F3 to recall the last command line in the DOS window and hit RETURN

The new version of your program should now execute. You can now close the DOS box by typing the word exit at the prompt, then RETURN - that will close the DOS window again. 

6. A more complicated Program: Rings.java

Now we are ready to create, compile, and execute a more complicated program: Rings.java which is discussed in your text book.

First, here is the source code (note that I have actually modified the book's version just a little, you could use the book's version just as well):

import java.awt.*;
import java.awt.event.*;

public class Rings extends Frame
{
   private class WindowCloser extends WindowAdapter
   {  public void windowClosing(WindowEvent we)
      {  System.exit(0);
      }
   }

   public Rings()
   {  setTitle("Rings");
      setSize(300, 200);
      setVisible(true);
      addWindowListener(new WindowCloser());
   }

   public static void main(String args[])
   {  System.out.println("Starting Rings program");
      Rings rings = new Rings();
   }

   public void paint(Graphics g)
   {
      g.setColor(Color.red);
      g.drawOval(10, 30, 30, 30);
      g.setColor(Color.blue);
      g.drawOval(35, 30, 30, 30);
      g.setColor(Color.green);
      g.drawOval(60, 30, 30, 30);
      g.setColor(Color.yellow);
      g.drawOval(85, 30, 30, 30);
      g.setColor(Color.black);
      g.drawOval(110, 30, 30, 30);

      g.drawString("Rings", 40, 100);
   }
}

Not only is this a longer program (with plenty of opportunity for types), it also uses a lot of Java programming techniques that we will not encounter for a while. But, the program is fun, and after all, you just need to type it up exactly as displayed (including the capitalization and all periods, brackets, and semicolons) - how hard can that be (you might wonder -:).

Here are the steps to create, compile, and execute the above program in short form (after all, you can find explanations of the various steps involved above).

Type the program exactly as displayed into a new window in the Programmer's Editor
Save the program as Rings.java in any directory you wish.
Right-click and Change to File's Directory
Press F11 and hit RETURN to compile.
Fix any errors if necessary and recompile
When there are no more errors, hit F10 to bring up a DOS box (will appear automatically in the same directory where the source code and the class file are)
Type c:\jdk1.2\bin\java Rings to execute your program

A standard Window will appear, showing five interlocking rings (similar to the Olympic Rings). You can close that program (as is usual for Windows 95 programs) by clicking on the "X" box in the upper right-hand corner. You can also move and resize the window, just like any other Windows 95 window. 

7. How to go about creating a "Snow Man"

To create a "snow man" instead of just five rings, you should first create the Rings.java program as discussed before. Then you should modify the various g.drawOval statements, recompile, and execute the new version of your program. Do not make too many changes at once. Instead, make only one, two, or at most three changes each time, then recompile and re-execute to see if your new version of the program is closer that the previous version.

If you prefer to draw filled-in cicles (in the current drawing color), you can use the command g.fillOval(...) instead of g.drawOval(...) which will simply draw an oval (as before) and fill it with the current drawing colors.

When you are all done, and you have create a perfect "snow man", you should send me the source code file only, and you should send it exactly once.

Best of Luck !!!

 

Bert G. Wachsmut
Last modified: 05/03/00