HW 01
Compiling an Applet
Our text uses the naming convention that strings together the words in
a title, with the initial and each subsequent word starting with a capital
letter. For example, if we wanted to write a program which calculated the
number of days since you were born, they might choose the name NumberOfDaysSinceBirth.java
for the source code file. Unix and Java are case sensitive and we will
use the text's convention as well (but it is not part of Java nor Unix,
just a specific convention which is easy to remember). It is considered
good practice to keep Java programs separate or compartmentalized. The
basic building blocks of Java programs are referred to as classes,
and this last consideration suggests that we use a separate source file
for each class. One advantage of this practice is that it makes it easy
to re-use our code (or other persons code for that matter).
Class_name = source_file_name
One convention, which is part of Java and a cause of many errors early
on, is that the source filename (without the .java extension) must
exactly match the name of the first (and typically only) class
which is defined in the source file. For example, if you have a source
file named SilLycOnvenTion.java, it
MUST define the class SilLycOnvenTion.
This may seem like a silly convention, but it has good reasons behind it.
Re-using Code
The authors have constructed a Java class called PeopleInfo. It's
definition is included in the file PeopleInfo.java.
Since the purpose of this activity is to see how to compile an applet,
don't worry about the meaning of the Java contained in this file, nor what
it does. Assume it works (it does) and we'll simply use it in our first
program. Again, the point of this homework is simply to walk through the
compilation and display of an applet.
Setup for a Program
One way to keep your file structure organized is to create a directory
(use the mkdir Unix command) for this course, named say cs120
or something. For each program you create, make a directory underneath
your cs120 directory with a descriptive name. You might call the directory
for this program, FirstProgram or firstapplet for example. You can place
all relevant files for a given programming assignment in that directory.
Java has an easy time finding files if they are all located together. Do
this now from a terminal window, then follow the link above to the PeopleInfo.java
file and save that file into your newly created directory for this assignment.
Also copy the file FirstProgram.java into
the same directory. In the terminal window, (if needed) navigate to your
directory for this assignment and issue the ls -l command, to make
sure that both source code files are present. The code inside FirstProgram.java
appears on page 22 of the text. The class declaration line:
public class FirstProgram extends PeopleInfo {
is what requires the file PeopleInfo.java, since our new class, FirstProgram,
extends or "adds onto" the PeopleInfo class. The class PeopleInfo is declared
to be an extension of an applet, so FirstProgram is also an applet.
Compiling a Java Source File
The compiler converts your Java source code (a name.java
text file) into Java byte code (something like name.class and
this file will look like gibberish to a human) which is used by the Java
virtual machine that runs your program.
To compile your program, go to the Terminal window and type javac
followed by the name of your program, including the .java
extension. (See the javac man page for options if you are
curious.)
For example, the file FirstProgram.java requires the PeopleInfo
class, so let's compile PeopleInfo.java first. In a terminal window (in
the directory which contains the source file) type javac
PeopleInfo.java to compile it. If all goes well (it normally
won't, but should this time), the Java byte code will be placed in the
file PeopleInfo.class (in the same directory where you compiled
the Java source code). In the terminal window, execute the Unix command
ls -l (list directory in long format) to see if the class file is
present. Now compile the FirstProgram.java file and check to make sure
that the class file gets created.
Viewing a Compiled Applet
Applets were meant to be viewed in a browser, like Netscape, but that requires
a lot of effort and sometimes changing settings in your Netscape setup
file. Sun provides a perfectly satisfactory applet viewer application which
allows you to "test" applets quickly with much less hassle and computer
memory. All browsers and Sun's appletviewer application as well,
require that you first create a Web page, using HTML (Hyper Text Markup
Language), before an applet can be displayed. I don't want to overdo things
in this lab, so I've written a sample HTML file that can be quickly edited
and made to display any simple applet. Once you've read the rest of this
portion of the assignment, right click the link below and open a
new browser window for the template HTML file. You can return to this file
by using the "Back" button. Once the template file loads, select the "View"
menu item and then "Page Source" to read the raw HTML code inside the template
file. Some of you may be able to figure out what it means, don't worry
if you can't, we'll come back to it much later in this course. After reading
the raw HTML (I've added several comments to it), use the "File" menu on
Netscape to save the linked file into your directory with the other files
for this assignment. Here's the template HTML
source file.
From a terminal window, check to make sure that FirstProgram.html is
in the same directory as your other class and java files. If it isn't,
ask for help.
To view the compiled applet, at a terminal window prompt, issue the
command appletviewer testapplet.html (in the future, you should
replace testapplet.html with the name of your HTML file which has
your applet embedded in it). Be sure to click on the left and right panels
in the applet that appears. HTML files are plain text (ascii) and can be
edited with nedit or any other text editor.
Submitting This Part of Lab02
Using a text editor, create a file called lab02. In that file include the
names of whomever worked on this part of the assignment with you, and what
you thought of this portion of the lab (which parts were confusing, where
did you get stuck, what parts were easy etc.). When that is done, click
here to return to part 2 of this lab exercise.