Goals
Your task is to try some the experiments on this page.
Deliverable
Write notes on three experiments and include links to the code.
If the experiment includes an applet then embedd the applet in your web page.
Browser
In JBH358 the Firefox browser has the Java plugin loaded. Use the command
firefoxin a terminal window to run this browser.
You can use this page as a model for your own examples of Java Applets.
Demonstration 2
Here
[ test.Henrici.html ]
is a demonstration of something that was easy to program in
Java. Have a look at it.... the graphic is produced by the
Java applet in the HTML page. You can also follow the two links
to the Java source code and to the documentation generated by the
source code.
Note. You don't have to understand why this picture is called an Henrici spiral or what the mathematics means. Its just a formula that generates rather nice looking graphics -- enjoy it.
Your First Java Program
Here you will need to have two windows working at one time: One running
a browser (say Netscape on your workstation) and the other to edit, compile, and
run commands in the Java Development Kit(JDK). The various versions
can be listed by inputing the UNIX command into a command/terminal window:
ls -ld /share/j*Different machines seem to have different relaible ones so you may have to try out several below.
In your browser window look at this:
import java.lang.*;
public class Hello {
public static void main(String argv[]){
System.out.println("Hello, World!");
}
}It is a traditional first C program written in Java. Perfect for experimenting with the compilers and JVMs.
Use your favorite editor to create a copy of the program above in a file called.
Hello.javaNotice: not hello.java, not h.java, but Hello.java. This is important!
Compile it like this:
/share/java1.4.1/bin/javac Hello.javaor perhaps
/share/j2sdk1.4.1/bin/javac Hello.javaNote: Do not hold your breath waiting for this to compile, the compiler was very slow when I tested it.
Run the compiled class like this:
/share/..../bin/java Hello
Your Second Java Application
Modify your Hello.java program -- using your favorite editor --
to output a message with your name on it.
(use the same name for the file and the class)
Then try out my quickie or Q program to compile,
document and run your program.
Q Hello.java
If your shell reports that 'Q can not be found', then you can
~dick/bin/Q Hello.javainstead.
Demonstration of Polymorphism in Java Application
In the class we met a simple example of polymorphism in
Java
[ ../15q.java ]
and in C++
[ ../15q.cpp ]
download these files as source, save them, compile them and make
the C++ program do the same thing as the Java application.
Your First Applet
Your task is to construct a public WWW page that has an applet
that displays some text. The name of the page is:
test.HelloWorld.htmland it should contain the following HTML:
<head><title>Test Hello World</title></head><body>
<APPLET CODE="HelloWorld.class" HEIGHT=150 WIDTH=150>
You can not see this brilliant Java Applet.
</APPLET>
</body>You now need a file called:
HelloWorld.javacontaining
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet {
public void init() {
setSize(150,25);
}//init
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}//paint
}//HelloWorld
The Applet is compiled just like any other program:
/share/java1.4.1/bin/javac HelloWorld.javaThis will generate a file with "HelloWorld.class". Notice that the compiler forces you to name the file
HelloWorld.javaand the class in the file must match
HelloWorldand be public for it to be used over the World Wide Web.
You can view this file with a browser..... BUT make sure it is Netscape 6.* not 4.* on your workstation. Or else try Konqueror or Mozilla (and turn on the Java and console). Konqueror need to know where to find the 'java' JVM as well!
Before others can access your work you must
copy all the *HelloWorld* files to
your public directory we will worry about this in a later!
Note
Net
g.setColor(Color.blue);to HelloWorld.java. Compile and then publish to check the result. Make sure you shut netscape down and restart it.... otherwise you'll probably get the OLD cached version:-(
Next change the font in your graphic:
g.setFont(new Font("Helvetica", Font.BOLD, 24));
javac HelloWorld.java
java HelloWorldOnce this runs ok, change the color and form of the graphic string. Make the result run.
Edit test.HelloWorld.html so that is has a links to the Source code and documentation after the end of the applet:
<a href="HelloWorld.java">Source Code</A>and publish all the files by copying them all to your public directory:
cp *HelloWorld* /www/public/..../or
cp *HelloWorld* /web/public/..../(depending where your public WWW/Web directory is).
Now use Netscape to look at the page... Follow the links to the code
and then to the documentation, and from there to the documentation
on String.
Earn credit for the lab
Write up your experience in a new CS320/java page or (if it works)
on your test.HelloWorld.html page. You can get full credit for
programs that run in private in this lab.
Extras
Study the documentation a bit before you get your own copy of the source code [ Luv.java ] Compile and test the application.
A Modern Version
These days users expect to point and click rather than input
text to answer questions. To see how the previous program looks
as an event-driven applet follow this link:
[ test.Luv5.html ]
While you are there follow the links to the Code and Documentation.
. . . . . . . . . ( end of section Extras) <<Contents | End>>
PATH="$PATH:/share/j2sdk1.4.1/bin"
PATH="$PATH:/share/bin"
Test it before you logout by executing
. ~/.bash_profile
. . . . . . . . . ( end of section CS320: First Java Laboratory) <<Contents | End>>
Check the Preparation for next class
[ ../16.html ]
End