Assignment 2

Back ] Up ] Next ]

Discussion group, leave your messages

Home
Assignment 1
Assignment 2
Assignment 3
Assignment 4
Assignment 5
Last Assignment

 

Compiling a Java program 

Due Date: announced in class, check message board 50 Points

This assignment will allow your to use BlueJ and compile your first java application.

1. Create a new folder in your Home Folder on the C: or D: drive and give it a name (e.g. hellojava). All of the folders and files associated with this practical should be placed in this new folder. In the rest of this document, this folder will be referred to as hellojava, so remember to use the name of your folder in place of that.

You are now ready to use the Java programming environment, using an application called BlueJ provided by Monash University in Australia. At this stage we are going to get you to compile and execute a program that we have provided. This will give you some experience of using the BlueJ environment before you have to cope with writing your own Java programs.

A compiler is a software system that is used to check the syntactic correctness of a computer program written in a specific language such as Java, and if all is well, then to translate it into a form that the computer can then execute.

The first Java program you are going to run is a simple one that you can experiment with to find out how BlueJ is used. I suggest that every time you start a brand new program (technically called a project), you create a new folder to contain the various files you will need.

2. Download this file (hello.zip) and place it in the folder that you just created called hellojava. Having clicked on the above link, you will have downloaded a zip file.

  1. Using your zip application Extract the files to the hellojava folder. Since most zip applications work a little bit different, you will need to find out how to extract files. See me during lab hours if you have questions. If you do not have a ZIP program, you can find many in the www.tucows.com site. Winzip is a popular application.

Go into the Explorer window and find your new hellojava folder, and see if it contains the four files
bulletHello.java - contains the Java source program
bulletREADME.txt - should contain the documentation that will be written by the programmer about this project.
bulletbluej.pkg - file contains a list of all classes in the project.
bulletbluej.pkh - This is a backup of the last version of "bluej.pkg".

The presence of bluej.pkg and bluej.pkh files within a folder identifies that folder as containing a BlueJ project.

3. Open the BlueJ system. When BlueJ is ready for use, you will see a blank (grey) window with four menus. Pull down on the Project menu and select the Open Project item. (You may need to click this item again to make it respond). This will open a File Dialog to allow you to navigate around to find your project. Initially, you will see all of the folders in your bluej directory. Navigate back to the drive and folder where you have your Bluej project. You should now see your hellojava folder, but instead of the usual folder icon, it will have an icon that looks a bit like a wrapped parcel. Although Windows Explorer regards it as an ordinary folder, BlueJ recognizes the two "bluej" files in the folder, and regards the folder as a BlueJ project.

Select on the hellojava icon in the BlueJ window and press the Open button. The project window will now change, and it will look like this:

BlueJ project window

 

The icon in the top left hand corner represents the document describing the project. 

The other icon represents the class file containing the Java source code for the Hello program. Notice that at present it is colored orange, and has black diagonal lines across it. 

4. Click on the Compile button. This will cause BlueJ to call the Sun JDK compiler to compile all of the Java source files for the project (only one in this case). If the compilation fails (though it shouldn't this time), the source window will be opened (if it wasn't already opened), an error message will appear at the bottom of the source window, and the offending line in the source code itself will be highlighted. If the compilation is successful, the class icon in the project window will change color to just orange, and if the source window is open, a message will appear stating that the compilation was successful.

5. To run the program, move the cursor over the class window and right-click (i.e. press the right-hand mouse button. A menu will appear giving a number of options. Select the option which says void main (). The program will then run, and create a new output window with the text Hello world! printed in it. Note that this window does not disappear when the program terminates - you have to close it explicitly (by clicking on the cross in the top right hand corner) when you no longer need it. In addition, unless you explicitly remove the contents of the Terminal Window once a program has ceased execution, they will remain. To remove previous output from the Terminal Window, select the Options menu and the Clear option. The other thing you may notice when your program executes is that the "Barber's Pole" in the project window changes color and begins to rotate while the program is running, but reverts to its gray color when the program finishes.

Congratulations! You've just compiled and run your first Java program.

6. Double-click on the Hello icon in the BlueJ project window. This will bring up a window in which the Java source code of the program is displayed. Find the point in the program where the text "Hello world!" is printed out, and alter it to print out some other message, preferably  your name. Notice that as soon as you make any alteration to the Java source text, the icon in the project window will display the black diagonal lines once again. This indicates the file needs to be compiled again.

7. Re-compile the program, and run it. The output window will open up again (if you closed it, that is), and the program will display your new message. If you made a mistake in editing the program, the compilation will fail. Notice, however, that the editor itself knows a bit about Java, because if you delete the final double-quote symbol ("), the characters in the text will be colored red, and they will turn back to their original green color when the final double-quote is restored.

Implement the following changes on this program:

Change those two lines, change the author with your name and the version with the date when your did this assignment.

* @author CRS 
* @version 19/9/2000

Put the symbol // in front of the statement
System.out.println("Hello world!");

and recompile the program. 

Answer this in a separate sheet of paper:

  1. What happened? 
  2. Any ideas why?
 

8. Modify the Hello program so that it will now print out your name and the college name. Remember to recompile the program and run it. 

When you run the program, it will open the Terminal window with the output of this program. Select the entire text by dragging your mouse over it, select copy from the Options menu. This will place a copy of the output of your program to the clipboard.

9. Start the Notepad application, select the option Edit, then Paste. This should put a copy of the output of your program in the body of Notepad.

Submit:

STAPLE these three sheets together.

1. The answer to the questions above.

2. The output of your your program that was "pasted" on Notepad.

3. The source code for this program.

Click on the Close (X) button in the top-right corner of the BlueJ Terminal Window. Also close the Hello program source window in the same way. Using the BlueJ Project menu, click on the Close option. That will terminate our interest in the Hello program and will keep the desktop tidy.

 

Back to CS161 JAVA Homepage
This page was last modified January 10, 2002
wmorales@pcc.edu