[ http://cse.csusb.edu/dick/cs320/ ]
Your task is to try some the experiments on this page.
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.
In JBH358 the Firefox browser has the latest Java plugin loaded.
However, it doesn't trust my web site to deliver secure applets -- they were
developed with an older Java Software Development Kit (SDK). You should tell the browser to
(1) run the first applet when the browser grumbles about it, and (2) also tell it to
not ask you about this web site again.
- Keep your Java handout handy.
- Put an applet called AaAa in file AaAa.java not in a file called aaaa.java.
- See
[ java.html ]
and
[ ../java/ ]
Here
[ ../java/test.Goodbye.html ]
is a simple Java applet in an HTML page. The page also has links to
the Code I wrote, the documentation, and bytecode
that I prepared from it. Follow the links to the
Source code, Documentation and Byte Code.
You can use this page as a model for your own examples of Java Applets.
Here
[ ../java/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.
Here you will need to have two windows working at one time: One running
a browser (say Firefox 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 had different reliable 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.java
Notice: not hello.java, not h.java, but Hello.java. This is important!
Compile it like this:
/share/java1.4.1/bin/javac Hello.java
or perhaps
/share/j2sdk1.4.2_07/bin/javac Hello.java
Note: Do not hold your breath waiting for this to compile, the compiler
was very slow when I tested it.
- If the above worked you now have a file called 'Hello.class'. It is
a binary file. Do not try editing or listing it!
- If the 'javac...' doesn't work you may have a typographical error...
First check that you did type '.../javac Hello.java' as shown. Then try
downloading
[ ../java/Hello.java ]
as Source into a file called "Hello.java" and try again.
Run the compiled class like this:
/share/..../bin/java Hello
Notice
- 'java Hello.java' and 'java Hello.class' do not work!
- The source is compiled, but a class is interpreted.
- Java code was designed to be interpreted by a
machine independent virtual machine that runs bytecode.
- bytecode::= See http://cse.csusb.edu/dick/samples/java.html#bytecode.
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
I named the program Q for a character played by John De Lancey in
StarTrek: The Next Generation, my Q program is almost as
quirky and powerful.
If your shell reports that 'Q can not be found', then you can
- Use
~dick/bin/Q Hello.java
instead.
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 task is to construct a public WWW page that has an applet
that displays some text. The name of the page is:
test.HelloWorld.html
and 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.java
containing
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.java
This will generate a file with "HelloWorld.class".
Notice that the compiler forces you to name the file
HelloWorld.java
and the class in the file must match
HelloWorld
and be public for it to be used over the World Wide Web.
You can view the HTML test page with a browser..... BUT
Mozilla seems to work best on our workstations this quarter.
Konqueror need to know where to find the 'java' JVM as well!
Chome is available but not very helpful.
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
- Java is supposed to offer: Write Once, Run Anywhere.
- What you get is: Write once, Debug Everywhere
(End of Net)
Your next task is to change your HelloWorld.java program
to output the result in color by adding
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));
- If your browser doesn't show your Applet... see if you can find a
menu entry that sets the "Show Java Console" option. This will show any errors
and also any System.out messages.
- Browsers do not make a useful tool when developing Java
applets for one simple reason. They do not want to reload your
Java Applet's compiled code. The reload button makes no difference!
Sometimes a SHift or Control click on the reload button may work.
Personally I include a test harness in the class itself, as
follows.
An application is a class with a 'main' function. An Applet
needs a graphic window to run in..... so if we write a
main function for HelloWorld that creates a graphic window then
it can run the Applet. Get a copy of this:
[ ../java/HelloWorld.java ]
and compile and test it like this:
javac HelloWorld.java
java HelloWorld
Once this runs ok, change the color and form of the graphic string.
Make the result run.
- Adding a suitable main function is a viable development technique. However
Java does not make it easy to add a function to a class.
- It is worth keeping 'main' in
a special file and learning how to read files into Java code
using your favorite editor.
Don't forget that you must always publish the *.class files for
all Applets on your web pages... and all the classes that they call.
You should always make the documentation available on any class you publish
so that others can use it. In this class you also need to publish the
source code so that It can be graded! You should have noticed
that my favorite technique for Applets
is to add links to a published test page that links to the source code.
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* ~/web
or
cp *HelloWorld* ~/web
(depending where your public WWW/Web directory is).
Now use your browser to look at the page... Follow the links to the code
and then to the documentation, and from there to the documentation
on String.
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.
This is an application that was used to teach me what a loop
was, back in the 1960s. It also shows how Java Input/Output
works.
The documentation for the "Luv" example is at
[ ../java/Luv.html ]
Study the documentation a bit before you download your own copy of
the source code
[ ../java/Luv.java ]
then compile and test the application.
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:
[ ../java/test.Luv5.html ]
While you are there follow the links to the Code and Documentation.
Here
[ IStack.java ]
and
[ MyStack.java ]
is an example of an interface and a primitive implementation.
Compar it with
[ istack.h ]
[ stack.cpp ]
[ test.stack.cpp ]
in C++.
. . . . . . . . . ( end of section Extras) <<Contents | End>>
- Make sure your PATH variable has /share/java1.4.1/bin or else
/share/j2sdk1.4.2_07/bin added to it when you next plan to
use Java.
- To add /share/j2sdk1.4.2_07/bin to your UNIX shell PATH
Net
edit your ~/.bash_profile file so that the
PATH is redefined precisely like this
PATH="$PATH:/share/j2sdk1.4.2_07/bin"
PATH="$PATH:/share/bin"
(End of Net)
This is reread whenever you login or open a terminal window.
Test it before you logout by executing
. ~/.bash_profile